CPUFunctions.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #include <ATen/core/TensorBody.h>
  3. // TODO Undo all logic introduced for Note [Avoiding Include Cycles In Static Dispatch]
  4. // Code introduced to avoid cyclic dependency in static dispatch is no longer
  5. // needed as static dispatch logic is moved from TensorBody.h, which caused cycles in the first place,
  6. // to Operators.cpp for supporting multiple backends with multiple kernels.
  7. //
  8. // Note [Avoiding Include Cycles In Static Dispatch]
  9. // In order to avoid #include cycles in the static dispatch build, we've carefully split out
  10. // the static function definition files into {DispatchKey}Functions.h and {DispatchKey}Functions_inl.h.
  11. //
  12. // Without this split, the include cycle looks like TensorBody.h -> CPUFunctions.h -> TensorBody.h.
  13. // - TensorBody.h #includes CPUFunctions.h in the static dispatch build, because the tensor methods
  14. // all need to call into the fastpath C++ API defined in CPUFunctions.h. The methods are also all
  15. // directly inlined into TensorBody.h.
  16. // - CPUFunctions.h #includes TensorBody.h because it contains function declarations for the entire C++ API,
  17. // which include functions that have defaultable std::optional<Tensor> arguments.
  18. // That requires knowing the full Tensor class definition.
  19. //
  20. // We break the cycle by doing the following:
  21. // - Split out CPUFunction.h into two files: CPUFunctions.h and CPUFunctions_inl.h
  22. // - CPUFunction.h is a dummy file that just includes the Tensor class and includes CPUFunctions_inl.,
  23. // - CPUFunctions_inl.h includes everything else
  24. // - (only in the static dispatch build) TensorBody.h makes sure to finish defining the Tensor class,
  25. // and then it includes CPUFunctions_inl.h.
  26. // - All other files that want the cpu fastpath functions can include CPUFunctions.h directly.
  27. // - This also means that static dispatch build, CPUFunctions.h only needs to
  28. // #include TensorBody.h, and it will automatically bring in CPUFunctions_inl.h.
  29. #include <ATen/CPUFunctions_inl.h>
  30. #else
  31. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  32. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)