I have a function which slices and resizes tensors, essentially cropping an image around an object, resizing it and putting it in a batch.  How can I parallelize this? I know I cannot do it with batched operations because the size of the slice differs.  But, there’s no reason I can’t run them in parallel.  How can I parallelize the function to have them run at the same time on the same GPU?
This is the function:
def slice_and_resize(imgs, center, diff):
        # TODO: parallelize slicing
        for i in range(B):
            xc, yc = center[i]
            diff_i = diff[i]
            if diff_i == 0:
                cropped_img = torch.zeros((1, 3, 224, 224)).cuda()
            else:
                x1 = torch.maximum(torch.FloatTensor([0]), xc - diff_i)
                x2 = torch.minimum(xc + diff_i, torch.FloatTensor([w]))
                y1 = torch.maximum(torch.FloatTensor([0]), yc - diff_i)
                y2 = torch.minimum(yc + diff_i, torch.FloatTensor([h]))
                cropped_img = (imgs[i, int(y1):int(y2), int(x1):int(x2), :])
                cropped_img = F.interpolate(cropped_img.permute(2,0,1).unsqueeze(0), (224,224))

            cropped_imgs[i] = cropped_img
        return cropped_imgs

of note, this function is called at inference time, so I run it with torch.no_grad()