utils.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // DeepSpeed note, code taken & adapted from commit 9aa94789f13ada713af36cfd8cca2fc9a7f6b79a
  2. // https://github.com/ptillet/torch-blocksparse/blob/master/csrc/utils.cpp
  3. #include <torch/extension.h>
  4. #include <string>
  5. #include <tuple>
  6. #include <vector>
  7. #ifdef _OPENMP
  8. #include <omp.h>
  9. #endif
  10. typedef std::vector<std::tuple<int, torch::Tensor>> ret_t;
  11. void segment_blocks(torch::Tensor layout,
  12. torch::Tensor idx,
  13. torch::Tensor scratch,
  14. int max_width,
  15. ret_t& ret)
  16. {
  17. size_t H = layout.size(0);
  18. size_t M = layout.size(1);
  19. size_t N = layout.size(2);
  20. torch::Tensor tmp = torch::zeros_like(layout);
  21. auto _tmp = tmp.accessor<int, 3>();
  22. auto _layout = layout.accessor<int, 3>();
  23. auto _idx = idx.accessor<int, 3>();
  24. auto _scratch = scratch.accessor<int, 3>();
  25. std::vector<int> current(H, 0);
  26. #ifdef _OPENMP
  27. #pragma omp parallel for
  28. #endif
  29. for (size_t h = 0; h < H; h++) {
  30. // surrounding indices
  31. std::vector<int> ii_left(max_width, -1);
  32. std::vector<std::vector<int>> ii_top(max_width, std::vector<int>(N, -1));
  33. for (size_t m = 0; m < M; m++) {
  34. for (size_t n = 0; n < N; n++) {
  35. int v = _layout[h][m][n];
  36. if (v == 0) continue;
  37. int n_left = ii_left[max_width - 1];
  38. int m_top = ii_top[max_width - 1][n];
  39. int top = (m_top >= 0) ? _tmp[h][m_top][n] : 0;
  40. int left = (n_left >= 0) ? _tmp[h][m][n_left] : 0;
  41. int topleft = (m_top >= 0 && n_left >= 0) ? _tmp[h][m_top][n_left] : 0;
  42. int width = std::min(left, std::min(top, topleft)) + 1;
  43. // reset width if blocks cannot be
  44. // packed together (i.e., there's a 1 "in the middle")
  45. for (int nn = n_left + 1; nn < n; nn++)
  46. if (ii_top[max_width - 1][nn] > ii_top[max_width - 1][n]) width = 1;
  47. _tmp[h][m][n] = width;
  48. // update n_left ring buffer
  49. for (int k = 0; k < max_width - 1; k++) ii_left[k] = ii_left[k + 1];
  50. ii_left[max_width - 1] = n;
  51. // update ii_top ring buffer
  52. for (int k = 0; k < max_width - 1; k++) ii_top[k][n] = ii_top[k + 1][n];
  53. ii_top[max_width - 1][n] = m;
  54. // block is too small -- skip
  55. if (width != max_width) continue;
  56. // retained blocks are set to zeros
  57. for (size_t km = 0; km < max_width; km++)
  58. for (size_t kn = 0; kn < max_width; kn++) {
  59. int mm = ii_top[km][n];
  60. int nn = ii_left[kn];
  61. if (mm < 0 || nn < 0) continue;
  62. _layout[h][mm][nn] = 0;
  63. _tmp[h][mm][nn] = 0;
  64. _scratch[h][current[h]][0] = (int)h;
  65. _scratch[h][current[h]][1] = (int)mm;
  66. _scratch[h][current[h]][2] = (int)nn;
  67. _scratch[h][current[h]][3] = _idx[h][mm][nn];
  68. current[h]++;
  69. }
  70. }
  71. }
  72. }
  73. std::vector<torch::Tensor> to_cat;
  74. for (size_t h = 0; h < H; h++)
  75. if (current[h] > 0) to_cat.push_back(scratch[h].slice(0, 0, current[h]));
  76. if (!to_cat.empty()) ret.push_back({max_width, torch::cat(to_cat)});
  77. }
  78. ret_t sdd_segment(torch::Tensor layout, int start_width)
  79. {
  80. ret_t ret;
  81. // block index
  82. torch::Tensor idx = torch::zeros_like(layout);
  83. int current = 0;
  84. size_t H = layout.size(0);
  85. size_t M = layout.size(1);
  86. size_t N = layout.size(2);
  87. auto _layout = layout.accessor<int, 3>();
  88. auto _idx = idx.accessor<int, 3>();
  89. for (size_t h = 0; h < H; h++)
  90. for (size_t m = 0; m < M; m++)
  91. for (size_t n = 0; n < N; n++) {
  92. if (_layout[h][m][n] == 0) continue;
  93. _idx[h][m][n] = current++;
  94. }
  95. // scratch memory
  96. torch::Tensor scratch = torch::empty({H, layout.sum().item<int>(), 4}, layout.dtype());
  97. for (int max_width = start_width; max_width > 0; max_width /= 2)
  98. segment_blocks(layout, idx, scratch, max_width, ret);
  99. return ret;
  100. }
  101. PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)
  102. {
  103. m.def("sdd_segment", &sdd_segment, "SDD segmentation handler");
  104. }