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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
#include "kernel/kernel.h" #include "ops/sample_distorted_bounding_box_v2.h" #include "plugin/device/gpu/hal/device/gpu_common.h" #include "plugin/device/gpu/kernel/other/sample_distorted_bounding_box_v2_gpu_kernel.h" #include "targets/x86_64-linux/include/cuda_runtime_api.h" #include "targets/x86_64-linux/include/curand_kernel.h" #include "targets/x86_64-linux/include/driver_types.h" #include "plugin/device/gpu/kernel/cuda_impl/cuda_ops/sample_distorted_bounding_box_v2_impl.cuh"
namespace mindspore { namespace kernel { namespace { using KernelRunFunc = SampleDistortedBoundingBoxV2GpuKernelMod::KernelRunFunc; #define ADD_KERNEL(image_size_dtype, kernel_type) \ { \ KernelAttr() \ .AddInputAttr(image_size_dtype) \ .AddInputAttr(kNumberTypeFloat32) \ .AddInputAttr(kNumberTypeFloat32) \ .AddOutputAttr(image_size_dtype) \ .AddOutputAttr(image_size_dtype) \ .AddOutputAttr(kNumberTypeFloat32), \ &SampleDistortedBoundingBoxV2GpuKernelMod::LaunchKernel<kernel_type> \ }
constexpr size_t kOutputSize = 3; constexpr size_t kInputSize = 3; constexpr size_t kIndex0 = 0; constexpr size_t kIndex1 = 1; constexpr size_t kIndex2 = 2; constexpr size_t kIndex3 = 3; constexpr size_t kBBoxesDimension = 3; constexpr size_t kShapeSize1 = 1; constexpr size_t kShapeSize2 = 2; constexpr size_t kShapeSize3 = 3; constexpr size_t kShapeSize4 = 4; constexpr size_t kNumber0 = 0; constexpr float kFloatNum0 = 0.0; constexpr float kFloatNum1 = 1.0; }
bool SampleDistortedBoundingBoxV2GpuKernelMod::Init(const BaseOperatorPtr &base_operator, const std::vector<KernelTensorPtr> &inputs, const std::vector<KernelTensorPtr> &outputs) { MS_EXCEPTION_IF_NULL(base_operator); kernel_name_ = base_operator->name();
if (!MatchKernelFunc(base_operator, inputs, outputs)) { return false; }
auto kernel_attr = GetKernelAttrFromTensors(inputs, outputs); dtype_ = kernel_attr.GetInputAttr(kIndex0).first; unit_dtype_size_ = abstract::TypeIdSize(dtype_);
auto kernel_ptr = std::make_shared<ops::SampleDistortedBoundingBoxV2>(base_operator->GetPrim()); seed_ = static_cast<int64_t>(GetValue<int64_t>(kernel_ptr->GetAttr("seed"))); seed2_ = static_cast<int64_t>(GetValue<int64_t>(kernel_ptr->GetAttr("seed2"))); aspect_ratio_range_ = GetValue<std::vector<float>>(kernel_ptr->GetAttr("aspect_ratio_range")); area_range_ = GetValue<std::vector<float>>(kernel_ptr->GetAttr("area_range")); max_attempts_ = static_cast<int64_t>(GetValue<int64_t>(kernel_ptr->GetAttr("max_attempts"))); use_image_if_no_bounding_boxes_ = static_cast<bool>(GetValue<bool>(kernel_ptr->GetAttr("use_image_if_no_bounding_boxes")));
std::vector<int64_t> shape_image_size = std::vector<int64_t>(inputs.at(kIndex0)->GetDeviceShapeAdaptively().begin(), inputs.at(kIndex0)->GetDeviceShapeAdaptively().end()); std::vector<int64_t> shape_bounding_boxes = std::vector<int64_t>(inputs.at(kIndex1)->GetDeviceShapeAdaptively().begin(), inputs.at(kIndex1)->GetDeviceShapeAdaptively().end()); size_t shape_dim_image_size = shape_image_size.size(); size_t shape_dim_bounding_boxes = shape_bounding_boxes.size(); if (shape_dim_image_size != kShapeSize1) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', image_size must be 1-dimensional, got: [" << shape_dim_image_size << "]."; } if (shape_image_size[kIndex0] != kShapeSize3) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', image_size must contain 3 elements, got: [" << shape_image_size[kIndex0] << "]."; } if (shape_dim_bounding_boxes != kBBoxesDimension) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', bounding_boxes must be 3-dimensional" << " [batch, num_boxes, coords], got: [" << shape_dim_bounding_boxes << "]."; } if (shape_bounding_boxes[shape_dim_bounding_boxes - 1] != kShapeSize4) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', bounding_boxes must have shape [4], got: [" << shape_bounding_boxes[shape_dim_bounding_boxes - 1] << "]."; }
if (max_attempts_ <= SizeToLong(kNumber0)) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', max_attempts must be positive: [" << max_attempts_ << "]."; } if (aspect_ratio_range_[kIndex1] <= kFloatNum0 || aspect_ratio_range_[kIndex0] <= kFloatNum0) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', aspect_ratio_range must be positive: [" << aspect_ratio_range_[kIndex0] << "], [" << aspect_ratio_range_[kIndex1] << "]."; } if (area_range_[kIndex1] <= kFloatNum0 || area_range_[kIndex0] <= kFloatNum0) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', area_range must be positive: [" << area_range_[kIndex0] << "], [" << area_range_[kIndex1] << "]."; } if (area_range_[kIndex1] > kFloatNum1 || area_range_[kIndex0] > kFloatNum1) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', area_range must be less then or equal to 1.0: [" << area_range_[kIndex0] << "], [" << area_range_[kIndex1] << "]."; } if (aspect_ratio_range_.size() != kShapeSize2) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', aspect_ratio_range field must specify 2 dimensions."; } if (area_range_.size() != kShapeSize2) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', area_range field must specify 2 dimensions."; } return true; }
int SampleDistortedBoundingBoxV2GpuKernelMod::Resize(const BaseOperatorPtr &base_operator, const std::vector<KernelTensorPtr> &inputs, const std::vector<KernelTensorPtr> &outputs, const std::map<uint32_t, tensor::TensorPtr> &) { for (auto input : inputs) { auto input_shape = input->GetShapeVector(); if (!IsValidShape(input_shape)) { return KRET_UNKNOWN_SHAPE; } } ResetResource(); std::vector<int64_t> shape_bounding_boxes = std::vector<int64_t>(inputs.at(kIndex1)->GetDeviceShapeAdaptively().begin(), inputs.at(kIndex1)->GetDeviceShapeAdaptively().end()); bounding_boxes_elements_ = std::accumulate(shape_bounding_boxes.begin(), shape_bounding_boxes.end(), 1, std::multiplies<int64_t>()); input_size_list_.emplace_back(kShapeSize3 * unit_dtype_size_); input_size_list_.emplace_back(bounding_boxes_elements_ * abstract::TypeIdSize(kNumberTypeFloat32)); input_size_list_.emplace_back(kShapeSize1 * abstract::TypeIdSize(kNumberTypeFloat32)); output_size_list_.emplace_back(kShapeSize3 * unit_dtype_size_); output_size_list_.emplace_back(kShapeSize3 * unit_dtype_size_); output_size_list_.emplace_back(kShapeSize1 * kShapeSize1 * kShapeSize4 * abstract::TypeIdSize(kNumberTypeFloat32)); workspace_size_list_.push_back(kShapeSize1 * kShapeSize1 * kShapeSize4 * max_attempts_ * sizeof(int32_t)); workspace_size_list_.push_back(kShapeSize1 * kShapeSize1 * kShapeSize4 * max_attempts_ * sizeof(curandState)); return KRET_OK; }
bool SampleDistortedBoundingBoxV2GpuKernelMod::SatisfiesOverlapConstraints(const Region &crop, float minimum_object_covered, const std::vector<Region> &bounding_boxes) { const float kMinArea = 1.0; if (crop.Area() < kMinArea) { return false; }
bool is_object_covered = false; for (const auto &bbox : bounding_boxes) { const float object_area = bbox.Area(); if (object_area < kMinArea) { continue; }
const float object_covered = object_area != 0 ? crop.Intersect(bbox).Area() / object_area : 0; if (object_covered >= minimum_object_covered) { is_object_covered = true; break; } } return is_object_covered; }
template <typename T> void SampleDistortedBoundingBoxV2GpuKernelMod::SelectBox(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace, const std::vector<AddressPtr> &outputs) { cudaStream_t stream = reinterpret_cast<cudaStream_t>(cuda_stream_);
T *image_size_addr = GetDeviceAddress<T>(inputs, kIndex0); T image_size[kShapeSize3]; CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE( cudaMemcpyAsync(&image_size, image_size_addr, kShapeSize3 * unit_dtype_size_, cudaMemcpyDeviceToHost, stream), "cudaMemcpy failed."); float *bounding_boxes_addr = GetDeviceAddress<float>(inputs, kIndex1); float bounding_boxes[bounding_boxes_elements_]; CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE( cudaMemcpyAsync(&bounding_boxes, bounding_boxes_addr, bounding_boxes_elements_ * abstract::TypeIdSize(kNumberTypeFloat32), cudaMemcpyDeviceToHost, stream), "cudaMemcpy failed."); float *min_object_covered_addr = GetDeviceAddress<float>(inputs, kIndex2); float min_object_covered[kShapeSize1]; CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE( cudaMemcpyAsync(&min_object_covered, min_object_covered_addr, kShapeSize1 * abstract::TypeIdSize(kNumberTypeFloat32), cudaMemcpyDeviceToHost, stream), "cudaMemcpy failed."); int32_t *workspace_ptr = GetDeviceAddress<int32_t>(workspace, kIndex0); size_t boxes_elements_count = kShapeSize1 * kShapeSize1 * kShapeSize4 * max_attempts_; int32_t generated_boxes[boxes_elements_count]; CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE( cudaMemcpyAsync(&generated_boxes, workspace_ptr, boxes_elements_count * sizeof(int32_t), cudaMemcpyDeviceToHost, stream), "cudaMemcpy failed."); T *begin_addr = GetDeviceAddress<T>(outputs, kIndex0); T begin[kShapeSize3]; CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE( cudaMemcpyAsync(&begin, begin_addr, kShapeSize3 * unit_dtype_size_, cudaMemcpyDeviceToHost, stream), "cudaMemcpy failed."); T *size_addr = GetDeviceAddress<T>(outputs, kIndex1); T size[kShapeSize3]; CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE( cudaMemcpyAsync(&size, size_addr, kShapeSize3 * unit_dtype_size_, cudaMemcpyDeviceToHost, stream), "cudaMemcpy failed."); float *bboxes_addr = GetDeviceAddress<float>(outputs, kIndex2); size_t bboxes_elements_count = kShapeSize1 * kShapeSize1 * kShapeSize4; float bboxes[bboxes_elements_count]; CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE( cudaMemcpyAsync(&bboxes, bboxes_addr, bboxes_elements_count * sizeof(float), cudaMemcpyDeviceToHost, stream), "cudaMemcpy failed.");
const int32_t height = static_cast<int32_t>(image_size[kIndex0]); const int32_t width = static_cast<int32_t>(image_size[kIndex1]); if (!(height > 0 && width > 0)) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', image height and width must be positive, got: [" << height << "] and [" << width << "]."; }
float min_object_covered_val = 0.0; min_object_covered_val = *min_object_covered; if (min_object_covered_val < 0.0 || min_object_covered_val > 1.0) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', min_object_covered must be in [0.0, 1.0], got: [" << min_object_covered_val << "]."; }
std::vector<Region> boxes; size_t size_bounding_boxes = inputs[kIndex1]->size / sizeof(float); for (size_t b = 0; b < size_bounding_boxes / kShapeSize4; ++b) { for (size_t i = 0; i < kShapeSize4; ++i) { if (bounding_boxes[b * kShapeSize4 + i] < 0.0 || bounding_boxes[b * kShapeSize4 + i] > 1.0) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', all bounding box coordinates must in [0.0, 1.0], got: [" << bounding_boxes[b * kShapeSize4 + i] << "]."; } } if (!(bounding_boxes[b * kShapeSize4 + kIndex1] < bounding_boxes[b * kShapeSize4 + kIndex3])) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', x_min of bounding box must be less than x_max, got: [" << bounding_boxes[b * kShapeSize4 + kIndex1] << "] and [" << bounding_boxes[b * kShapeSize4 + kIndex3] << "]."; } if (!(bounding_boxes[b * kShapeSize4 + kIndex0] < bounding_boxes[b * kShapeSize4 + kIndex2])) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', y_min of bounding box must be less than y_max, got: [" << bounding_boxes[b * kShapeSize4 + kIndex0] << "] and [" << bounding_boxes[b * kShapeSize4 + kIndex2] << "]."; } const int32_t x_min = static_cast<int32_t>(bounding_boxes[b * kShapeSize4 + 1] * width ); const int32_t y_min = static_cast<int32_t>(bounding_boxes[b * kShapeSize4 + 0] * height ); const int32_t x_max = static_cast<int32_t>(bounding_boxes[b * kShapeSize4 + 3] * width ); const int32_t y_max = static_cast<int32_t>(bounding_boxes[b * kShapeSize4 + 2] * height); boxes.push_back(Region(x_min, y_min, x_max, y_max)); }
const Region ms_image_rect(0, 0, width, height); if (boxes.empty()) { if (!use_image_if_no_bounding_boxes_) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', no bounding boxes provided as input. One must enable use_image_if_no_bounding_boxes " "if you wish to not provide any bounding boxes."; }
boxes.push_back(ms_image_rect); }
Region ms_crop_rect; bool ms_sample_generated = false; for (size_t i = 0; i < LongToSize(max_attempts_); ++i) { const int32_t x_min = generated_boxes[i * kShapeSize4 + 0] ; const int32_t y_min = generated_boxes[i * kShapeSize4 + 1]; const int32_t x_max = generated_boxes[i * kShapeSize4 + 2]; const int32_t y_max = generated_boxes[i * kShapeSize4 + 3]; ms_crop_rect.SetPoint(x_min, y_min, x_max, y_max); if (SatisfiesOverlapConstraints(ms_crop_rect, min_object_covered_val, boxes)) { ms_sample_generated = true; break; } }
if (!ms_sample_generated) { ms_crop_rect = ms_image_rect; }
const int target_width = ms_crop_rect.max_x_ - ms_crop_rect.min_x_; const int target_height = ms_crop_rect.max_y_ - ms_crop_rect.min_y_; const int offset_width = ms_crop_rect.min_x_; const int offset_height = ms_crop_rect.min_y_;
if (width < target_width + offset_width) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', width must be >= target_width + offset_width: [" << width << "] vs [" << target_width << "] + [" << offset_width << "]"; }
if (height < target_height + offset_height) { MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', height must be >= target_height + offset_height: [" << height << "] vs [" << target_height << "] + [" << offset_height << "]"; }
begin[kIndex0] = static_cast<T>(offset_height); size[kIndex0] = static_cast<T>(target_height); begin[kIndex1] = static_cast<T>(offset_width); size[kIndex1] = static_cast<T>(target_width);
bboxes[kIndex0] = static_cast<float>(ms_crop_rect.min_y_) / static_cast<float>(height); bboxes[kIndex1] = static_cast<float>(ms_crop_rect.min_x_) / static_cast<float>(width); bboxes[kIndex2] = static_cast<float>(ms_crop_rect.max_y_) / static_cast<float>(height); bboxes[kIndex3] = static_cast<float>(ms_crop_rect.max_x_) / static_cast<float>(width);
begin[kIndex2] = static_cast<T>(0); size[kIndex2] = static_cast<T>(-1);
CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE( cudaMemcpyAsync(begin_addr, &begin, kShapeSize3 * unit_dtype_size_, cudaMemcpyHostToDevice, stream), "cudaMemcpy failed."); CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE( cudaMemcpyAsync(size_addr, &size, kShapeSize3 * unit_dtype_size_, cudaMemcpyHostToDevice, stream), "cudaMemcpy failed."); CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE( cudaMemcpyAsync(bboxes_addr, &bboxes, boxes_elements_count * sizeof(float), cudaMemcpyHostToDevice, stream), "cudaMemcpy failed."); return; }
template <typename T> bool SampleDistortedBoundingBoxV2GpuKernelMod::LaunchKernel(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace, const std::vector<AddressPtr> &outputs) { if (is_null_input_) { return true; } cudaStream_t stream = reinterpret_cast<cudaStream_t>(cuda_stream_); T *image_size_addr = GetDeviceAddress<T>(inputs, kIndex0); T image_size[kShapeSize3]; CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE( cudaMemcpyAsync(&image_size, image_size_addr, kShapeSize3 * unit_dtype_size_, cudaMemcpyDeviceToHost, stream), "cudaMemcpy failed."); int32_t* workspace_addr = GetDeviceAddress<int32_t>(workspace, kIndex0);
void *curandState_addr = GetDeviceAddress<void *>(workspace, kIndex1); curandState *devStates = reinterpret_cast<curandState *>(curandState_addr); SampleDistortedBoundingBoxV2(image_size, seed_, seed2_, aspect_ratio_range_[kIndex0], aspect_ratio_range_[kIndex1], area_range_[kIndex0],area_range_[kIndex1] ,max_attempts_, use_image_if_no_bounding_boxes_,workspace_addr, devStates, reinterpret_cast<cudaStream_t>(cuda_stream_)); SelectBox<T>(inputs, workspace, outputs); return true; }
const std::vector<std::pair<KernelAttr, KernelRunFunc>> &SampleDistortedBoundingBoxV2GpuKernelMod::GetFuncList() const { static const std::vector<std::pair<KernelAttr, KernelRunFunc>> func_list = { ADD_KERNEL(kNumberTypeUInt8, uint8_t), ADD_KERNEL(kNumberTypeInt8, int8_t), ADD_KERNEL(kNumberTypeInt16, int16_t), ADD_KERNEL(kNumberTypeInt32, int32_t), ADD_KERNEL(kNumberTypeInt64, int64_t), }; return func_list; } MS_KERNEL_FACTORY_REG(NativeGpuKernelMod, SampleDistortedBoundingBoxV2, SampleDistortedBoundingBoxV2GpuKernelMod); } }
|