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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
#include <ATen/Utils.h>
#include <c10/core/ScalarType.h>
#include <c10/util/Exception.h>
#include <c10/util/accumulate.h>
#include <c10/util/irange.h>
#include <torch/csrc/jit/ir/constants.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/passes/constant_propagation.h>
#include <torch/csrc/jit/passes/dead_code_elimination.h>
#include <torch/csrc/jit/passes/fold_conv_bn.h>
#include <torch/csrc/jit/passes/frozen_conv_folding.h>
#include <torch/csrc/jit/passes/utils/optimization_utils.h>
#include <torch/csrc/jit/tensorexpr/types.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/Functions.h>
#else
#include <ATen/ops/ones_like.h>
#include <ATen/ops/zeros.h>
#include <ATen/ops/zeros_like.h>
#endif
namespace torch {
namespace jit {
namespace {
using Tensor = at::Tensor;
bool supportedConvNode(Node* n) {
switch (n->kind()) {
case aten::conv1d:
case aten::conv2d:
case aten::conv3d:
return true;
case aten::_convolution: {
auto transposed_conv =
constant_as<bool>(n->namedInput("transposed")).value_or(true);
// dont handle transposed conv yet or not-constant transpose parameter
return !transposed_conv;
}
default:
return false;
}
}
bool FoldFrozenConvBatchnorm(Block* b) {
bool graph_modified = false;
for (Node* n : b->nodes()) {
for (Block* block : n->blocks()) {
graph_modified |= FoldFrozenConvBatchnorm(block);
}
if (n->kind() == aten::batch_norm &&
supportedConvNode(n->inputs().at(0)->node())) {
auto conv = n->inputs().at(0)->node();
auto bn = n;
if (nonConstantParameters(conv) || nonConstantParameters(bn)) {
continue;
}
if (conv->output()->uses().size() > 1) {
continue;
}
auto bn_rm_ivalue = bn->namedInput("running_mean");
auto bn_rv_ivalue = bn->namedInput("running_var");
// check running_mean and running_var has value, if they are
// None(track_running_stats=False), skiping the folding path.
if (bn_rm_ivalue->type() == NoneType::get() &&
bn_rv_ivalue->type() == NoneType::get()) {
continue;
}
auto bn_rm = constant_as<Tensor>(bn->namedInput("running_mean")).value();
auto bn_rv = constant_as<Tensor>(bn->namedInput("running_var")).value();
auto bn_eps = constant_as<double>(bn->namedInput("eps")).value();
auto conv_w = constant_as<Tensor>(conv->namedInput("weight")).value();
// implementation taken from torch/nn/utils/fusion.py
Tensor conv_b;
if (conv->namedInput("bias")->type() == NoneType::get()) {
// If this is on GPU and bias is none and weight was half/bfloat, but
// bn_rm was float, then probably this was a case where autocasting
// casted inputs to conv. And since CUDA conv implementation requires
// all the inputs to have the same scalar dtype, we need to make this
// placeholder have the same type as conv_w.
at::ScalarType bias_dtype = bn_rm.scalar_type();
at::ScalarType weight_dtype = conv_w.scalar_type();
if ((weight_dtype == at::kHalf || weight_dtype == at::kBFloat16) &&
bias_dtype == at::kFloat) {
bias_dtype = weight_dtype;
}
conv_b = at::zeros_like(bn_rm, at::TensorOptions().dtype(bias_dtype));
} else {
conv_b = constant_as<Tensor>(conv->namedInput("bias")).value();
}
Tensor bn_w;
if (bn->namedInput("weight")->type() == NoneType::get()) {
bn_w = at::ones_like(bn_rm);
} else {
bn_w = constant_as<Tensor>(bn->namedInput("weight")).value();
}
Tensor bn_b;
if (n->namedInput("bias")->type() == NoneType::get()) {
bn_b = at::zeros_like(bn_rm);
} else {
bn_b = constant_as<Tensor>(bn->namedInput("bias")).value();
}
ConvBNParameters params;
params.conv_w = conv_w;
params.conv_b = conv_b;
params.bn_rm = bn_rm;
params.bn_rv = bn_rv;
params.bn_eps = bn_eps;
params.bn_w = bn_w;
params.bn_b = bn_b;
std::tuple<Tensor, Tensor> out = computeUpdatedConvWeightAndBias(params);
WithInsertPoint guard(conv);
auto fused_conv_w = b->owningGraph()->insertConstant(std::get<0>(out));
auto fused_conv_b = b->owningGraph()->insertConstant(std::get<1>(out));
auto conv_w_value = conv->namedInput("weight");
auto conv_b_value = conv->namedInput("bias");
fused_conv_w->setDebugName(conv_w_value->debugName() + "_fused_bn");
fused_conv_b->setDebugName(conv_b_value->debugName() + "_fused_bn");
conv->replaceInputWith(conv_w_value, fused_conv_w);
conv->replaceInputWith(conv_b_value, fused_conv_b);
bn->output()->replaceAllUsesWith(conv->output());
graph_modified = true;
}
}
return graph_modified;
}
bool supportedAddOrSub(Node* n) {
static const OperatorSet add_set{
"aten::add.Tensor(Tensor self, Tensor other, *, Scalar alpha=1) -> Tensor",
"aten::add.Scalar(Tensor self, Scalar other, Scalar alpha=1) -> Tensor",
// sub is equivalent to add
"aten::sub.Tensor(Tensor self, Tensor other, *, Scalar alpha=1) -> Tensor",
"aten::sub.Scalar(Tensor self, Scalar other, Scalar alpha=1) -> Tensor",
};
return n->isMemberOf(add_set);
}
// In order to fuse add/sub/mul/div with conv, the dimensions of its
// constant tensor must satisfy the following:
// - with resizing, broadcast to w/ weight/bias tensor shape
// - broadcast to the conv output shape
// It needs to have a shape that can resize to weight/bias
// tensor shape because we need to run the op with the conv
// weights/bias without changing their sizes.
// It needs to broadcast to the conv output shape so that we do
// accidentally change the shape of op output by pre-fusing it
// compared to eager.
// The only dimension value shared by weight/bias/conv output
// is they all contain a dim with value = channels-out. In the
// conv output tensor, this is in the second dimension,
// so the pointwise op tensor may have a second dimension of
// value == channels-out, but all the other dimensions have to be 1
bool opDoesNotBroadCastWithConv(Tensor& op_tensor, Tensor& weight_tensor) {
if (op_tensor.ndimension() > weight_tensor.ndimension()) {
return false;
}
for (int64_t i = op_tensor.ndimension() - 1; i >= 0; i--) {
// channels-out dimension == weight_tensor.size(0)
if (i == 1 && op_tensor.size(i) == weight_tensor.size(0)) {
continue;
}
if (op_tensor.size(i) != 1) {
return false;
}
}
return true;
}
bool checkConvAndBroadcastingOpPreConditions(Node* conv, Node* op) {
if (nonConstantParameters(conv) || nonConstantParameters(op)) {
return false;
}
if (conv->output()->uses().size() > 1) {
return false;
}
Tensor weight_tensor =
constant_as<Tensor>(conv->namedInput("weight")).value();
// avoid fusing op that causes type promotion
// resticting to float avoids int/float difficulties with scalar overload
if (!weight_tensor.is_floating_point()) {
return false;
}
if (op->inputs().at(1)->type()->cast<TensorType>()) {
auto op_tensor = constant_as<Tensor>(op->inputs().at(1)).value();
if (!opDoesNotBroadCastWithConv(op_tensor, weight_tensor)) {
return false;
}
if (!op_tensor.is_floating_point() &&
c10::promoteTypes(
op_tensor.scalar_type(), weight_tensor.scalar_type()) !=
weight_tensor.scalar_type()) {
return false;
}
}
return true;
}
Tensor resizeConstantScalarOrTensorToShape(
Value* v,
const std::vector<int64_t>& shape,
at::TensorOptions options) {
Tensor ret_tensor;
if (v->type()->cast<TensorType>()) {
ret_tensor = constant_as<Tensor>(v).value();
} else {
ret_tensor = at::zeros(shape, options);
if (v->type()->cast<IntType>()) {
ret_tensor.fill_(constant_as<int64_t>(v).value());
} else {
ret_tensor.fill_(constant_as<double>(v).value());
}
}
if (ret_tensor.numel() == 1) {
// expand errors if the shape input has less # dims than the tensor input
ret_tensor = ret_tensor.reshape({1});
ret_tensor = ret_tensor.expand(shape);
} else {
TORCH_INTERNAL_ASSERT(ret_tensor.numel() == c10::multiply_integers(shape));
ret_tensor = ret_tensor.view(shape);
}
return ret_tensor;
}
bool FoldFrozenConvAddOrSub(Block* b) {
bool graph_modified = false;
for (Node* n : b->nodes()) {
for (Block* block : n->blocks()) {
graph_modified |= FoldFrozenConvAddOrSub(block);
}
if (supportedAddOrSub(n) && supportedConvNode(n->inputs().at(0)->node())) {
auto conv = n->inputs().at(0)->node();
auto add_or_sub = n;
if (!checkConvAndBroadcastingOpPreConditions(conv, add_or_sub)) {
continue;
}
Tensor weight_tensor =
constant_as<Tensor>(conv->namedInput("weight")).value();
Tensor add_or_sub_tensor = resizeConstantScalarOrTensorToShape(
add_or_sub->inputs().at(1),
{weight_tensor.size(0)},
weight_tensor.options());
Tensor bias;
if (conv->namedInput("bias")->type() == NoneType::get()) {
bias = at::zeros_like(add_or_sub_tensor, weight_tensor.dtype());
} else {
bias = constant_as<Tensor>(conv->namedInput("bias")).value();
}
WithInsertPoint guard(conv);
add_or_sub->replaceInputWith(
conv->output(), b->owningGraph()->insertConstant(bias));
add_or_sub->replaceInput(
1, b->owningGraph()->insertConstant(add_or_sub_tensor));
auto stack_out = runNodeIfInputsAreConstant(add_or_sub);
TORCH_INTERNAL_ASSERT(stack_out && stack_out->size() == 1);
Tensor fuse_bias = (*stack_out)[0].toTensor().to(bias.dtype());
auto fused_conv_b = b->owningGraph()->insertConstant(fuse_bias);
auto conv_b_value = conv->namedInput("bias");
fused_conv_b->setDebugName(
conv_b_value->debugName() + "_fused_" +
add_or_sub->kind().toUnqualString());
conv->replaceInputWith(conv_b_value, fused_conv_b);
add_or_sub->output()->replaceAllUsesWith(conv->output());
graph_modified = true;
// DCE run after cleans up nodes
}
}
return graph_modified;
}
bool supportedMulOrDiv(Node* n) {
static const OperatorSet add_set{
"aten::mul.Tensor(Tensor self, Tensor other) -> Tensor",
"aten::mul.Scalar(Tensor self, Scalar other) -> Tensor",
// div is equivalent to mul
"aten::div.Tensor(Tensor self, Tensor other) -> Tensor",
"aten::div.Scalar(Tensor self, Scalar other) -> Tensor",
};
return n->isMemberOf(add_set);
}
bool FoldFrozenConvMulOrDiv(Block* b) {
bool graph_modified = false;
for (Node* n : b->nodes()) {
for (Block* block : n->blocks()) {
graph_modified |= FoldFrozenConvMulOrDiv(block);
}
if (supportedMulOrDiv(n) && supportedConvNode(n->inputs().at(0)->node())) {
auto conv = n->inputs().at(0)->node();
auto mul_or_div = n;
if (!checkConvAndBroadcastingOpPreConditions(conv, mul_or_div)) {
continue;
}
Tensor weight_tensor =
constant_as<Tensor>(conv->namedInput("weight")).value();
int64_t out_channels = weight_tensor.size(0);
// We've already verified that the second input has numel == 1 or
// channels-out resize it to the shape that will broadcast to
// weight_tensor when the op is run so we dont change weight size
std::vector<int64_t> weight_compatible_size = {out_channels};
for (const auto i : c10::irange(1, weight_tensor.ndimension())) {
(void)i; // Suppress unused variable warning
weight_compatible_size.push_back(1);
}
WithInsertPoint guard(conv);
Tensor mul_tensor = resizeConstantScalarOrTensorToShape(
mul_or_div->inputs().at(1),
weight_compatible_size,
weight_tensor.options());
// First fold with weight tensor
mul_or_div->replaceInputWith(
conv->output(), b->owningGraph()->insertConstant(weight_tensor));
mul_or_div->replaceInput(1, b->owningGraph()->insertConstant(mul_tensor));
auto stack_out = runNodeIfInputsAreConstant(mul_or_div);
TORCH_INTERNAL_ASSERT(stack_out && stack_out->size() == 1);
Tensor fuse_weight = (*stack_out)[0].toTensor().to(weight_tensor.dtype());
auto fused_conv_weight = b->owningGraph()->insertConstant(fuse_weight);
auto conv_weight_value = conv->namedInput("weight");
fused_conv_weight->setDebugName(
conv_weight_value->debugName() + "_fused_" +
mul_or_div->kind().toUnqualString());
conv->replaceInputWith(conv_weight_value, fused_conv_weight);
mul_or_div->output()->replaceAllUsesWith(conv->output());
// now fold with bias tensor
if (conv->namedInput("bias")->type() != NoneType::get()) {
Tensor bias = constant_as<Tensor>(conv->namedInput("bias")).value();
// bias is of shape {channels_out}
auto mul_tensor = resizeConstantScalarOrTensorToShape(
mul_or_div->inputs().at(1), {out_channels}, bias.options());
mul_or_div->replaceInput(0, b->owningGraph()->insertConstant(bias));
mul_or_div->replaceInput(
1, b->owningGraph()->insertConstant(mul_tensor));
auto stack_out = runNodeIfInputsAreConstant(mul_or_div);
TORCH_INTERNAL_ASSERT(stack_out && stack_out->size() == 1);
Tensor fuse_bias = (*stack_out)[0].toTensor().to(bias.dtype());
auto fused_conv_bias = b->owningGraph()->insertConstant(fuse_bias);
auto conv_b_value = conv->namedInput("bias");
fused_conv_weight->setDebugName(
conv_b_value->debugName() + "_fused_" +
mul_or_div->kind().toUnqualString());
conv->replaceInputWith(conv_b_value, fused_conv_bias);
}
graph_modified = true;
// DCE run after cleans up nodes
}
}
return graph_modified;
}
} // namespace
bool FoldFrozenConvBatchnorm(std::shared_ptr<Graph>& graph) {
bool graph_modified = FoldFrozenConvBatchnorm(graph->block());
EliminateDeadCode(graph);
return graph_modified;
}
bool FoldFrozenConvAddOrSub(std::shared_ptr<Graph>& graph) {
bool graph_modified = FoldFrozenConvAddOrSub(graph->block());
EliminateDeadCode(graph);
return graph_modified;
}
bool FoldFrozenConvMulOrDiv(std::shared_ptr<Graph>& graph) {
bool graph_modified = FoldFrozenConvMulOrDiv(graph->block());
EliminateDeadCode(graph);
return graph_modified;
}
} // namespace jit
} // namespace torch
|