Skip to content

[RFC] Extend CPU AMP to add FP16 support on eager mode #96093

Closed

Description

@CaoE

🚀 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 use torch.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_enabled will 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

cc @mcarilli @ptrblck @leslie-fang-intel @jgong5

Activity

  1. reopened this on Mar 6, 2023
  2. changed the title [-]Expand autocast to support both BF16 and FP16 on CPU[/-] [+][RFC] Expand autocast to support both BF16 and FP16 on CPU[/+] on Mar 6, 2023
  3. self-assigned this
    on Mar 6, 2023
  4. ngimel commented on Mar 7, 2023

    @ngimel
    Collaborator

    Why would we want to add fp16 support to cpu? CPU doesn't provide high-performance libraries operating on fp16 data.

  5. added
    triaged
    on Mar 7, 2023
  6. mingfeima commented on Mar 7, 2023

    @mingfeima
    Collaborator

    Why would we want to add fp16 support to cpu? CPU doesn't provide high-performance libraries operating on fp16 data.

    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).

  7. leslie-fang-intel commented on Mar 7, 2023

    @leslie-fang-intel
    Collaborator

    Copy @ezyang and @ngimel.

  8. CaoE commented on Mar 7, 2023

    @CaoE
    CollaboratorAuthor

    Copy @jgong5

  9. jgong5 commented on Mar 7, 2023

    @jgong5
    Collaborator
  10. jgong5 commented on Mar 7, 2023

    @jgong5
    Collaborator

    @CaoE some comments:

    1. Can you elaborate more about the changes/impact from the frontend API? I suppose bfloat16 would still be the default for CPU? What is the behavior for nested autocast context with mixed dtypes: bfloat16 and half? Suggest to define these semantics clearly.
    2. Can you estimate "how long" would the op list be at the beginning for option 2? We may consider to cover a good portion of ops at least functionality wise before amending the list so that the list for bfloat16 and half almost matches and we don't have to add a lot of special entries for half.
  11. ezyang commented on Mar 7, 2023

    @ezyang
    Contributor

    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?

  12. CaoE commented on Mar 8, 2023

    @CaoE
    CollaboratorAuthor

    Thanks. @jgong5

    1. Bfloat16 would still be the default for CPU and float16 support will be add in the frontend API; For nested autocast context with mixed dtypes: bfloat16 and half, I think we can simplify ban such usage because this may have an unpredictable effect on precision.
    2. To find out "how long" would the op list be at the beginning for option 2. We need to collect common ops which have no fp16 support and are not in the current bf16 op list, mostly, fallthrough ops.

    I will add details and the code example in the description.

  13. CaoE commented on Mar 14, 2023

    @CaoE
    CollaboratorAuthor

    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?

    @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.

  14. changed the title [-][RFC] Expand autocast to support both BF16 and FP16 on CPU[/-] [+][RFC] Expand CPU AMP to support both BF16 and FP16 on eager mode[/+] on Mar 20, 2023
  15. jgong5 commented on Mar 20, 2023

    @jgong5
    Collaborator

    @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.

  16. jgong5 commented on Mar 20, 2023

    @jgong5
    Collaborator

    @ezyang @CaoE has updated the API proposal in the description. Any comments?

  17. CaoE commented on Mar 20, 2023

    @CaoE
    CollaboratorAuthor

    @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

  18. EikanWang commented on Mar 20, 2023

    @EikanWang
    Collaborator

    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?

    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

  19. changed the title [-][RFC] Expand CPU AMP to support both BF16 and FP16 on eager mode[/-] [+][RFC] Extend CPU AMP to add FP16 support on eager mode[/+] on Mar 22, 2023
  20. jgong5 commented on Mar 22, 2023

    @jgong5
    Collaborator

    @CaoE some feedbacks from today's review (thanks @ngimel and @malfet ):

    1. Add to the description how CUDA does in 1) handling nested autocast scopes with hybrid data types; 2) autocast policy for bf16 and fp16. Make the design/behavior consistent between CPU and CUDA.
    2. For autocast policy of fp16, consider to make it same as bf16 just like what CUDA does. A design to support individual lists for fp16 and bf16 introduces unnecessary complexity since fp16 coverage will be improved to be on par with bf16. Initially, when fp16 op coverage is not good enough, can we make the default policy as fall back to fp32 for fp16?
  21. CaoE commented on Mar 23, 2023

    @CaoE
    CollaboratorAuthor

    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.

  22. moved this to In Progress in PyTorch Intelon Oct 19, 2023
  23. moved this from In Progress to TODO in PyTorch Intelon Oct 19, 2023
  24. moved this from TODO to Done in PyTorch Intelon Nov 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

module: amp (automated mixed precision)triaged

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions