Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

ps_roi_align_kernel.mm 9.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  1. #include <ATen/mps/MPSProfiler.h>
  2. #include <ATen/native/mps/OperationUtils.h>
  3. #include "mps_helpers.h"
  4. #include "mps_kernels.h"
  5. namespace vision {
  6. namespace ops {
  7. namespace {
  8. std::tuple<at::Tensor, at::Tensor> ps_roi_align_forward_kernel(const at::Tensor& input,
  9. const at::Tensor& rois,
  10. double spatial_scale,
  11. int64_t pooled_height,
  12. int64_t pooled_width,
  13. int64_t sampling_ratio) {
  14. using namespace at::native::mps;
  15. TORCH_CHECK(input.is_mps(), "input must be a MPS tensor");
  16. TORCH_CHECK(rois.is_mps(), "rois must be a MPS tensor");
  17. TORCH_CHECK(rois.size(1) == 5, "rois must have shape as Tensor[K, 5]");
  18. at::TensorArg input_t{input, "input", 1}, rois_t{rois, "rois", 2};
  19. at::CheckedFrom c = "ps_roi_align_forward_kernel";
  20. at::checkAllSameGPU(c, {input_t, rois_t});
  21. at::checkAllSameType(c, {input_t, rois_t});
  22. int64_t num_rois = rois.size(0);
  23. int64_t channels = input.size(1);
  24. int64_t height = input.size(2);
  25. int64_t width = input.size(3);
  26. float spatial_scale_f = static_cast<float>(spatial_scale);
  27. TORCH_CHECK(channels % (pooled_height * pooled_width) == 0,
  28. "input channels must be a multiple of pooling height * pooling width");
  29. int64_t channels_out = channels / (pooled_height * pooled_width);
  30. auto output = at::zeros({num_rois, channels_out, pooled_height, pooled_width}, input.options());
  31. auto channel_mapping = at::zeros(output.sizes(), input.options().dtype(at::kLong));
  32. int64_t output_size = output.numel();
  33. if (output_size == 0) {
  34. return std::make_tuple(output, channel_mapping);
  35. }
  36. auto input_ = input.contiguous();
  37. auto rois_ = rois.contiguous();
  38. id<MTLBuffer> inputBuffer = getMTLBufferStorage(input_);
  39. id<MTLBuffer> roisBuffer = getMTLBufferStorage(rois_);
  40. id<MTLBuffer> outputBuffer = getMTLBufferStorage(output);
  41. id<MTLBuffer> channelMappingBuffer = getMTLBufferStorage(channel_mapping);
  42. id<MTLDevice> device = MPSDevice::getInstance()->device();
  43. MPSStream* mpsStream = getCurrentMPSStream();
  44. dispatch_sync(mpsStream->queue(), ^() {
  45. @autoreleasepool {
  46. id<MTLComputeCommandEncoder> computeEncoder = mpsStream->commandEncoder();
  47. MTLSize threadgroupsPerGrid = MTLSizeMake(
  48. std::min(ceil_div(static_cast<int64_t>(output_size), static_cast<int64_t>(512)), static_cast<int64_t>(4096)),
  49. 1,
  50. 1);
  51. const std::string kernel = "ps_roi_align_" + scalarToMetalTypeString(input.scalar_type());
  52. id<MTLComputePipelineState> visionPSO = mps::visionPipelineState(device, kernel);
  53. // this function call is a no-op if MPS Profiler is not enabled
  54. getMPSProfiler().beginProfileKernel(visionPSO, kernel, {input_, rois_});
  55. [computeEncoder setComputePipelineState:visionPSO];
  56. // [N, C, H, W]
  57. [computeEncoder setBuffer:inputBuffer offset:input_.storage_offset() * input_.element_size() atIndex:0];
  58. [computeEncoder setBuffer:roisBuffer offset:rois_.storage_offset() * rois_.element_size() atIndex:1];
  59. [computeEncoder setBuffer:outputBuffer offset:output.storage_offset() * output.element_size() atIndex:2];
  60. [computeEncoder setBuffer:channelMappingBuffer
  61. offset:channel_mapping.storage_offset() * channel_mapping.element_size()
  62. atIndex:3];
  63. [computeEncoder setBytes:&output_size length:sizeof(int64_t) atIndex:4];
  64. [computeEncoder setBytes:&channels length:sizeof(int64_t) atIndex:5];
  65. [computeEncoder setBytes:&height length:sizeof(int64_t) atIndex:6];
  66. [computeEncoder setBytes:&width length:sizeof(int64_t) atIndex:7];
  67. [computeEncoder setBytes:&pooled_height length:sizeof(int64_t) atIndex:8];
  68. [computeEncoder setBytes:&pooled_width length:sizeof(int64_t) atIndex:9];
  69. [computeEncoder setBytes:&sampling_ratio length:sizeof(int64_t) atIndex:10];
  70. [computeEncoder setBytes:&channels_out length:sizeof(int64_t) atIndex:11];
  71. [computeEncoder setBytes:&spatial_scale_f length:sizeof(float) atIndex:12];
  72. // A threadGroup is equivalent to a cuda's block.
  73. NSUInteger tgSize = visionPSO.maxTotalThreadsPerThreadgroup;
  74. if (tgSize > threadsPerBlock) {
  75. tgSize = threadsPerBlock;
  76. }
  77. MTLSize threadGroupSize = MTLSizeMake(tgSize, 1, 1);
  78. [computeEncoder dispatchThreadgroups:threadgroupsPerGrid threadsPerThreadgroup:threadGroupSize];
  79. getMPSProfiler().endProfileKernel(visionPSO);
  80. }
  81. });
  82. return std::make_tuple(output, channel_mapping);
  83. }
  84. at::Tensor ps_roi_align_backward_kernel(const at::Tensor& grad,
  85. const at::Tensor& rois,
  86. const at::Tensor& channel_mapping,
  87. double spatial_scale,
  88. int64_t pooled_height,
  89. int64_t pooled_width,
  90. int64_t sampling_ratio,
  91. int64_t batch_size,
  92. int64_t channels,
  93. int64_t height,
  94. int64_t width) {
  95. using namespace at::native::mps;
  96. TORCH_CHECK(grad.is_mps(), "grad must be a MPS tensor");
  97. TORCH_CHECK(rois.is_mps(), "rois must be a MPS tensor");
  98. TORCH_CHECK(grad.scalar_type() != at::kHalf, "MPS does not support ps_roi_align backward with float16 inputs.");
  99. TORCH_CHECK(channel_mapping.is_mps(), "channel_mapping must be a MPS tensor");
  100. at::TensorArg grad_t{grad, "input", 1}, rois_t{rois, "rois", 2},
  101. channel_mapping_t{channel_mapping, "channel_mapping", 3};
  102. at::CheckedFrom c = "ps_roi_align_backward_kernel";
  103. at::checkAllSameGPU(c, {grad_t, rois_t, channel_mapping_t});
  104. at::checkAllSameType(c, {grad_t, rois_t});
  105. float spatial_scale_f = static_cast<float>(spatial_scale);
  106. auto grad_input = at::zeros({batch_size, channels, height, width}, grad.options());
  107. if (grad.numel() == 0) {
  108. return grad_input;
  109. }
  110. int64_t output_size = grad.numel();
  111. int64_t channels_out = channels / (pooled_height * pooled_width);
  112. at::globalContext().alertNotDeterministic("ps_roi_align_backward_kernel");
  113. auto grad_ = grad.contiguous(), rois_ = rois.contiguous();
  114. id<MTLBuffer> inputBuffer = getMTLBufferStorage(grad_);
  115. id<MTLBuffer> roisBuffer = getMTLBufferStorage(rois_);
  116. id<MTLBuffer> channelMappingBuffer = getMTLBufferStorage(channel_mapping);
  117. id<MTLBuffer> outputBuffer = getMTLBufferStorage(grad_input);
  118. id<MTLDevice> device = MPSDevice::getInstance()->device();
  119. MPSStream* mpsStream = getCurrentMPSStream();
  120. dispatch_sync(mpsStream->queue(), ^() {
  121. @autoreleasepool {
  122. id<MTLComputeCommandEncoder> computeEncoder = mpsStream->commandEncoder();
  123. MTLSize threadgroupsPerGrid = MTLSizeMake(
  124. std::min(ceil_div(static_cast<int64_t>(grad.numel()), static_cast<int64_t>(512)), static_cast<int64_t>(4096)),
  125. 1,
  126. 1);
  127. const std::string kernel = "ps_roi_align_backward_" + scalarToMetalTypeString(grad.scalar_type());
  128. id<MTLComputePipelineState> visionPSO = mps::visionPipelineState(device, kernel);
  129. // this function call is a no-op if MPS Profiler is not enabled
  130. getMPSProfiler().beginProfileKernel(visionPSO, kernel, {grad, rois_});
  131. [computeEncoder setComputePipelineState:visionPSO];
  132. // [N, C, H, W]
  133. [computeEncoder setBuffer:inputBuffer offset:grad_.storage_offset() * grad_.element_size() atIndex:0];
  134. [computeEncoder setBuffer:roisBuffer offset:rois_.storage_offset() * rois_.element_size() atIndex:1];
  135. [computeEncoder setBuffer:channelMappingBuffer
  136. offset:channel_mapping.storage_offset() * channel_mapping.element_size()
  137. atIndex:2];
  138. [computeEncoder setBuffer:outputBuffer offset:grad_input.storage_offset() * grad_input.element_size() atIndex:3];
  139. [computeEncoder setBytes:&output_size length:sizeof(int64_t) atIndex:4];
  140. [computeEncoder setBytes:&channels length:sizeof(int64_t) atIndex:5];
  141. [computeEncoder setBytes:&height length:sizeof(int64_t) atIndex:6];
  142. [computeEncoder setBytes:&width length:sizeof(int64_t) atIndex:7];
  143. [computeEncoder setBytes:&pooled_height length:sizeof(int64_t) atIndex:8];
  144. [computeEncoder setBytes:&pooled_width length:sizeof(int64_t) atIndex:9];
  145. [computeEncoder setBytes:&sampling_ratio length:sizeof(int64_t) atIndex:10];
  146. [computeEncoder setBytes:&channels_out length:sizeof(int64_t) atIndex:11];
  147. [computeEncoder setBytes:&spatial_scale_f length:sizeof(float) atIndex:12];
  148. // A threadGroup is equivalent to a cuda's block.
  149. NSUInteger tgSize = visionPSO.maxTotalThreadsPerThreadgroup;
  150. if (tgSize > threadsPerBlock) {
  151. tgSize = threadsPerBlock;
  152. }
  153. MTLSize threadGroupSize = MTLSizeMake(tgSize, 1, 1);
  154. [computeEncoder dispatchThreadgroups:threadgroupsPerGrid threadsPerThreadgroup:threadGroupSize];
  155. getMPSProfiler().endProfileKernel(visionPSO);
  156. }
  157. });
  158. return grad_input;
  159. }
  160. } // namespace
  161. TORCH_LIBRARY_IMPL(torchvision, MPS, m) {
  162. m.impl(TORCH_SELECTIVE_NAME("torchvision::ps_roi_align"), TORCH_FN(ps_roi_align_forward_kernel));
  163. m.impl(TORCH_SELECTIVE_NAME("torchvision::_ps_roi_align_backward"), TORCH_FN(ps_roi_align_backward_kernel));
  164. }
  165. } // namespace ops
  166. } // namespace vision
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...