sm90_pipeline_no_cluster.hpp 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  40. template<class ClusterShape, class InitBarriers, class InitMasks>
  41. CUTLASS_DEVICE
  42. PipelineTmaAsyncNoCluster(SharedStorage& storage, Params params, ClusterShape cluster_shape, InitBarriers = {}, InitMasks = {})
  43. : Base(storage, params, make_shape(_1{}, _1{}, _1{}) /*cluster_shape*/, cute::false_type{} /*init_barriers*/, cute::false_type{} /*init_masks*/)
  44. , empty_barrier_ptr_(&storage.empty_barrier_[0]) {
  45. int warp_idx = canonical_warp_idx_sync();
  46. int lane_predicate = cute::elect_one_sync();
  47. static_assert(cute::is_same_v<InitBarriers, cute::true_type> || cute::is_same_v<InitBarriers, cute::false_type>);
  48. static_assert(cute::is_same_v<InitMasks, cute::true_type> || cute::is_same_v<InitMasks, cute::false_type>);
  49. if constexpr (cute::is_same_v<InitBarriers, cute::true_type>) {
  50. init_barriers(storage, params);
  51. }
  52. }
  53. // Constructor
  54. template<class ClusterShape>
  55. CUTLASS_DEVICE
  56. PipelineTmaAsyncNoCluster(SharedStorage& storage, Params params, ClusterShape cluster_shape)
  57. : PipelineTmaAsyncNoCluster(storage, params, cluster_shape, cute::true_type{}, cute::true_type{}) { }
  58. template<class ClusterShape, class InitBarriers>
  59. CUTLASS_DEVICE
  60. PipelineTmaAsyncNoCluster(SharedStorage& storage, Params params, ClusterShape cluster_shape, InitBarriers = {})
  61. : PipelineTmaAsyncNoCluster(storage, params, cluster_shape, InitBarriers{}, cute::true_type{}) { }
  62. CUTLASS_DEVICE
  63. void consumer_release(PipelineState state) {
  64. consumer_release(state.index());
  65. }
  66. private:
  67. EmptyBarrier* const empty_barrier_ptr_ = nullptr;
  68. // Consumer signalling Producer of completion
  69. // Ensures all blocks in the Same Row and Column get notifed.
  70. CUTLASS_DEVICE
  71. void consumer_release(uint32_t stage, uint32_t skip = false) {
  72. empty_barrier_ptr_[stage].arrive(0 /*dst_blockid_*/, uint32_t(threadIdx.x % cutlass::NumThreadsPerWarpGroup == 0) & (!skip) /*is_signaling_thread*/);
  73. }
  74. };
  75. ////////////////////////////////////////////////////////////////////////////////////////////////////
  76. } // end namespace cutlass