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
|
#include <torch/csrc/jit/passes/graph_rewrite_helper.h>
#include <torch/csrc/jit/ir/subgraph_matcher.h>
#include <torch/csrc/jit/passes/constant_propagation.h>
#include <torch/csrc/jit/passes/subgraph_rewrite.h>
namespace torch {
namespace jit {
namespace graph_rewrite_helper {
std::string getFuncName(Value* func_value) {
auto func = func_value->type()->expectRef<FunctionType>().function();
const auto& qname = func->qualname();
const auto& name = qname.qualifiedName();
auto rdot_idx = name.rfind('.');
if (rdot_idx != std::string::npos) {
return name.substr(rdot_idx + 1, name.length());
} else {
return name;
}
}
Value* getValue(
const std::string& name,
const std::unordered_map<const Value*, Value*>& match_vmap,
const std::unordered_map<std::string, Value*>& vmap) {
return match_vmap.at(vmap.at(name));
}
c10::optional<IValue> getIValue(
const std::string& name,
const std::unordered_map<const Value*, Value*>& match_vmap,
const std::unordered_map<std::string, Value*>& vmap) {
return toIValue(getValue(name, match_vmap, vmap));
}
std::unordered_map<std::string, c10::IValue> getConvParams(
const Match& match,
const std::unordered_map<std::string, Value*>& vmap) {
std::unordered_map<std::string, c10::IValue> calc_values;
const auto& match_vmap = match.values_map;
auto transposed_value = getIValue("transposed", match_vmap, vmap).value();
calc_values["transposed"] = transposed_value;
auto output_padding_value =
getIValue("output_padding", match_vmap, vmap).value();
calc_values["output_padding"] = output_padding_value;
auto stride_value = getIValue("stride", match_vmap, vmap).value();
calc_values["stride"] = stride_value;
auto padding_value = getIValue("padding", match_vmap, vmap).value();
calc_values["padding"] = padding_value;
auto dilation_value = getIValue("dilation", match_vmap, vmap).value();
calc_values["dilation"] = dilation_value;
return calc_values;
}
void replaceConvolutionWithAtenConv(std::shared_ptr<Graph>& graph) {
// TODO: remove constant prop in the pass
ConstantPropagation(graph);
std::string convolution_deprecated = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool):
%r = aten::_convolution(%a, %w, %b, %stride, %padding, %dilation,
%transposed, %output_padding, %groups, %benchmark, %deterministic, %cudnn_enabled)
return (%r) )";
std::string convolution = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool, %allow_tf32:bool):
%r = aten::_convolution(%a, %w, %b, %stride, %padding, %dilation,
%transposed, %output_padding, %groups, %benchmark, %deterministic, %cudnn_enabled, %allow_tf32)
return (%r) )";
std::string conv2d_for_deprecated_conv = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool):
%r = aten::conv2d(%a, %w, %b, %stride, %padding, %dilation, %groups)
return (%r) )";
std::string conv2d = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool, %allow_tf32:bool):
%r = aten::conv2d(%a, %w, %b, %stride, %padding, %dilation, %groups)
return (%r) )";
std::string conv1d_for_deprecated_conv = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool):
%r = aten::conv1d(%a, %w, %b, %stride, %padding, %dilation, %groups)
return (%r) )";
std::string conv1d = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool, %allow_tf32:bool):
%r = aten::conv1d(%a, %w, %b, %stride, %padding, %dilation, %groups)
return (%r) )";
std::string conv3d_for_deprecated_conv = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool):
%r = aten::conv3d(%a, %w, %b, %stride, %padding, %dilation, %groups)
return (%r) )";
std::string conv3d = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool, %allow_tf32:bool):
%r = aten::conv3d(%a, %w, %b, %stride, %padding, %dilation, %groups)
return (%r) )";
std::string conv_transpose1d_for_deprecated_conv = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool):
%r = aten::conv_transpose1d(%a, %w, %b, %stride, %padding, %output_padding, %groups, %dilation)
return (%r) )";
std::string conv_transpose1d = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool, %allow_tf32:bool):
%r = aten::conv_transpose1d(%a, %w, %b, %stride, %padding, %output_padding, %groups, %dilation)
return (%r) )";
std::string conv_transpose2d_for_deprecated_conv = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool):
%r = aten::conv_transpose2d(%a, %w, %b, %stride, %padding, %output_padding, %groups, %dilation)
return (%r) )";
std::string conv_transpose2d = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool, %allow_tf32:bool):
%r = aten::conv_transpose2d(%a, %w, %b, %stride, %padding, %output_padding, %groups, %dilation)
return (%r) )";
std::string conv_transpose3d_for_deprecated_conv = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool):
%r = aten::conv_transpose3d(%a, %w, %b, %stride, %padding, %output_padding, %groups, %dilation)
return (%r) )";
std::string conv_transpose3d = R"(
graph(%a, %w, %b, %stride:int[], %padding:int[], %dilation:int[],
%transposed:bool, %output_padding:int[], %groups:int, %benchmark:bool,
%deterministic:bool, %cudnn_enabled:bool, %allow_tf32:bool):
%r = aten::conv_transpose3d(%a, %w, %b, %stride, %padding, %output_padding, %groups, %dilation)
return (%r) )";
// Filter the unsupported case
auto filter_conv1d = [](const Match& match,
const std::unordered_map<std::string, Value*>& vmap) {
auto calc_value_map = getConvParams(match, vmap);
if (calc_value_map["output_padding"].toIntList().size() != 1 ||
calc_value_map["stride"].toIntList().size() != 1 ||
calc_value_map["padding"].toIntList().size() != 1 ||
calc_value_map["dilation"].toIntList().size() != 1) {
return false;
}
return !calc_value_map["transposed"].toBool();
};
auto filter_conv2d = [](const Match& match,
const std::unordered_map<std::string, Value*>& vmap) {
auto calc_value_map = getConvParams(match, vmap);
if (calc_value_map["output_padding"].toIntList().size() != 2 ||
calc_value_map["stride"].toIntList().size() != 2 ||
calc_value_map["padding"].toIntList().size() != 2 ||
calc_value_map["dilation"].toIntList().size() != 2) {
return false;
}
return !calc_value_map["transposed"].toBool();
};
auto filter_conv3d = [](const Match& match,
const std::unordered_map<std::string, Value*>& vmap) {
auto calc_value_map = getConvParams(match, vmap);
if (calc_value_map["output_padding"].toIntList().size() != 3 ||
calc_value_map["stride"].toIntList().size() != 3 ||
calc_value_map["padding"].toIntList().size() != 3 ||
calc_value_map["dilation"].toIntList().size() != 3) {
return false;
}
return !calc_value_map["transposed"].toBool();
};
auto filter_conv_transpose1d =
[](const Match& match,
const std::unordered_map<std::string, Value*>& vmap) {
auto calc_value_map = getConvParams(match, vmap);
if (calc_value_map["output_padding"].toIntList().size() != 1 ||
calc_value_map["stride"].toIntList().size() != 1 ||
calc_value_map["padding"].toIntList().size() != 1 ||
calc_value_map["dilation"].toIntList().size() != 1) {
return false;
}
return calc_value_map["transposed"].toBool();
};
auto filter_conv_transpose2d =
[](const Match& match,
const std::unordered_map<std::string, Value*>& vmap) {
auto calc_value_map = getConvParams(match, vmap);
if (calc_value_map["output_padding"].toIntList().size() != 2 ||
calc_value_map["stride"].toIntList().size() != 2 ||
calc_value_map["padding"].toIntList().size() != 2 ||
calc_value_map["dilation"].toIntList().size() != 2) {
return false;
}
return calc_value_map["transposed"].toBool();
};
auto filter_conv_transpose3d =
[](const Match& match,
const std::unordered_map<std::string, Value*>& vmap) {
auto calc_value_map = getConvParams(match, vmap);
if (calc_value_map["output_padding"].toIntList().size() != 3 ||
calc_value_map["stride"].toIntList().size() != 3 ||
calc_value_map["padding"].toIntList().size() != 3 ||
calc_value_map["dilation"].toIntList().size() != 3) {
return false;
}
return calc_value_map["transposed"].toBool();
};
SubgraphRewriter rewriter_conv1d;
rewriter_conv1d.RegisterRewritePattern(convolution, conv1d);
rewriter_conv1d.RegisterRewritePattern(
convolution_deprecated, conv1d_for_deprecated_conv);
rewriter_conv1d.runOnGraph(graph, filter_conv1d);
SubgraphRewriter rewriter_conv2d;
rewriter_conv2d.RegisterRewritePattern(convolution, conv2d);
rewriter_conv2d.RegisterRewritePattern(
convolution_deprecated, conv2d_for_deprecated_conv);
rewriter_conv2d.runOnGraph(graph, filter_conv2d);
SubgraphRewriter rewriter_conv3d;
rewriter_conv3d.RegisterRewritePattern(convolution, conv3d);
rewriter_conv3d.RegisterRewritePattern(
convolution_deprecated, conv3d_for_deprecated_conv);
rewriter_conv3d.runOnGraph(graph, filter_conv3d);
SubgraphRewriter rewriter_conv_transpose1d;
rewriter_conv_transpose1d.RegisterRewritePattern(
convolution, conv_transpose1d);
rewriter_conv_transpose1d.RegisterRewritePattern(
convolution_deprecated, conv_transpose1d_for_deprecated_conv);
rewriter_conv_transpose1d.runOnGraph(graph, filter_conv_transpose1d);
SubgraphRewriter rewriter_conv_transpose2d;
rewriter_conv_transpose2d.RegisterRewritePattern(
convolution, conv_transpose2d);
rewriter_conv_transpose2d.RegisterRewritePattern(
convolution_deprecated, conv_transpose2d_for_deprecated_conv);
rewriter_conv_transpose2d.runOnGraph(graph, filter_conv_transpose2d);
SubgraphRewriter rewriter_conv_transpose3d;
rewriter_conv_transpose3d.RegisterRewritePattern(
convolution, conv_transpose3d);
rewriter_conv_transpose3d.RegisterRewritePattern(
convolution_deprecated, conv_transpose3d_for_deprecated_conv);
rewriter_conv_transpose3d.runOnGraph(graph, filter_conv_transpose3d);
}
bool isClampFusable(
const Match& match,
const std::unordered_map<std::string, Value*>& vmap) {
const auto& match_vmap = match.values_map;
TORCH_CHECK(
vmap.find("dummy_min_max") != vmap.end(),
"Expected to find dummy_min_max Value in the subgraph to be replaced.");
auto dummy_min_max =
graph_rewrite_helper::getIValue("dummy_min_max", match_vmap, vmap);
auto is_fusable = !dummy_min_max || dummy_min_max.value().isNone();
// Also check if the output_min and output_max values are actually constant.
// If hardtanh's min/max Value's are not actually constants, we will end up
// rerouting those values to prepack op. And if they are not constants
// we will not be able to remove prepacking ops.
if (vmap.find("output_min") != vmap.end()) {
// aten::relu pattern does not have output_min/output_max.
// aten::hardtanh/_ does.
TORCH_CHECK(
vmap.find("output_max") != vmap.end(),
"Expected to find output_max as well given "
"output_min exist in pattern graph.");
// If output_min/max are not constant, we get c10::nullopt.
auto output_min =
graph_rewrite_helper::getIValue("output_min", match_vmap, vmap);
auto output_max =
graph_rewrite_helper::getIValue("output_max", match_vmap, vmap);
is_fusable =
is_fusable && (output_min.has_value() && output_max.has_value());
}
return is_fusable;
}
} // namespace graph_rewrite_helper
} // namespace jit
} // namespace torch
|