im2col.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // MIT license
  3. // Copyright (C) 2024 Intel Corporation
  4. // SPDX-License-Identifier: MIT
  5. //
  6. //
  7. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  8. // See https://llvm.org/LICENSE.txt for license information.
  9. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  10. //
  11. #include "im2col.hpp"
  12. template <typename T>
  13. static void im2col_kernel(
  14. const float *x, T *dst, int64_t batch_offset, int64_t offset_delta,
  15. int64_t IC, int64_t IW, int64_t IH, int64_t OH, int64_t OW, int64_t KW, int64_t KH,
  16. int64_t pelements, int64_t CHW, int s0, int s1, int p0, int p1, int d0, int d1,
  17. const sycl::nd_item<3> &item_ct1) {
  18. const int64_t work_group_size = item_ct1.get_local_range(2);
  19. const int64_t global_id = item_ct1.get_local_id(2) + work_group_size * item_ct1.get_group(2);
  20. // make each work-item deal with more elements since sycl global range can not exceed max int
  21. for (int64_t i = global_id; i < pelements; i += work_group_size * item_ct1.get_group_range(2)) {
  22. const int64_t ksize = OW * (KH > 1 ? KW : 1);
  23. const int64_t kx = i / ksize;
  24. const int64_t kd = kx * ksize;
  25. const int64_t ky = (i - kd) / OW;
  26. const int64_t ix = i % OW;
  27. const int64_t oh = item_ct1.get_group(1);
  28. const int64_t batch = item_ct1.get_group(0) / IC;
  29. const int64_t ic = item_ct1.get_group(0) % IC;
  30. const int64_t iiw = ix * s0 + kx * d0 - p0;
  31. const int64_t iih = oh * s1 + ky * d1 - p1;
  32. const int64_t offset_dst =
  33. ((batch * OH + oh) * OW + ix) * CHW +
  34. (ic * (KW * KH) + ky * KW + kx);
  35. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  36. dst[offset_dst] =
  37. sycl::vec<float, 1>(0.0f)
  38. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  39. } else {
  40. const int64_t offset_src = ic * offset_delta + batch * batch_offset;
  41. dst[offset_dst] =
  42. sycl::vec<float, 1>(x[offset_src + iih * IW + iiw])
  43. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  44. }
  45. }
  46. }
  47. template <typename T>
  48. static void im2col_sycl(
  49. const float *x, T *dst, int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW,
  50. int64_t KH, int64_t IC, int64_t batch, int64_t batch_offset, int64_t offset_delta,
  51. int s0, int s1, int p0, int p1, int d0, int d1,
  52. queue_ptr stream) {
  53. const int64_t parallel_elements = OW * KW * KH;
  54. const int64_t num_blocks = (parallel_elements + SYCL_IM2COL_BLOCK_SIZE - 1) / SYCL_IM2COL_BLOCK_SIZE;
  55. // decrease global range when it exceeds the max int
  56. int64_t local_size = downsample_sycl_global_range(batch * IC * OH * num_blocks, SYCL_IM2COL_BLOCK_SIZE);
  57. sycl::range<3> block_nums(batch * IC, OH, num_blocks);
  58. sycl::range<3> local_range(1, 1, local_size);
  59. {
  60. dpct::has_capability_or_fail(stream->get_device(),
  61. {sycl::aspect::fp16});
  62. stream->parallel_for(
  63. sycl::nd_range<3>(block_nums * local_range, local_range),
  64. [=](sycl::nd_item<3> item_ct1) {
  65. im2col_kernel(x, dst, batch_offset, offset_delta, IC, IW, IH, OH, OW, KW, KH,
  66. parallel_elements, (IC * KH * KW), s0, s1, p0,
  67. p1, d0, d1, item_ct1);
  68. });
  69. }
  70. }
  71. void ggml_sycl_op_im2col(
  72. ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  73. ggml_tensor *dst, const float *src0_dd, const float *src1_dd, float *dst_dd,
  74. const queue_ptr &main_stream) {
  75. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  76. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  77. GGML_ASSERT(dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  78. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  79. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  80. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  81. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  82. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  83. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  84. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  85. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  86. const int64_t IH = is_2D ? src1->ne[1] : 1;
  87. const int64_t IW = src1->ne[0];
  88. const int64_t KH = is_2D ? src0->ne[1] : 1;
  89. const int64_t KW = src0->ne[0];
  90. const int64_t OH = is_2D ? dst->ne[2] : 1;
  91. const int64_t OW = dst->ne[1];
  92. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  93. const int64_t batch = src1->ne[3];
  94. const size_t batch_offset = src1->nb[3] / 4; // nb is byte offset, src is type float32
  95. if (dst->type == GGML_TYPE_F16) {
  96. im2col_sycl(src1_dd, (sycl::half *)dst_dd, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  97. } else {
  98. im2col_sycl(src1_dd, (float *)dst_dd, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  99. }
  100. GGML_UNUSED(src0);
  101. GGML_UNUSED(src0_dd);
  102. GGML_UNUSED(ctx);
  103. }