Do we need to do torch.cuda.amp.autocast(enabled=False) before a custom function?

hughperkins: <p>We have some code that is designed to run in mixed-precision. It contains a custom CUDA kernel call. Currently we are placing a <code>with torch.cuda.amp.autocast(enabled=False)</code> guard around the custom CUDA kernel call, and then cast all tensors to the same type. Is the <code>autocast(enabled=False)</code> really necessary? What might happen without this block? (context: this pull request, that proposes to move the casts from out of the autocast guard block, to enable compatibility with APEX, <a href="https://github.com/asappresearch/sru/pull/158/files" class="inline-onebox" rel="noopener nofollow ugc">Enable both Pytorch native AMP and Nvidia APEX AMP for SRU by visionscaper · Pull Request #158 · asappresearch/sru · GitHub</a> )</p>

mcarilli: <p>If the function accepts arguments in any precision, and doesn’t even care if they match, you probably don’t need to do anything.</p>
<p>If you’re wrapping the op with a torch.autograd.Function:<br>
If the kernel needs matching input dtypes, but those dtypes can be anything as long as they match, the approach you describe is probably the best.<br>
If the cuda kernel needs a particular dtype, you can do it <a href="https://pytorch.org/docs/stable/notes/amp_examples.html#functions-that-need-a-particular-dtype" rel="noopener nofollow ugc">a bit more conveniently</a>.</p>
<p>If you’re registering the op with torch on the C++ side, see the <a href="https://pytorch.org/tutorials/advanced/dispatcher.html#autocast" rel="noopener nofollow ugc">autocast section of the dispatch registration tutorial</a>.</p>
<p>autocast(enabled=False) does not affect apex amp. torch.cuda.amp is meant as the permanent and complete replacement, so you should prefer torch.cuda.amp.</p>

hughperkins: <p>Ok. Thank you for the very useful information. Question: what might go wrong if we simply cast everything to the same type (16 vs 32bits) as one of the tensors, but without using an <code>autocast(enabled=False)</code> guard around this?</p>

mcarilli: <aside class="quote no-group" data-username="hughperkins" data-post="3" data-topic="113486">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hughperkins/48/915_2.png" class="avatar"> hughperkins:</div>
<blockquote>
<p>what might go wrong if we simply cast everything to the same type (16 vs 32bits) as one of the tensors, but without using an <code>autocast(enabled=False)</code> guard around this?</p>
</blockquote>
</aside>
<p>If the forward pass of your op consists only of the custom kernel itself, that’s fine. Autocast doesn’t know about your kernel (unless you register it like in the dispatch tutorial) and won’t touch the inputs.</p>
<p>If your op consists of your custom kernel + a few torch.* ops, and you don’t locally <code>autocast(enabled=False)</code>, the torch.* ops still might be affected by autocast, which you may or may not want.</p>
<p>For torch.autograd.Functions, the <a href="https://pytorch.org/docs/stable/notes/amp_examples.html#functions-that-need-a-particular-dtype" rel="noopener nofollow ugc"><code>@custom_fwd(cast_inputs=torch.float32/16)</code></a> takes care of casting inputs and disabling autocast for the forward body.</p>

avi_singhal: <p>Could you explain more how the torch.* ops will be affected by the explicit casting if we dont do it inside a disabled autocast context? Do you mean it can lead to difference in precision in cases where output of the kernel is fp32 and a torch.mm follows it and in this case it will execute in bf16? If this is the case, then its fine, because this is not a misbehavior, its expected. What I want to understand is if there is a hard rule of “no explicit casting” inside autocast enabled context as it can lead to unexplained weird behaviors? If that is not the case, then its just that the user needs to be careful about the usage.</p>