Repository navigation
Description
🚀 The feature, motivation and pitch
Currently low precision of PyTorch CPU AMP on eager mode is BF16 and it does not support FP16 i.e. torch.half. If we want to add FP16 support on CPU #97068, we need also to extend PyTorch CPU AMP.
This RFC is proposed to extend PyTorch CPU AMP to support both BF16 and FP16 on eager mode so that users can use torch.cpu.amp.autocast to set the lower precision data type to torch.half/torch.bfloat16 and autocast will automatically cast the inputs to float, lower precision data type i.e. FP16/BF16 or make inputs fall through.
Proposed Implementation:
Extend autocast to support setting lower precision data type to FP16 on CPU.
-
Extend autocast init method on CPU:
Usage of
torch.autocast:
Before:torch.autocast()only supports setting dtype to torch.bfloat16 on CPU. if users usetorch.autocast(device_type="cpu", dtype=torch.half), autocast will be disabled with the warning: "In CPU autocast, but the target dtype is not supported. Disabling autocast".After: users can set dtype=torch.bfloat16 or dtype=torch.half.:
with torch.autocast(device_type="cpu", dtype=torch.bfloat16): ... with torch.autocast(device_type="cpu", dtype=torch.half): ...
-
Nest behavior of autocast on CPU:
Nest AMP with mixed torch.bfloat16 and torch.half is not defined yet in PyTorch. For example:
with torch.autocast(device_type="cpu", dtype=torch.bfloat16): ... with torch.autocast(device_type="cpu", dtype=torch.half):: ...
Both BF16 and FP16 are allowed on CUDA, and it also does not handle such usage specially. In addition, in such case
cache_enabledwill be also enabled by default, and the dtype of cached tensor (e.g., BF16) may be different with the target data type (e.g., FP16 set by nested amp). Ops may report errors with inconsistent data types.Nesting AMP with mixed torch.bfloat16 and torch.half seems unreasonable. To simplify things we can disable such mixed usage or give warning and document it.
Test
- Add test cases for FP16 autocast on CPU.
Additional context
This RFC depends on FP16 supports of operators #97068
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
[-]Expand autocast to support both BF16 and FP16 on CPU[/-][+][RFC] Expand autocast to support both BF16 and FP16 on CPU[/+]ngimel commentedon Mar 7, 2023
Why would we want to add fp16 support to cpu? CPU doesn't provide high-performance libraries operating on fp16 data.
mingfeima commentedon Mar 7, 2023
we are optimizing fp16 on CPU, the next gen of Xeon Granite Rapids (GNR) is going to provide AMX fp16 instructions and oneDNN has done its part on fp16. So for conv and gemm, we rely on onednn to get high performance and for other Ops covert back to fp32 for computation (use vectorized cvt instructions
_mm256_cvtph_ps/_mm256_cvtps_ph) which is similar to bfloat16 optimizations.The job has just started and I am preparing the feature request doc right now, some related work can be found at #96076 (still have a bunch of CI fails to clear).
leslie-fang-intel commentedon Mar 7, 2023
Copy @ezyang and @ngimel.
CaoE commentedon Mar 7, 2023
Copy @jgong5
jgong5 commentedon Mar 7, 2023
FYI, we implemented option 2 in IPEX. Here is the code for reference:
https://github.com/intel/intel-extension-for-pytorch/blob/master/csrc/cpu/autocast/autocast_mode.cpp
jgong5 commentedon Mar 7, 2023
@CaoE some comments:
ezyang commentedon Mar 7, 2023
I'm amenable to adding fp16 autocast support on cpu. However, we need an API proposal. Also, maybe we can use cpu inductor to get coverage fast and easy to start?
CaoE commentedon Mar 8, 2023
Thanks. @jgong5
I will add details and the code example in the description.
CaoE commentedon Mar 14, 2023
@ezyang Sorry for the delay. I'm occupied by other tasks recently. I will add an API proposal. Using cpu inductor to get coverage is fast and easy to start and we will also add the inductor fp16 support to the plan.
[-][RFC] Expand autocast to support both BF16 and FP16 on CPU[/-][+][RFC] Expand CPU AMP to support both BF16 and FP16 on eager mode[/+]jgong5 commentedon Mar 20, 2023
@CaoE for handling nested autocast with mixed half and bfloat16 data types, do you have preference on the options? I guess disabling such a usage is simpler and make sense as well.
jgong5 commentedon Mar 20, 2023
@ezyang @CaoE has updated the API proposal in the description. Any comments?
CaoE commentedon Mar 20, 2023
@jgong5 I prefer option2 for nested autocast with mixed half and bfloat16 data types since it can avoid potential precision problems, is simpler, and can also give clear messages to users
EikanWang commentedon Mar 20, 2023
Regarding Inductor FP16 support for the C++/OpenMP backend, the work is WIP in parallel. We just finished the BF16 support and optimized it by leveraging
at::vectorization. I will keep you informed. @ezyang[-][RFC] Expand CPU AMP to support both BF16 and FP16 on eager mode[/-][+][RFC] Extend CPU AMP to add FP16 support on eager mode[/+]jgong5 commentedon Mar 22, 2023
@CaoE some feedbacks from today's review (thanks @ngimel and @malfet ):
CaoE commentedon Mar 23, 2023
Thanks @jgong5. Description have been updated. For autocast policy list, 1) we may add fallback list for fp16 if we want to run fp16 with amp at early stage. 2) Do not make changes. Add PF16 support for ops first, and fp16 support can be added for CPU AMP after common ops have FP16 support.
Add Half support for CPU autocast on eager mode (#112484)