Skip to content

Update no_grad usage to inference_mode if possible#2534

Merged
rwightman merged 2 commits intohuggingface:mainfrom
GuillaumeErhard:inference_mode
Jul 4, 2025
Merged

Update no_grad usage to inference_mode if possible#2534
rwightman merged 2 commits intohuggingface:mainfrom
GuillaumeErhard:inference_mode

Conversation

@GuillaumeErhard
Copy link
Copy Markdown
Contributor

@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

train.py Outdated
)
if args.head_init_scale is not None:
with torch.no_grad():
with torch.inference_mode():
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 one should probably remain as no_grad

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it works. It does not interfer with the graph / properties of existing variable, and the mul just scale and does not create new params.

I just tried to run train.py for some steps with those params and saw no error
--model resnet18 --dataset hfds/imagenet-1k --head-init-scale .1

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.

All the same, would like to revert that to previous... everything else looks good assuming no test fails, which I doubt.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@rwightman rwightman merged commit 6239313 into huggingface:main Jul 4, 2025
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants