|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-unsafe |
| 8 | + |
| 9 | + |
| 10 | +from typing import List |
| 11 | + |
| 12 | +import torch |
| 13 | + |
| 14 | +from torch.library import impl, Library |
| 15 | + |
| 16 | +preprocess_op_lib = Library("preprocess", "DEF") |
| 17 | + |
| 18 | +# Register and define pad and out variant. |
| 19 | +# Note: pad doesn't require an explicit meta kernel because |
| 20 | +# CompositeExplicitAutograd automatically registers the implementation to meta, |
| 21 | +# and meta kernels do not go through functionalization. The implementation |
| 22 | +# does not export due to issues during functionalization. |
| 23 | +# See: https://github.com/pytorch/pytorch/issues/120288 |
| 24 | +preprocess_op_lib.define("pad(Tensor image, SymInt[] padding) -> Tensor") |
| 25 | + |
| 26 | + |
| 27 | +@impl(preprocess_op_lib, "pad", dispatch_key="CompositeExplicitAutograd") |
| 28 | +def pad_impl( |
| 29 | + image: torch.Tensor, |
| 30 | + padding: List[int], |
| 31 | +) -> torch.Tensor: |
| 32 | + output = torch.empty( |
| 33 | + [image.shape[0], image.shape[1] + padding[3], image.shape[2] + padding[1]], |
| 34 | + dtype=image.dtype, |
| 35 | + device=image.device, |
| 36 | + requires_grad=False, |
| 37 | + ) |
| 38 | + output = torch.fill(output, 0) |
| 39 | + output.narrow(1, 0, image.shape[1]).narrow(2, 0, image.shape[2]).copy_(image) |
| 40 | + return output |
| 41 | + |
| 42 | + |
| 43 | +preprocess_op_lib.define( |
| 44 | + "pad.out(Tensor image, SymInt[] padding, *, Tensor(a!) out) -> Tensor(a!)" |
| 45 | +) |
| 46 | + |
| 47 | + |
| 48 | +@impl(preprocess_op_lib, "pad.out", dispatch_key="CompositeExplicitAutograd") |
| 49 | +def pad_out_impl( |
| 50 | + image: torch.Tensor, |
| 51 | + padding: List[int], |
| 52 | + out: torch.Tensor, |
| 53 | +) -> torch.Tensor: |
| 54 | + out = torch.empty( |
| 55 | + [image.shape[0], image.shape[1] + padding[3], image.shape[2] + padding[1]], |
| 56 | + dtype=image.dtype, |
| 57 | + device=image.device, |
| 58 | + requires_grad=False, |
| 59 | + ) |
| 60 | + out = torch.fill(out, 0) |
| 61 | + out.narrow(1, 0, image.shape[1]).narrow(2, 0, image.shape[2]).copy_(image) |
| 62 | + return out |
| 63 | + |
| 64 | + |
| 65 | +# Register and define tile_crop and out variant. |
| 66 | +preprocess_op_lib.define("tile_crop(Tensor input, int tile_size) -> Tensor") |
| 67 | + |
| 68 | + |
| 69 | +@impl(preprocess_op_lib, "tile_crop", dispatch_key="CompositeExplicitAutograd") |
| 70 | +def tile_crop_impl(input: torch.Tensor, tile_size: int) -> torch.Tensor: |
| 71 | + c = input.shape[0] |
| 72 | + h = input.shape[1] |
| 73 | + w = input.shape[2] |
| 74 | + tiles_height = h // tile_size |
| 75 | + tiles_width = w // tile_size |
| 76 | + tile_cropped = input.view(c, tiles_height, tile_size, tiles_width, tile_size) |
| 77 | + transposed = tile_cropped.permute(1, 3, 0, 2, 4) |
| 78 | + tiles = transposed.contiguous().view( |
| 79 | + tiles_height * tiles_width, c, tile_size, tile_size |
| 80 | + ) |
| 81 | + return tiles |
| 82 | + |
| 83 | + |
| 84 | +preprocess_op_lib.define( |
| 85 | + "tile_crop.out(Tensor input, int tile_size, *, Tensor(a!) out) -> Tensor(a!)" |
| 86 | +) |
| 87 | + |
| 88 | + |
| 89 | +@impl(preprocess_op_lib, "tile_crop.out", dispatch_key="CompositeExplicitAutograd") |
| 90 | +def tile_crop_out_impl( |
| 91 | + input: torch.Tensor, tile_size: int, out: torch.Tensor |
| 92 | +) -> torch.Tensor: |
| 93 | + out = input.clone() |
| 94 | + c = out.shape[0] |
| 95 | + h = out.shape[1] |
| 96 | + w = out.shape[2] |
| 97 | + tiles_height = h // tile_size |
| 98 | + tiles_width = w // tile_size |
| 99 | + out = out.view(c, tiles_height, tile_size, tiles_width, tile_size) |
| 100 | + out = out.permute(1, 3, 0, 2, 4) |
| 101 | + out = out.contiguous().view(tiles_height * tiles_width, c, tile_size, tile_size) |
| 102 | + return out |
| 103 | + |
| 104 | + |
| 105 | +# Register meta kernel to prevent export tracing into the tile_crop impl. |
| 106 | +@torch.library.register_fake("preprocess::tile_crop") |
| 107 | +def tile_crop(output: torch.Tensor, tile_size: int) -> torch.Tensor: |
| 108 | + # Returned tensor is of size [n, 3, 224, 224], where n is the number of tiles. |
| 109 | + # We should export with n = max_num_tiles. Set 50 for now. |
| 110 | + return torch.empty([50, output.size(0), 224, 224]) |
0 commit comments