RAII guard that activates AMP with a target dtype for the duration of the guard's lifetime.
Construction sets the thread-local autocast state to (active=true, dtype=target) and saves the previous values. Destruction restores
the previous values unconditionally, so guards may be nested freely
(an inner ForceFP32 scope inside an outer F16 scope, etc.).
The Python-side lucid.amp.autocast context manager wraps this guard
and is the user-facing entry point — direct C++ use is rare outside
the engine itself.
Notes
Copy and move are deleted: this is a stack-only RAII object, and duplicating it would corrupt the saved/restore state machine.
The Python binding exposes __enter__ / __exit__ semantics
without restoring on exit (the Python autocast class implements
proper RAII in Python); the destructor still runs the restore when
the C++ object is finally collected.
Examples
.. code-block:: cpp {
amp::AutocastGuard guard(Dtype::F16);
// ops inside this scope dispatch under AMP
auto y = matmul(a, b);
} // guard destructor restores previous AMP stateConstructors
1AutocastGuard
void AutocastGuard(Dtype target)Activates AMP with target as the effective compute dtype.
Saves the previous (active, dtype) pair so the destructor can
restore it. Does not validate target — any Dtype is
accepted (the per-op AmpPolicy controls how that dtype is
interpreted at dispatch time).
Parameters
targetDtypeamp::active_dtype while this guard is live. Typical values are Dtype::F16 for GPU half-precision training and Dtype::BF16 for bfloat16 training; Dtype::F32 produces an effectively neutral guard.Destructor
1~AutocastGuard
void ~AutocastGuard()Restores the previous AMP state captured at construction.
Runs unconditionally — required for correct nesting semantics.