| |
| |
|
|
| #pragma once |
|
|
| #include <torch/torch.h> |
|
|
| inline |
| torch::Tensor region_counts_to_indices(torch::Tensor regionCounts, int64_t numOutputs) |
| { |
| |
| if (regionCounts.size(0) == 1) { |
| return torch::zeros({ numOutputs }, regionCounts.options().dtype(torch::kInt64)); |
| } |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| auto indices = torch::arange(regionCounts.size(0), regionCounts.options().dtype(torch::kInt64)); |
|
|
| auto outputIndices = torch::repeat_interleave(indices, regionCounts, 0, numOutputs); |
|
|
| return outputIndices; |
| } |
|
|
| torch::Tensor gpu_indirect_grid_sample_forward(torch::Tensor input, torch::Tensor grid, torch::Tensor inputIndices, const std::string &method); |
| torch::Tensor cpu_indirect_grid_sample_forward(torch::Tensor input, torch::Tensor grid, torch::Tensor inputIndices, const std::string &method); |
| std::vector<torch::Tensor> gpu_indirect_grad_sample_backward(torch::Tensor gradOutput, torch::Tensor input, torch::Tensor grid, torch::Tensor inputIndices, const std::string &method); |
|
|
| inline |
| torch::Tensor indirect_grid_sample_forward(torch::Tensor input, torch::Tensor grid, torch::Tensor inputIndices, const std::string &method) |
| { |
| if (input.is_cuda() != grid.is_cuda() || input.is_cuda() != inputIndices.is_cuda()) { |
| throw std::runtime_error("Input tensors must all be on the same device!"); |
| } |
| if (inputIndices.size(0) != grid.size(0)) { |
| throw std::runtime_error("The batch dimensions must match!"); |
| } |
| if (grid.size(-1) != 2) { |
| throw std::runtime_error("The final grid dimension must be 2."); |
| } |
|
|
| if (input.is_cuda()) { |
| return gpu_indirect_grid_sample_forward(std::move(input), std::move(grid), std::move(inputIndices), method); |
| } else { |
| return cpu_indirect_grid_sample_forward(std::move(input), std::move(grid), std::move(inputIndices), method); |
| } |
| } |
|
|
| inline |
| std::vector<torch::Tensor> indirect_grad_sample_backward(torch::Tensor gradOutput, torch::Tensor input, torch::Tensor grid, torch::Tensor inputIndices, const std::string &method) |
| { |
| if (gradOutput.is_cuda()) { |
| return gpu_indirect_grad_sample_backward(std::move(gradOutput), std::move(input), std::move(grid), std::move(inputIndices), method); |
| } else { |
| throw std::runtime_error("Not implemented!"); |
| } |
| } |
|
|