# RuntimeError: Expected dtype float16 for half precision but got float32

- **ID:** `pytorch/amp-gradscaler-dtype`
- **Domain:** pytorch
- **Category:** type_error
- **Verification:** ai_generated
- **Fix Rate:** 78%

## Root Cause

A tensor with incorrect dtype (float32 instead of float16) was passed to an operation that expects half-precision input, such as within torch.cuda.amp.autocast with GradScaler.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| torch>=2.0.0 | active | — | — |
| CUDA>=11.8 | active | — | — |

## Workarounds

1. **Explicitly cast inputs to half precision before the operation, or ensure the model uses half precision layers correctly with GradScaler.** (85% success)
   ```
   Explicitly cast inputs to half precision before the operation, or ensure the model uses half precision layers correctly with GradScaler.
   ```
2. **Check for custom operations that do not support autocast and manually cast inputs to float16.** (75% success)
   ```
   Check for custom operations that do not support autocast and manually cast inputs to float16.
   ```

## Dead Ends

- **** — Disabling autocast entirely removes the error but defeats the purpose of mixed precision training. (60% fail)
- **** — Manually converting all tensors to float16 can cause numerical instability and does not fix the root cause of incorrect dtype propagation. (70% fail)
