Skip to content

Commit 8464384

Browse files
author
Mark-ZhouWX
committed
optimize performance with pad -> concat
1 parent f08bbdd commit 8464384

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

research/segment-anything/configs/coco_box_finetune.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ train_loader:
6161

6262
shuffle: True
6363
batch_size: 1
64-
epoch_size: 10
64+
epoch_size: 20
6565
drop_remainder: True
6666
num_workers: 2
6767
max_rowsize: 24 # 24M space for dataloader

research/segment-anything/segment_anything/modeling/image_encoder.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,10 @@ def window_partition(x: ms.Tensor, window_size: int) -> Tuple[ms.Tensor, Tuple[i
264264
pad_h = (window_size - H % window_size) % window_size
265265
pad_w = (window_size - W % window_size) % window_size
266266
if pad_h > 0 or pad_w > 0:
267-
x = ops.pad(x, (0, 0, 0, pad_w, 0, pad_h))
267+
# replace ops.pad with ops.concat for better performance
268+
pad_mat1 = ops.zeros((B, H, pad_w, C), x.dtype)
269+
pad_mat2 = ops.zeros((B, pad_h, W + pad_w, C), x.dtype)
270+
x = ops.concat([ops.concat([x, pad_mat1], axis=2), pad_mat2], axis=1)
268271
Hp, Wp = H + pad_h, W + pad_w
269272

270273
x = x.view(B, Hp // window_size, window_size, Wp // window_size, window_size, C)
@@ -401,7 +404,8 @@ def __init__(
401404
)
402405

403406
def construct(self, x: ms.Tensor) -> ms.Tensor:
404-
x = ops.pad(x, (self.padding[0], self.padding[0], self.padding[1], self.padding[1])) # to align with torch
407+
if sum(self.padding) > 0:
408+
x = ops.pad(x, (self.padding[0], self.padding[0], self.padding[1], self.padding[1])) # to align with torch
405409
x = self.proj(x)
406410
# B C H W -> B H W C
407411
x = x.permute(0, 2, 3, 1)

0 commit comments

Comments
 (0)