handle odd padding mode forward Args: x, the input tensor odd_padding, the odd_padding Returns: tensor, the output
(x, odd_padding, is_pool=False)
| 54 | |
| 55 | |
| 56 | def handle_odd_pad_fwd(x, odd_padding, is_pool=False): |
| 57 | """ |
| 58 | handle odd padding mode forward |
| 59 | Args: |
| 60 | x, the input tensor |
| 61 | odd_padding, the odd_padding |
| 62 | Returns: |
| 63 | tensor, the output |
| 64 | """ |
| 65 | # (axis, left padding if True else right padding) |
| 66 | flags = [(2, True), (2, False), (3, True), (3, False)] |
| 67 | for (axis, left), pad in zip(flags, odd_padding): |
| 68 | if pad == 0: |
| 69 | continue |
| 70 | if is_pool: |
| 71 | if left: |
| 72 | padding = singa.SliceOn(x, 0, pad, axis) |
| 73 | else: |
| 74 | axis_shape = list(x.shape())[axis] |
| 75 | padding = singa.SliceOn(x, axis_shape - pad, axis_shape, axis) |
| 76 | else: |
| 77 | pad_shape = list(x.shape()) |
| 78 | pad_shape[axis] = pad |
| 79 | padding = singa.Tensor(list(pad_shape), x.device()) |
| 80 | padding.SetFloatValue(0.) |
| 81 | if left: |
| 82 | x = singa.ConcatOn(singa.VecTensor([padding, x]), axis) |
| 83 | else: |
| 84 | x = singa.ConcatOn(singa.VecTensor([x, padding]), axis) |
| 85 | return x |
| 86 | |
| 87 | |
| 88 | def handle_odd_pad_bwd(dx, odd_padding): |