Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

Fix text generation not properly cleaning itself up. (#17169)

comfyanonymousb3522383ab99
diff --git a/comfy/text_encoders/llama.py b/comfy/text_encoders/llama.py
index 542c4590e..88c0d6b24 100644
--- a/comfy/text_encoders/llama.py
+++ b/comfy/text_encoders/llama.py
@@ -1205,61 +1205,63 @@ class BaseGenerate:
penalize = penalty_active(repetition_penalty, presence_penalty)
penalty_mask = None
- # Generation loop
current_input_ids = initial_input_ids
- for step in tqdm(range(max_length), desc="Generating tokens"):
- if step > 0:
- if compile_allocations:
- comfy.model_prefetch.malloc_graph_begin(device)
- embed = self.model.embed_tokens
- if hasattr(embed, "_v") and embed.weight_lowvram_function is None and len(embed.weight_function) == 0 and comfy.ops.vbar_above_watermark(embed):
- embeds = embed.host_rows(decode_tokens, out_dtype=execution_dtype)
- else:
- embeds = embed(decode_tokens).to(execution_dtype)
- current_input_ids = decode_tokens if initial_input_ids is not None else None
- position_ids = torch.tensor([[next_pos]], device=device) if next_pos is not None else None
-
- # DeepStack visual features are injected on the prefill only; gemma4's forward lacks these kwargs.
- extra = {}
- if decode_buffers is not None:
- extra["decode_buffers"] = decode_buffers
- if rope is not None and step > 0:
- past_len = self.model.get_past_len(past_key_values)
- extra["freqs_cis"] = rope[:, :, past_len:past_len + 1]
- if step == 0 and deepstack_embeds is not None:
- extra["deepstack_embeds"] = deepstack_embeds
- extra["visual_pos_masks"] = visual_pos_masks
- x, _, past_key_values = self.model.forward(None, embeds=embeds, attention_mask=None, past_key_values=past_key_values, input_ids=current_input_ids, position_ids=position_ids, **extra, embeds_info=(embeds_info if step == 0 else None))
- logits = self.logits(x)[:, -1]
- if penalty_mask is None and do_sample and penalize:
- # allocated on the (unbracketed) first step; later steps only index_fill_ it
- penalty_mask = torch.zeros((logits.shape[-1],), dtype=torch.bool, device=device)
- if len(initial_tokens) > 0:
- penalty_mask.index_fill_(0, torch.tensor(initial_tokens, device=device), True)
- next_token = self.sample_token(logits, temperature, top_k, top_p, min_p, repetition_penalty, [], generator, do_sample=do_sample, presence_penalty=presence_penalty, penalty_mask=penalty_mask)
-
- decode_tokens.copy_(next_token)
- if penalty_mask is not None:
- penalty_mask.index_fill_(0, decode_tokens[0], True)
- del next_token, logits, x, embeds, position_ids
- if step > 0 and compile_allocations:
- comfy.model_prefetch.malloc_graph_end()
-
- token_id = decode_tokens[0].item()
- generated_token_ids.append(token_id)
-
- if step == 0 and hasattr(self.model.embed_tokens, "_v"):
- # prefill's transients can evict body pages and lower the VBAR watermark: let the decode fault them back
- vbar = self.model.embed_tokens._v[0]
- comfy.model_management.reset_cast_buffers()
- vbar.set_watermark(vbar.max_size)
-
- if step > 0 and next_pos is not None:
- next_pos += 1
- pbar.update(1)
-
- if token_id in stop_tokens:
- break
+ try:
+ for step in tqdm(range(max_length), desc="Generating tokens"):
+ if step > 0:
+ if compile_allocations:
+ comfy.model_prefetch.malloc_graph_begin(device)
+ embed = self.model.embed_tokens
+ if hasattr(embed, "_v") and embed.weight_lowvram_function is None and len(embed.weight_function) == 0 and comfy.ops.vbar_above_watermark(embed):
+ embeds = embed.host_rows(decode_tokens, out_dtype=execution_dtype)
+ else:
+ embeds = embed(decode_tokens).to(execution_dtype)
+ current_input_ids = decode_tokens if initial_input_ids is not None else None
+ position_ids = torch.tensor([[next_pos]], device=device) if next_pos is not None else None
+
+ # DeepStack visual features are injected on the prefill only; gemma4's forward lacks these kwargs.
+ extra = {}
+ if decode_buffers is not None:
+ extra["decode_buffers"] = decode_buffers
+ if rope is not None and step > 0:
+ past_len = self.model.get_past_len(past_key_values)
+ extra["freqs_cis"] = rope[:, :, past_len:past_len + 1]
+ if step == 0 and deepstack_embeds is not None:
+ extra["deepstack_embeds"] = deepstack_embeds
+ extra["visual_pos_masks"] = visual_pos_masks
+ x, _, past_key_values = self.model.forward(None, embeds=embeds, attention_mask=None, past_key_values=past_key_values, input_ids=current_input_ids, position_ids=position_ids, **extra, embeds_info=(embeds_info if step == 0 else None))
+ logits = self.logits(x)[:, -1]
+ if penalty_mask is None and do_sample and penalize:
+ # allocated on the (unbracketed) first step; later steps only index_fill_ it
+ penalty_mask = torch.zeros((logits.shape[-1],), dtype=torch.bool, device=device)
+ if len(initial_tokens) > 0:
+ penalty_mask.index_fill_(0, torch.tensor(initial_tokens, device=device), True)
+ next_token = self.sample_token(logits, temperature, top_k, top_p, min_p, repetition_penalty, [], generator, do_sample=do_sample, presence_penalty=presence_penalty, penalty_mask=penalty_mask)
+
+ decode_tokens.copy_(next_token)
+ if penalty_mask is not None:
+ penalty_mask.index_fill_(0, decode_tokens[0], True)
+ del next_token, logits, x, embeds, position_ids
+ if step > 0 and compile_allocations:
+ comfy.model_prefetch.malloc_graph_end()
+
+ token_id = decode_tokens[0].item()
+ generated_token_ids.append(token_id)
+
+ if step == 0 and hasattr(self.model.embed_tokens, "_v"):
+ # prefill's transients can evict body pages and lower the VBAR watermark: let the decode fault them back
+ vbar = self.model.embed_tokens._v[0]
+ comfy.model_management.reset_cast_buffers()
+ vbar.set_watermark(vbar.max_size)
+
+ if step > 0 and next_pos is not None:
+ next_pos += 1
+ pbar.update(1)
+
+ if token_id in stop_tokens:
+ break
+ finally:
+ comfy.model_prefetch.cleanup_prefetch_queues()
return generated_token_ids