Skip to content

Add autocast sub-config to inference settings and disable torch.autocast by default#3105

Merged
MMathisLab merged 6 commits intomainfrom
maxim/torch_autocast
Oct 1, 2025
Merged

Add autocast sub-config to inference settings and disable torch.autocast by default#3105
MMathisLab merged 6 commits intomainfrom
maxim/torch_autocast

Conversation

@maximpavliv
Copy link
Copy Markdown
Contributor

@maximpavliv maximpavliv commented Sep 23, 2025

Description

This Pull Request builds on top of #3104 and #3098, adding a new autocast sub-config to the inference: block of the PyTorch configuration.

The use of torch.autocast during inference was first introduced in #3012 (“Shaokai/faster inference”)
with the goal of improving inference speed.
However, we later discovered that enabling torch.autocast can significantly decrease inference accuracy — this is the root cause of issue #3083 and the forum discussion “Better results with DeepLabCut==2.3.0 than DeepLabCut==3.0 with PyTorch”.

Key Changes

  • Introduced an autocast sub-config in the inference: block of pytorch_config.yaml.
  • Added an AutocastConfig class to manage parameters consistently with the existing compile and multithreading options.
  • Inference logic now respects this configuration and defaults to autocast disabled to avoid unintended accuracy drops.
  • Advanced users can enable autocast either:
    • programmatically (by passing an InferenceConfig object or a dictionary), or
    • by editing the pytorch_config.yaml file under the inference: block.

Notes & Future Work

  • Default behavior is changed back to v3.0.0rc8 — autocast is now off by default to preserve accuracy.
  • Advanced users may still enable it to experiment with potential speedups, accepting the risk of reduced prediction quality.
  • In future work, we plan to investigate whether training models with torch.autocast can mitigate this accuracy gap and allow safer use of autocast at inference.

@maximpavliv maximpavliv added DLC3.0🔥 bug fix! fix for a real buggy one... pytorch labels Sep 24, 2025
@maximpavliv maximpavliv changed the title Maxim/torch autocast Add autocast sub-config to inference settings and disable torch.autocast by default Sep 24, 2025
@maximpavliv maximpavliv marked this pull request as ready for review September 24, 2025 08:28
@arashsm79
Copy link
Copy Markdown
Contributor

Thanks. Tested this with both autocast on and off.
LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug fix! fix for a real buggy one... DLC3.0🔥 pytorch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants