sm90_pipeline_no_cluster.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /******************************************************************************
  2. * Copyright (c) 2024, Jay Shah, Ganesh Bikshandi, Ying Zhang, Vijay Thakkar, Pradeep Ramani, Tri Dao.
  3. ******************************************************************************/
  4. #pragma once
  5. #include<cutlass/pipeline/sm90_pipeline.hpp>
  6. namespace cutlass {
  7. using namespace cute;
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////
  9. // As of Cutlass v3.6.0, if size(ClusterShape) == 1, PipelineTmaAsync has all threads
  10. // signaling the barrier during consumer_release. This causes a perf regression in FA3
  11. // forward pass (especially hdim 128 causal). We instead reimplement the version of
  12. // PipelineTmaAsync before v3.6.0 where only 1 out of 128 threads signals the barrier.
  13. //
  14. // Assumption: params.num_consumers % NumThreadsPerWarpGroup == 0
  15. template <int Stages_, class Base=cutlass::PipelineTmaAsync<Stages_>>
  16. class PipelineTmaAsyncNoCluster: public Base {
  17. public:
  18. using FullBarrier = typename Base::FullBarrier;
  19. using EmptyBarrier = typename Base::EmptyBarrier;
  20. static constexpr uint32_t Stages = Stages_;
  21. using PipelineState = typename Base::PipelineState;
  22. using SharedStorage = typename Base::SharedStorage;
  23. using ThreadCategory = typename Base::ThreadCategory;
  24. using Params = typename Base::Params;
  25. static
  26. CUTLASS_DEVICE
  27. void
  28. init_barriers(SharedStorage& storage, Params params) {
  29. int warp_idx = canonical_warp_idx_sync();
  30. bool is_initializing_warp = (warp_idx == 0);
  31. if (is_initializing_warp) {
  32. // Barrier FULL and EMPTY init
  33. constexpr int producer_arv_cnt = 1;
  34. uint32_t const num_consumer_warpgroups_per_cluster = params.num_consumers / NumThreadsPerWarpGroup;
  35. uint32_t const multicast_consumer_arrival_count = num_consumer_warpgroups_per_cluster;
  36. cutlass::arch::detail::initialize_barrier_array_pair_aligned<decltype(storage.full_barrier_), decltype(storage.empty_barrier_), Stages>(
  37. storage.full_barrier_, storage.empty_barrier_, producer_arv_cnt, multicast_consumer_arrival_count);
  38. }
  39. cutlass::arch::fence_barrier_init();
  40. }
  41. template<class ClusterShape, class InitBarriers, class InitMasks>
  42. CUTLASS_DEVICE
  43. PipelineTmaAsyncNoCluster(SharedStorage& storage, Params params, ClusterShape cluster_shape, InitBarriers = {}, InitMasks = {})
  44. : Base(storage, params, make_shape(_1{}, _1{}, _1{}) /*cluster_shape*/, cute::false_type{} /*init_barriers*/, cute::false_type{} /*init_masks*/)
  45. , empty_barrier_ptr_(&storage.empty_barrier_[0]) {
  46. int warp_idx = canonical_warp_idx_sync();
  47. int lane_predicate = cute::elect_one_sync();
  48. static_assert(cute::is_same_v<InitBarriers, cute::true_type> || cute::is_same_v<InitBarriers, cute::false_type>);
  49. static_assert(cute::is_same_v<InitMasks, cute::true_type> || cute::is_same_v<InitMasks, cute::false_type>);
  50. if constexpr (cute::is_same_v<InitBarriers, cute::true_type>) {
  51. init_barriers(storage, params);
  52. }
  53. }
  54. // Constructor
  55. template<class ClusterShape>
  56. CUTLASS_DEVICE
  57. PipelineTmaAsyncNoCluster(SharedStorage& storage, Params params, ClusterShape cluster_shape)
  58. : PipelineTmaAsyncNoCluster(storage, params, cluster_shape, cute::true_type{}, cute::true_type{}) { }
  59. template<class ClusterShape, class InitBarriers>
  60. CUTLASS_DEVICE
  61. PipelineTmaAsyncNoCluster(SharedStorage& storage, Params params, ClusterShape cluster_shape, InitBarriers = {})
  62. : PipelineTmaAsyncNoCluster(storage, params, cluster_shape, InitBarriers{}, cute::true_type{}) { }
  63. CUTLASS_DEVICE
  64. void consumer_release(PipelineState state) {
  65. consumer_release(state.index());
  66. }
  67. private:
  68. EmptyBarrier* const empty_barrier_ptr_ = nullptr;
  69. // Consumer signalling Producer of completion
  70. // Ensures all blocks in the Same Row and Column get notifed.
  71. CUTLASS_DEVICE
  72. void consumer_release(uint32_t stage, uint32_t skip = false) {
  73. empty_barrier_ptr_[stage].arrive(0 /*dst_blockid_*/, uint32_t(threadIdx.x % cutlass::NumThreadsPerWarpGroup == 0) & (!skip) /*is_signaling_thread*/);
  74. }
  75. };
  76. ////////////////////////////////////////////////////////////////////////////////////////////////////
  77. } // end namespace cutlass