Guest User

Untitled

a guest
Jan 21st, 2026
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. def conv2d(x, weight, bias=None, stride=1, padding=0, dilation=1, groups=1):
  2. if bias is None:
  3. bias = torch.zeros((weight.shape[0],), device=x.device, dtype=x.dtype)
  4.  
  5. if isinstance(stride, int):
  6. stride = (stride, stride)
  7. if isinstance(padding, int):
  8. padding = (padding, padding)
  9. if isinstance(dilation, int):
  10. dilation = (dilation, dilation)
  11.  
  12. N, Cin, H, W = x.shape
  13. Cout, Cin_per_w, Kh, Kw = weight.shape
  14. sh, sw = stride
  15. ph, pw = padding
  16. dh, dw = dilation
  17.  
  18. assert Cin % groups == 0, "Cin must be divisible by groups"
  19. assert Cout % groups == 0, "Cout must be divisible by groups"
  20. assert Cin_per_w == (Cin // groups), "w.shape[1] must equal Cin/groups"
  21.  
  22. Kh_eff = dh * (Kh - 1) + 1
  23. Kw_eff = dw * (Kw - 1) + 1
  24.  
  25. Hout = (H + 2 * ph - Kh_eff) // sh + 1
  26. Wout = (W + 2 * pw - Kw_eff) // sw + 1
  27.  
  28. y = torch.empty((N, Cout, Hout, Wout), device=x.device, dtype=x.dtype)
  29.  
  30. s_x_n, s_x_c, s_x_h, s_x_w = x.stride()
  31. s_w_co, s_w_cinperg, s_w_kh, s_w_kw = weight.stride()
  32. s_y_n, s_y_c, s_y_h, s_y_w = y.stride()
  33.  
  34. BLOCK_OC = 8
  35. BLOCK_HW = 128
  36. hw_tiles = triton.cdiv(Hout * Wout, BLOCK_HW)
  37. Cout_g = Cout // groups
  38. oc_tiles_per_group = triton.cdiv(Cout_g, BLOCK_OC)
  39.  
  40. grid = (hw_tiles * oc_tiles_per_group, N, groups)
  41. conv2d_kernel[grid](
  42. x, weight, bias, y,
  43. N, Cin, H, W,
  44. Cout, Kh, Kw,
  45. Hout, Wout,
  46. sh, sw,
  47. ph, pw,
  48. dh, dw,
  49. groups,
  50. s_x_n, s_x_c, s_x_h, s_x_w,
  51. s_w_co, s_w_cinperg, s_w_kh, s_w_kw,
  52. s_y_n, s_y_c, s_y_h, s_y_w,
  53. BLOCK_OC=BLOCK_OC,
  54. BLOCK_HW=BLOCK_HW,
  55. num_warps=4,
  56. num_stages=1,
  57. )
  58.  
  59. return y
  60.  
  61. @triton.jit
  62. def conv2d_kernel(
  63. X_ptr, W_ptr, B_ptr, Y_ptr,
  64. N, Cin, H, W,
  65. Cout, Kh, Kw,
  66. Hout, Wout,
  67. sh, sw,
  68. ph, pw,
  69. dh, dw,
  70. groups,
  71. s_x_n, s_x_c, s_x_h, s_x_w,
  72. s_w_co, s_w_cinperg, s_w_kh, s_w_kw,
  73. s_y_n, s_y_c, s_y_h, s_y_w,
  74. BLOCK_OC: tl.constexpr,
  75. BLOCK_HW: tl.constexpr,
  76. ):
  77. pid0 = tl.program_id(0) # spatial * oc
  78. pid1 = tl.program_id(1) # batch index
  79. pid2 = tl.program_id(2) # group index
  80.  
  81. Cin_g = Cin // groups
  82. Cout_g = Cout // groups
  83.  
  84. hw_tiles = tl.cdiv(Hout * Wout, BLOCK_HW)
  85. oc_tiles_per_group = tl.cdiv(Cout_g, BLOCK_OC)
  86.  
  87. hw_block = pid0 % hw_tiles
  88. oc_block_in_group = pid0 // hw_tiles
  89. oc_base = pid2 * Cout_g + oc_block_in_group * BLOCK_OC
  90.  
  91. oc_offsets = oc_base + tl.arange(0, BLOCK_OC)
  92. hw_offsets = hw_block * BLOCK_HW + tl.arange(0, BLOCK_HW)
  93.  
  94. h = hw_offsets // Wout
  95. w = hw_offsets % Wout
  96.  
  97. mask_oc = (oc_offsets < (pid2 + 1) * Cout_g) & (oc_offsets < Cout)
  98. mask_hw = hw_offsets < Hout * Wout
  99. mask_n = pid1 < N
  100. mask_group = pid2 < groups
  101.  
  102. acc = tl.full((BLOCK_OC, BLOCK_HW), 0.0, tl.float32)
  103.  
  104. Xb = X_ptr + pid1 * s_x_n
  105. Yb = Y_ptr + pid1 * s_y_n
  106.  
  107. cin_group_start = pid2 * Cin_g
  108. Wb = W_ptr + oc_offsets * s_w_co
  109.  
  110. for icg in range(Cin_g):
  111. ic_base = Wb + icg * s_w_cinperg
  112.  
  113. for ky in range(Kh):
  114. for kx in range(Kw):
  115. in_h = h * sh + ky * dh - ph
  116. in_w = w * sw + kx * dw - pw
  117.  
  118. in_bounds = (in_h >= 0) & (in_h < H) & (in_w >= 0) & (in_w < W)
  119.  
  120. x_ptrs = Xb + (cin_group_start + icg) * s_x_c + in_h * s_x_h + in_w * s_x_w
  121. x = tl.load(x_ptrs, mask=mask_hw & in_bounds & mask_n, other=0.0)
  122.  
  123. w_ptrs = ic_base + ky * s_w_kh + kx * s_w_kw
  124. w_val = tl.load(w_ptrs, mask=mask_oc, other=0.0)
  125.  
  126. acc += x[None, :] * w_val[:, None]
  127.  
  128. bias = tl.load(B_ptr + oc_offsets, mask=mask_oc, other=0.0)
  129. acc += bias[:, None]
  130.  
  131. out_ptrs = Yb + oc_offsets[:, None] * s_y_c + h[None, :] * s_y_h + w[None, :] * s_y_w
  132. tl.store(out_ptrs, acc, mask=mask_oc[:, None] & mask_hw & mask_n & mask_group)
Advertisement
Add Comment
Please, Sign In to add comment