Skip to content
Open
Changes from 1 commit
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 swift/llm/infer/infer_engine/pt_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ async def _gen_wrapper():
return await queue.get()

# Ensure `template._post_encode` has no gradient.
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To improve maintainability and prevent future regressions, it would be beneficial to add a comment explaining why @torch.no_grad() is used here instead of the potentially more performant @torch.inference_mode(). This will clarify the reasoning for other developers who might not have the context of the GRPOTrainer RuntimeError.

# Use `torch.no_grad()` instead of `torch.inference_mode()` to allow model
# parameters to have `requires_grad=True` set on them later (e.g., during
# PEFT training). This prevents a RuntimeError when the model is used for
# both inference and training.

@torch.inference_mode()
@torch.no_grad()
def _infer(
self,
infer_requests: List[InferRequest],
Expand Down