1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#include <torch/csrc/jit/tensorexpr/operators/reduction.h>
using namespace torch::jit::tensorexpr;
// Remove all indices from axes positions.
static std::vector<VarHandle> squeezeIndices(
const ParameterList& indices,
const std::vector<size_t>& axes) {
std::vector<VarHandle> indices_squeezed;
for (size_t dim = 0; dim < indices.size(); ++dim) {
if (!std::count(axes.begin(), axes.end(), dim)) {
indices_squeezed.push_back(indices[dim]);
}
}
return indices_squeezed;
}
namespace torch::jit::tensorexpr {
Tensor computeSum(
const std::vector<ArgValue>& inputs,
const std::vector<ExprHandle>& outputShape,
const std::vector<ExprHandle>& outputStrides,
const std::optional<ScalarType>& outputType,
at::Device device) {
std::vector<size_t> axes;
bool keepdim = false;
// aten::sum takes the input tensor named self.
auto sizes = valueShape(inputs[0]);
size_t rank = sizes.size();
if (inputs.size() > 2) {
if (auto emptyAxes = std::get_if<BufList>(&inputs[1])) {
// If dim-array is an empty list, it will appear as BufList instead of
// IntList, and hence we need a special handling for it.
// In that case, we need to sum over all axes.
TORCH_INTERNAL_ASSERT(emptyAxes->empty());
axes.resize(rank);
std::iota(axes.begin(), axes.end(), 0);
} else if (rank > 0) {
auto const& nodeAxes = std::get<IntList>(inputs[1]);
// Canonicalize axes: wrap around, sort and make unique.
for (auto axis : nodeAxes) {
axes.push_back(at::maybe_wrap_dim(axis, static_cast<int64_t>(rank)));
}
std::sort(axes.begin(), axes.end());
axes.erase(std::unique(axes.begin(), axes.end()), axes.end());
}
keepdim = std::get<bool>(inputs[2]);
} else {
axes.resize(rank);
std::iota(axes.begin(), axes.end(), 0);
}
// Axes go into reduction dimensions.
std::vector<ExprHandle> reductionDims;
reductionDims.reserve(rank);
for (size_t axis : axes) {
reductionDims.emplace_back(sizes[axis]);
}
std::vector<ExprHandle> outputDims;
// Output dimensions are the complement of axes. When keepdim is set, a
// one-sized dimension is inserted for each axis.
for (size_t dim = 0; dim < rank; ++dim) {
if (!std::count(axes.begin(), axes.end(), dim)) {
outputDims.emplace_back(sizes[dim]);
} else if (keepdim) {
outputDims.emplace_back(1);
}
}
return Reduce(
"sum",
outputDims,
outputStrides,
Sum(),
[&](ParameterList& indices) {
// "Squeeze" out indices inserted when keepdim is set.
auto indices_squeezed =
keepdim ? squeezeIndices(indices, axes) : indices;
TORCH_INTERNAL_ASSERT(axes.size() <= indices_squeezed.size());
// Move innermost indices into axes positions:
// 1. Fill the outermost indices first.
// 2. Insert the innermost indices into the correct axis position,
// displacing the outermost indices as needed.
std::vector<ExprHandle> indices_exprs;
size_t i = 0;
for (; i < indices_squeezed.size() - axes.size(); ++i) {
indices_exprs.push_back(indices_squeezed[i]);
}
for (auto axis : axes) {
indices_exprs.insert(
indices_exprs.begin() + static_cast<std::ptrdiff_t>(axis),
indices_squeezed[i]);
++i;
}
auto indexed = tensorOrConstant(inputs[0], indices_exprs);
if (outputType) {
return Cast::make(ToDtype(*outputType), indexed);
} else {
return indexed;
}
},
reductionDims);
}
Tensor computeMean(
const std::vector<ArgValue>& inputs,
const std::vector<ExprHandle>& outputShape,
const std::vector<ExprHandle>& outputStrides,
const std::optional<ScalarType>& outputType,
at::Device device) {
Dtype dtype = kFloat;
if (outputType) {
dtype = Dtype(*outputType);
}
bool keepdim = false;
BufHandle ResultBuf("mean", outputShape, dtype);
auto const& InputBuf = std::get<BufHandle>(inputs[0]);
std::vector<ExprHandle> extra_args;
if (inputs.size() > 2) {
keepdim = std::get<bool>(inputs[2]);
}
if (auto mean_dims = std::get_if<IntList>(&inputs[1])) {
extra_args = c10::fmap<ExprHandle>(*mean_dims);
} else {
// When dims argument is not specified, reduce over all dimensions
for (int64_t idx = 0; idx < static_cast<int64_t>(InputBuf.ndim()); ++idx) {
extra_args.emplace_back(idx);
}
}
extra_args.push_back(LongImm::make(static_cast<int64_t>(keepdim)));
return Tensor(
ResultBuf.node(),
ExternalCall::make(ResultBuf, "nnc_aten_mean", {InputBuf}, extra_args));
}
Tensor computeMax(
const std::vector<ArgValue>& inputs,
const std::vector<ExprHandle>& outputShape,
const std::vector<ExprHandle>& outputStrides,
const std::optional<ScalarType>& outputType,
at::Device device) {
Dtype dtype = kFloat;
if (outputType) {
dtype = Dtype(*outputType);
}
BufHandle ResultBuf("max", outputShape, dtype);
auto const& InputBuf = std::get<BufHandle>(inputs[0]);
auto max_dim = std::get<int64_t>(inputs[1]);
auto keep_dim = std::get<bool>(inputs[2]);
return Tensor(
ResultBuf.node(),
ExternalCall::make(
ResultBuf,
"nnc_aten_max_red",
{InputBuf},
{max_dim, (int64_t)keep_dim}));
}
Tensor computeAdaptiveAvgPool2d(
const std::vector<ArgValue>& inputs,
const std::vector<ExprHandle>& outputShape,
const std::vector<ExprHandle>& outputStrides,
const std::optional<ScalarType>& outputType,
at::Device device) {
Dtype dtype = kFloat;
if (outputType) {
dtype = Dtype(*outputType);
}
BufHandle ResultBuf("adaptive_avgpool2d", outputShape, dtype);
auto const& out_size_param = std::get<IntList>(inputs[1]);
return Tensor(
ResultBuf.node(),
ExternalCall::make(
ResultBuf,
"nnc_aten_adaptive_avg_pool2d",
{std::get<BufHandle>(inputs[0])},
c10::fmap<ExprHandle>(out_size_param)));
}
} // namespace torch::jit::tensorexpr
|