Skip to content

Commit

Permalink
Lint all unused variables (#5989)
Browse files Browse the repository at this point in the history
* Enable F841

* Autofix

* Remove all unused variable assignment
  • Loading branch information
huchenlei authored Dec 12, 2024
1 parent fd5dfb8 commit d9d7f3c
Show file tree
Hide file tree
Showing 29 changed files with 22 additions and 72 deletions.
12 changes: 6 additions & 6 deletions .ci/update_windows/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def pull(repo, remote_name='origin', branch='master'):

user = repo.default_signature
tree = repo.index.write_tree()
commit = repo.create_commit('HEAD',
user,
user,
'Merge!',
tree,
[repo.head.target, remote_master_id])
repo.create_commit('HEAD',
user,
user,
'Merge!',
tree,
[repo.head.target, remote_master_id])
# We need to do this or git CLI will think we are still merging.
repo.state_cleanup()
else:
Expand Down
1 change: 0 additions & 1 deletion comfy/cldm/cldm.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ def forward(self, x, hint, timesteps, context, y=None, **kwargs):
out_output = []
out_middle = []

hs = []
if self.num_classes is not None:
assert y.shape[0] == x.shape[0]
emb = emb + self.label_emb(y)
Expand Down
2 changes: 0 additions & 2 deletions comfy/controlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ class ControlLoraOps:
class Linear(torch.nn.Module, comfy.ops.CastWeightBiasOp):
def __init__(self, in_features: int, out_features: int, bias: bool = True,
device=None, dtype=None) -> None:
factory_kwargs = {'device': device, 'dtype': dtype}
super().__init__()
self.in_features = in_features
self.out_features = out_features
Expand Down Expand Up @@ -382,7 +381,6 @@ class control_lora_ops(ControlLoraOps, comfy.ops.manual_cast):
self.control_model.to(comfy.model_management.get_torch_device())
diffusion_model = model.diffusion_model
sd = diffusion_model.state_dict()
cm = self.control_model.state_dict()

for k in sd:
weight = sd[k]
Expand Down
1 change: 0 additions & 1 deletion comfy/extra_samplers/uni_pc.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,6 @@ def sample(self, x, timesteps, t_start=None, t_end=None, order=3, skip_type='tim
):
# t_0 = 1. / self.noise_schedule.total_N if t_end is None else t_end
# t_T = self.noise_schedule.T if t_start is None else t_start
device = x.device
steps = len(timesteps) - 1
if method == 'multistep':
assert steps >= order
Expand Down
2 changes: 1 addition & 1 deletion comfy/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def add_hook_patches(self, model: 'ModelPatcher', model_options: dict, target: E
weights = self.weights
else:
weights = self.weights_clip
k = model.add_hook_patches(hook=self, patches=weights, strength_patch=strength)
model.add_hook_patches(hook=self, patches=weights, strength_patch=strength)
registered.append(self)
return True
# TODO: add logs about any keys that were not applied
Expand Down
1 change: 0 additions & 1 deletion comfy/k_diffusion/deis.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# Transfer from the input time (sigma) used in EDM to that (t) used in DEIS.

def edm2t(edm_steps, epsilon_s=1e-3, sigma_min=0.002, sigma_max=80):
vp_sigma = lambda beta_d, beta_min: lambda t: (np.e ** (0.5 * beta_d * (t ** 2) + beta_min * t) - 1) ** 0.5
vp_sigma_inv = lambda beta_d, beta_min: lambda sigma: ((beta_min ** 2 + 2 * beta_d * (sigma ** 2 + 1).log()).sqrt() - beta_min) / beta_d
vp_beta_d = 2 * (np.log(torch.tensor(sigma_min).cpu() ** 2 + 1) / epsilon_s - np.log(torch.tensor(sigma_max).cpu() ** 2 + 1)) / (epsilon_s - 1)
vp_beta_min = np.log(torch.tensor(sigma_max).cpu() ** 2 + 1) - 0.5 * vp_beta_d
Expand Down
8 changes: 1 addition & 7 deletions comfy/ldm/audio/dit.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ def forward_from_seq_len(self, seq_len, device, dtype):
def forward(self, t):
# device = self.inv_freq.device
device = t.device
dtype = t.dtype

# t = t.to(torch.float32)

Expand Down Expand Up @@ -346,18 +345,13 @@ def forward(

# determine masking
masks = []
final_attn_mask = None # The mask that will be applied to the attention matrix, taking all masks into account

if input_mask is not None:
input_mask = rearrange(input_mask, 'b j -> b 1 1 j')
masks.append(~input_mask)

# Other masks will be added here later

if len(masks) > 0:
final_attn_mask = ~or_reduce(masks)

n, device = q.shape[-2], q.device
n = q.shape[-2]

causal = self.causal if causal is None else causal

Expand Down
1 change: 0 additions & 1 deletion comfy/ldm/aura/mmdit.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ def forward(self, c, x):

bsz, seqlen1, _ = c.shape
bsz, seqlen2, _ = x.shape
seqlen = seqlen1 + seqlen2

cq, ck, cv = self.w1q(c), self.w1k(c), self.w1v(c)
cq = cq.view(bsz, seqlen1, self.n_heads, self.head_dim)
Expand Down
2 changes: 0 additions & 2 deletions comfy/ldm/genmo/joint_model/asymm_models_joint.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,6 @@ def prepare(
pH, pW = H // self.patch_size, W // self.patch_size
x = self.embed_x(x) # (B, N, D), where N = T * H * W / patch_size ** 2
assert x.ndim == 3
B = x.size(0)


pH, pW = H // self.patch_size, W // self.patch_size
N = T * pH * pW
Expand Down
3 changes: 0 additions & 3 deletions comfy/ldm/hydit/controlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,6 @@ def __init__(
),
)

# Image embedding
num_patches = self.x_embedder.num_patches

# HUnYuanDiT Blocks
self.blocks = nn.ModuleList(
[
Expand Down
3 changes: 0 additions & 3 deletions comfy/ldm/hydit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,6 @@ def __init__(self,
operations.Linear(hidden_size * 4, hidden_size, bias=True, dtype=dtype, device=device),
)

# Image embedding
num_patches = self.x_embedder.num_patches

# HUnYuanDiT Blocks
self.blocks = nn.ModuleList([
HunYuanDiTBlock(hidden_size=hidden_size,
Expand Down
6 changes: 1 addition & 5 deletions comfy/ldm/modules/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ def attention_sub_quad(query, key, value, heads, mask=None, attn_precision=None,
b, _, dim_head = query.shape
dim_head //= heads

scale = dim_head ** -0.5

if skip_reshape:
query = query.reshape(b * heads, -1, dim_head)
value = value.reshape(b * heads, -1, dim_head)
Expand All @@ -177,9 +175,8 @@ def attention_sub_quad(query, key, value, heads, mask=None, attn_precision=None,
bytes_per_token = torch.finfo(query.dtype).bits//8
batch_x_heads, q_tokens, _ = query.shape
_, _, k_tokens = key.shape
qk_matmul_size_bytes = batch_x_heads * bytes_per_token * q_tokens * k_tokens

mem_free_total, mem_free_torch = model_management.get_free_memory(query.device, True)
mem_free_total, _ = model_management.get_free_memory(query.device, True)

kv_chunk_size_min = None
kv_chunk_size = None
Expand Down Expand Up @@ -230,7 +227,6 @@ def attention_split(q, k, v, heads, mask=None, attn_precision=None, skip_reshape

scale = dim_head ** -0.5

h = heads
if skip_reshape:
q, k, v = map(
lambda t: t.reshape(b * heads, -1, dim_head),
Expand Down
9 changes: 3 additions & 6 deletions comfy/ldm/modules/diffusionmodules/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ def slice_attention(q, k, v):

mem_free_total = model_management.get_free_memory(q.device)

gb = 1024 ** 3
tensor_size = q.shape[0] * q.shape[1] * k.shape[2] * q.element_size()
modifier = 3 if q.element_size() == 2 else 2.5
mem_required = tensor_size * modifier
Expand Down Expand Up @@ -218,7 +217,7 @@ def xformers_attention(q, k, v):
try:
out = xformers.ops.memory_efficient_attention(q, k, v, attn_bias=None)
out = out.transpose(1, 2).reshape(B, C, H, W)
except NotImplementedError as e:
except NotImplementedError:
out = slice_attention(q.view(B, -1, C), k.view(B, -1, C).transpose(1, 2), v.view(B, -1, C).transpose(1, 2)).reshape(B, C, H, W)
return out

Expand All @@ -233,7 +232,7 @@ def pytorch_attention(q, k, v):
try:
out = torch.nn.functional.scaled_dot_product_attention(q, k, v, attn_mask=None, dropout_p=0.0, is_causal=False)
out = out.transpose(2, 3).reshape(B, C, H, W)
except model_management.OOM_EXCEPTION as e:
except model_management.OOM_EXCEPTION:
logging.warning("scaled_dot_product_attention OOMed: switched to slice attention")
out = slice_attention(q.view(B, -1, C), k.view(B, -1, C).transpose(1, 2), v.view(B, -1, C).transpose(1, 2)).reshape(B, C, H, W)
return out
Expand Down Expand Up @@ -546,7 +545,6 @@ def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks,
attn_op=AttnBlock,
**ignorekwargs):
super().__init__()
if use_linear_attn: attn_type = "linear"
self.ch = ch
self.temb_ch = 0
self.num_resolutions = len(ch_mult)
Expand All @@ -556,8 +554,7 @@ def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks,
self.give_pre_end = give_pre_end
self.tanh_out = tanh_out

# compute in_ch_mult, block_in and curr_res at lowest res
in_ch_mult = (1,)+tuple(ch_mult)
# compute block_in and curr_res at lowest res
block_in = ch*ch_mult[self.num_resolutions-1]
curr_res = resolution // 2**(self.num_resolutions-1)
self.z_shape = (1,z_channels,curr_res,curr_res)
Expand Down
1 change: 0 additions & 1 deletion comfy/ldm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def step(self, closure=None):
exp_avgs = []
exp_avg_sqs = []
ema_params_with_grad = []
state_sums = []
max_exp_avg_sqs = []
state_steps = []
amsgrad = group['amsgrad']
Expand Down
3 changes: 0 additions & 3 deletions comfy/model_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ def extra_conds(self, **kwargs):

latent_image = kwargs.get("concat_latent_image", None)
noise = kwargs.get("noise", None)
device = kwargs["device"]

if latent_image is None:
latent_image = torch.zeros_like(noise)
Expand Down Expand Up @@ -711,8 +710,6 @@ def extra_conds(self, **kwargs):

width = kwargs.get("width", 768)
height = kwargs.get("height", 768)
crop_w = kwargs.get("crop_w", 0)
crop_h = kwargs.get("crop_h", 0)
target_width = kwargs.get("target_width", width)
target_height = kwargs.get("target_height", height)

Expand Down
2 changes: 0 additions & 2 deletions comfy/model_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ def detect_unet_config(state_dict, key_prefix):

num_res_blocks = []
channel_mult = []
attention_resolutions = []
transformer_depth = []
transformer_depth_output = []
context_dim = None
Expand Down Expand Up @@ -388,7 +387,6 @@ def convert_config(unet_config):
t_out += [d] * (res + 1)
s *= 2
transformer_depth = t_in
transformer_depth_output = t_out
new_config["transformer_depth"] = t_in
new_config["transformer_depth_output"] = t_out
new_config["transformer_depth_middle"] = transformer_depth_middle
Expand Down
2 changes: 1 addition & 1 deletion comfy/model_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def load_models_gpu(models, memory_required=0, force_patch_weights=False, minimu
if vram_set_state == VRAMState.NO_VRAM:
lowvram_model_memory = 64 * 1024 * 1024

cur_loaded_model = loaded_model.model_load(lowvram_model_memory, force_patch_weights=force_patch_weights)
loaded_model.model_load(lowvram_model_memory, force_patch_weights=force_patch_weights)
current_loaded_models.insert(0, loaded_model)
return

Expand Down
1 change: 0 additions & 1 deletion comfy/sampler_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def cleanup_additional_models(models):


def prepare_sampling(model: 'ModelPatcher', noise_shape, conds):
device = model.load_device
real_model: 'BaseModel' = None
models, inference_memory = get_additional_models(conds, model.model_dtype())
models += model.get_nested_additional_models() # TODO: does this require inference_memory update?
Expand Down
7 changes: 0 additions & 7 deletions comfy/samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ def objects_concatable(obj1, obj2):
return cond_equal_size(c1.conditioning, c2.conditioning)

def cond_cat(c_list):
c_crossattn = []
c_concat = []
c_adm = []
crossattn_max_len = 0

temp = {}
for x in c_list:
for k in x:
Expand Down Expand Up @@ -608,8 +603,6 @@ def pre_run_control(model, conds):
for t in range(len(conds)):
x = conds[t]

timestep_start = None
timestep_end = None
percent_to_timestep_function = lambda a: s.percent_to_sigma(a)
if 'control' in x:
x['control'].pre_run(model, percent_to_timestep_function)
Expand Down
5 changes: 2 additions & 3 deletions comfy/sd.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ def decode(self, samples_in):
if pixel_samples is None:
pixel_samples = torch.empty((samples_in.shape[0],) + tuple(out.shape[1:]), device=self.output_device)
pixel_samples[x:x+batch_number] = out
except model_management.OOM_EXCEPTION as e:
except model_management.OOM_EXCEPTION:
logging.warning("Warning: Ran out of memory when regular VAE decoding, retrying with tiled VAE decoding.")
dims = samples_in.ndim - 2
if dims == 1:
Expand Down Expand Up @@ -490,7 +490,7 @@ def encode(self, pixel_samples):
samples = torch.empty((pixel_samples.shape[0],) + tuple(out.shape[1:]), device=self.output_device)
samples[x:x + batch_number] = out

except model_management.OOM_EXCEPTION as e:
except model_management.OOM_EXCEPTION:
logging.warning("Warning: Ran out of memory when regular VAE encoding, retrying with tiled VAE encoding.")
if len(pixel_samples.shape) == 3:
samples = self.encode_tiled_1d(pixel_samples)
Expand Down Expand Up @@ -691,7 +691,6 @@ def load_checkpoint(config_path=None, ckpt_path=None, output_vae=True, output_cl
config = yaml.safe_load(stream)
model_config_params = config['model']['params']
clip_config = model_config_params['cond_stage_config']
scale_factor = model_config_params['scale_factor']

if "parameterization" in model_config_params:
if model_config_params["parameterization"] == "v":
Expand Down
3 changes: 1 addition & 2 deletions comfy/sd1_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ def expand_directory_list(directories):
return list(dirs)

def bundled_embed(embed, prefix, suffix): #bundled embedding in lora format
i = 0
out_list = []
for k in embed:
if k.startswith(prefix) and k.endswith(suffix):
Expand Down Expand Up @@ -392,7 +391,7 @@ def load_embed(embedding_name, embedding_directory, embedding_size, embed_key=No
embed_out = safe_load_embed_zip(embed_path)
else:
embed = torch.load(embed_path, map_location="cpu")
except Exception as e:
except Exception:
logging.warning("{}\n\nerror loading embedding, skipping loading: {}".format(traceback.format_exc(), embedding_name))
return None

Expand Down
2 changes: 0 additions & 2 deletions comfy/supported_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ def process_clip_state_dict(self, state_dict):

def process_clip_state_dict_for_saving(self, state_dict):
replace_prefix = {}
keys_to_replace = {}
state_dict_g = diffusers_convert.convert_text_enc_state_dict_v20(state_dict, "clip_g")
for k in state_dict:
if k.startswith("clip_l"):
Expand Down Expand Up @@ -527,7 +526,6 @@ def clip_target(self, state_dict={}):
clip_l = False
clip_g = False
t5 = False
dtype_t5 = None
pref = self.text_encoder_key_prefix[0]
if "{}clip_l.transformer.text_model.final_layer_norm.weight".format(pref) in state_dict:
clip_l = True
Expand Down
1 change: 0 additions & 1 deletion comfy/text_encoders/t5.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ def __init__(self, model_dim, inner_dim, ff_dim, num_heads, relative_attention_b
# self.dropout = nn.Dropout(config.dropout_rate)

def forward(self, x, mask=None, past_bias=None, optimized_attention=None):
normed_hidden_states = self.layer_norm(x)
output, past_bias = self.SelfAttention(self.layer_norm(x), mask=mask, past_bias=past_bias, optimized_attention=optimized_attention)
# x = x + self.dropout(attention_output)
x += output
Expand Down
2 changes: 0 additions & 2 deletions comfy_extras/nodes_hypertile.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ def INPUT_TYPES(s):
CATEGORY = "model_patches/unet"

def patch(self, model, tile_size, swap_size, max_depth, scale_depth):
model_channels = model.model.model_config.unet_config["model_channels"]

latent_tile_size = max(32, tile_size) // 8
self.temp = None

Expand Down
1 change: 0 additions & 1 deletion comfy_extras/nodes_model_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ def INPUT_TYPES(s):
def patch(self, model, sampling, sigma_max, sigma_min):
m = model.clone()

latent_format = None
sigma_data = 1.0
if sampling == "v_prediction":
sampling_type = comfy.model_sampling.V_PREDICTION
Expand Down
4 changes: 2 additions & 2 deletions fix_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def fix_pytorch_libomp():
if b"libomp140.x86_64.dll" not in contents:
break
try:
mydll = ctypes.cdll.LoadLibrary(test_file)
except FileNotFoundError as e:
ctypes.cdll.LoadLibrary(test_file)
except FileNotFoundError:
logging.warning("Detected pytorch version with libomp issue, patching.")
shutil.copyfile(os.path.join(lib_folder, "libiomp5md.dll"), dest)
1 change: 1 addition & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ lint.ignore = ["ALL"]
lint.select = [
"S307", # suspicious-eval-usage
"F401", # unused-import
"F841", # unused-local-variable
]
Loading

0 comments on commit d9d7f3c

Please sign in to comment.