|
1 | 1 | #include "core/lowering/passes/passes.h"
|
2 | 2 | #include "gtest/gtest.h"
|
| 3 | +#include "tests/util/util.h" |
3 | 4 | #include "torch/csrc/jit/ir/irparser.h"
|
| 5 | +#include "torch/csrc/jit/passes/canonicalize.h" |
| 6 | +#include "torch/csrc/jit/passes/constant_pooling.h" |
| 7 | +#include "torch/csrc/jit/passes/remove_exceptions.h" |
4 | 8 |
|
5 | 9 | TEST(LoweringPasses, EliminateExceptionOrPassPattern_Block0) {
|
6 | 10 | // parseIR does not support " = prim::If(%51)" with no return value
|
@@ -169,3 +173,217 @@ TEST(LoweringPasses, EliminateExceptionOrPassPattern_Negative) {
|
169 | 173 | }
|
170 | 174 | EXPECT_EQ(1, if_count);
|
171 | 175 | }
|
| 176 | + |
| 177 | +TEST(LoweringPasses, EliminateExceptionsSafeIfBlock) { |
| 178 | + /*std::string source_graph = R"IR( |
| 179 | + graph(%x, %y): |
| 180 | + %dim : int = aten::dim(%x) |
| 181 | + %48 : int = prim::Constant[value=2]() |
| 182 | + %66 : bool = aten::eq(%48, %dim) |
| 183 | + %45 : str = prim::Constant[value="EXCEPTION"]() |
| 184 | + %4 : Tensor = prim::If(%66) |
| 185 | + block0(): |
| 186 | + = prim::RaiseException(%45) |
| 187 | + -> (%x) |
| 188 | + block1(): |
| 189 | + %res = aten::mul(%x, %y) |
| 190 | + -> (%res) |
| 191 | + return (%4))IR";*/ |
| 192 | + |
| 193 | + std::string target_graph = R"IR( |
| 194 | + graph(%x : Tensor, |
| 195 | + %y : Tensor): |
| 196 | + %6 : Tensor = aten::mul(%x, %y) |
| 197 | + return (%6))IR"; |
| 198 | + |
| 199 | + // Construct graph via manual commands, to avoid IR parsing issues with |
| 200 | + // unassigned variables (such as prim::RaiseException) |
| 201 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 202 | + auto x = g->insertInput(0, "x"); |
| 203 | + auto y = g->insertInput(1, "y"); |
| 204 | + auto none_const_val = g->insertConstant(torch::jit::IValue()); |
| 205 | + auto two_const_val = g->insertConstant(torch::jit::IValue(2)); |
| 206 | + auto x_dims = g->create(torch::jit::aten::dim, {x}, 1); |
| 207 | + g->appendNode(x_dims); |
| 208 | + x_dims->output()->setType(torch::jit::IntType::get()); |
| 209 | + auto eq = g->create(torch::jit::aten::eq, {two_const_val, x_dims->output()}, 1); |
| 210 | + g->appendNode(eq); |
| 211 | + eq->output()->setType(torch::jit::BoolType::get()); |
| 212 | + torch::jit::IValue except("EXCEPTION"); |
| 213 | + auto except_val = g->insertConstant(except); |
| 214 | + |
| 215 | + auto if_node = g->create(torch::jit::prim::If, {eq->output()}, 1); |
| 216 | + auto if_block0 = if_node->addBlock(); |
| 217 | + auto exception_node = g->create(torch::jit::prim::RaiseException, {except_val, none_const_val}, 0); |
| 218 | + if_block0->appendNode(exception_node); |
| 219 | + if_block0->registerOutput(x); |
| 220 | + |
| 221 | + auto if_block1 = if_node->addBlock(); |
| 222 | + auto sum_node = g->create(torch::jit::aten::mul, {x, y}, 1); |
| 223 | + if_block1->appendNode(sum_node); |
| 224 | + if_block1->registerOutput(sum_node->output()); |
| 225 | + |
| 226 | + g->insertNode(if_node); |
| 227 | + g->registerOutput(if_node->output()); |
| 228 | + |
| 229 | + // Apply lowering pass and canonicalization to the graph |
| 230 | + torch_tensorrt::core::lowering::passes::EliminateExceptionsSafe(g); |
| 231 | + g = torch::jit::Canonicalize(g, false); |
| 232 | + |
| 233 | + auto tg = std::make_shared<torch::jit::Graph>(); |
| 234 | + torch::jit::parseIR(target_graph, tg.get()); |
| 235 | + |
| 236 | + torch::jit::ConstantPooling(tg); |
| 237 | + tg = torch::jit::Canonicalize(tg, false); |
| 238 | + |
| 239 | + // Validate identical graphs after pooling constants and canonicalizing |
| 240 | + ASSERT_TRUE((tg->toString() == g->toString())); |
| 241 | +} |
| 242 | + |
| 243 | +TEST(LoweringPasses, EliminateExceptionsSafeElseBlock) { |
| 244 | + /*std::string source_graph = R"IR( |
| 245 | + graph(%x, %y): |
| 246 | + %dim : int = aten::dim(%x) |
| 247 | + %48 : int = prim::Constant[value=2]() |
| 248 | + %66 : bool = aten::eq(%48, %dim) |
| 249 | + %45 : str = prim::Constant[value="EXCEPTION"]() |
| 250 | + %4 : Tensor = prim::If(%66) |
| 251 | + block0(): |
| 252 | + %res = aten::matmul(%x, %y) |
| 253 | + -> (%res) |
| 254 | + block1(): |
| 255 | + = prim::RaiseException(%45) |
| 256 | + -> (%x) |
| 257 | + return (%4))IR";*/ |
| 258 | + |
| 259 | + std::string target_graph = R"IR( |
| 260 | + graph(%x : Tensor, |
| 261 | + %y : Tensor): |
| 262 | + %6 : Tensor = aten::matmul(%x, %y) |
| 263 | + return (%6))IR"; |
| 264 | + |
| 265 | + // Construct graph via manual commands, to avoid IR parsing issues with |
| 266 | + // unassigned variables (such as prim::RaiseException) |
| 267 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 268 | + auto x = g->insertInput(0, "x"); |
| 269 | + auto y = g->insertInput(1, "y"); |
| 270 | + auto none_const_val = g->insertConstant(torch::jit::IValue()); |
| 271 | + auto two_const_val = g->insertConstant(torch::jit::IValue(2)); |
| 272 | + auto x_dims = g->create(torch::jit::aten::dim, {x}, 1); |
| 273 | + g->appendNode(x_dims); |
| 274 | + x_dims->output()->setType(torch::jit::IntType::get()); |
| 275 | + auto eq = g->create(torch::jit::aten::eq, {two_const_val, x_dims->output()}, 1); |
| 276 | + g->appendNode(eq); |
| 277 | + eq->output()->setType(torch::jit::BoolType::get()); |
| 278 | + torch::jit::IValue except("EXCEPTION"); |
| 279 | + auto except_val = g->insertConstant(except); |
| 280 | + |
| 281 | + auto if_node = g->create(torch::jit::prim::If, {eq->output()}, 1); |
| 282 | + auto if_block0 = if_node->addBlock(); |
| 283 | + auto sum_node = g->create(torch::jit::aten::matmul, {x, y}, 1); |
| 284 | + if_block0->appendNode(sum_node); |
| 285 | + if_block0->registerOutput(sum_node->output()); |
| 286 | + |
| 287 | + auto if_block1 = if_node->addBlock(); |
| 288 | + auto exception_node = g->create(torch::jit::prim::RaiseException, {except_val, none_const_val}, 0); |
| 289 | + if_block1->appendNode(exception_node); |
| 290 | + if_block1->registerOutput(x); |
| 291 | + |
| 292 | + g->insertNode(if_node); |
| 293 | + g->registerOutput(if_node->output()); |
| 294 | + |
| 295 | + // Apply lowering pass and canonicalization to the graph |
| 296 | + torch_tensorrt::core::lowering::passes::EliminateExceptionsSafe(g); |
| 297 | + g = torch::jit::Canonicalize(g, false); |
| 298 | + |
| 299 | + auto tg = std::make_shared<torch::jit::Graph>(); |
| 300 | + torch::jit::parseIR(target_graph, tg.get()); |
| 301 | + |
| 302 | + torch::jit::ConstantPooling(tg); |
| 303 | + tg = torch::jit::Canonicalize(tg, false); |
| 304 | + |
| 305 | + // Validate identical graphs after pooling constants and canonicalizing |
| 306 | + ASSERT_TRUE((tg->toString() == g->toString())); |
| 307 | +} |
| 308 | + |
| 309 | +TEST(LoweringPasses, EliminateExceptionsSafeNestedIfBlock) { |
| 310 | + /*std::string source_graph = R"IR( |
| 311 | + graph(%x, %y): |
| 312 | + %false : bool = prim::Constant[value=0]() |
| 313 | + %dim : int = aten::dim(%x) |
| 314 | + %48 : int = prim::Constant[value=2]() |
| 315 | + %66 : bool = aten::eq(%48, %dim) |
| 316 | + %45 : str = prim::Constant[value="EXCEPTION"]() |
| 317 | + %4 : Tensor = prim::If(%66) |
| 318 | + block0(): |
| 319 | + %45 : str = prim::Constant[value="EXCEPTION"]() |
| 320 | + = prim::If(%false) |
| 321 | + block0(): |
| 322 | + -> () |
| 323 | + block1(): |
| 324 | + = prim::RaiseException(%45) |
| 325 | + -> () |
| 326 | + = prim::RaiseException(%45) |
| 327 | + -> (%x) |
| 328 | + block1(): |
| 329 | + %res = aten::mul(%x, %y) |
| 330 | + -> (%res) |
| 331 | + return (%4))IR";*/ |
| 332 | + |
| 333 | + std::string target_graph = R"IR( |
| 334 | + graph(%x : Tensor, |
| 335 | + %y : Tensor): |
| 336 | + %6 : Tensor = aten::mul(%x, %y) |
| 337 | + return (%6))IR"; |
| 338 | + |
| 339 | + // Construct graph via manual commands, to avoid IR parsing issues with |
| 340 | + // unassigned variables (such as prim::RaiseException) |
| 341 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 342 | + auto x = g->insertInput(0, "x"); |
| 343 | + auto y = g->insertInput(1, "y"); |
| 344 | + auto none_const_val = g->insertConstant(torch::jit::IValue()); |
| 345 | + auto false_const_val = g->insertConstant(torch::jit::IValue(false)); |
| 346 | + auto two_const_val = g->insertConstant(torch::jit::IValue(2)); |
| 347 | + auto x_dims = g->create(torch::jit::aten::dim, {x}, 1); |
| 348 | + g->appendNode(x_dims); |
| 349 | + x_dims->output()->setType(torch::jit::IntType::get()); |
| 350 | + auto eq = g->create(torch::jit::aten::eq, {two_const_val, x_dims->output()}, 1); |
| 351 | + g->appendNode(eq); |
| 352 | + eq->output()->setType(torch::jit::BoolType::get()); |
| 353 | + torch::jit::IValue except("EXCEPTION"); |
| 354 | + auto except_val = g->insertConstant(except); |
| 355 | + |
| 356 | + // Construct nested-If substructure in graph |
| 357 | + auto if_node = g->create(torch::jit::prim::If, {eq->output()}, 1); |
| 358 | + auto if_block0 = if_node->addBlock(); |
| 359 | + auto if_if_node = g->create(torch::jit::prim::If, {false_const_val}, 0); |
| 360 | + if_block0->appendNode(if_if_node); |
| 361 | + /* auto if_if_block0 = */ if_if_node->addBlock(); |
| 362 | + auto if_if_block1 = if_if_node->addBlock(); |
| 363 | + auto exception_node = g->create(torch::jit::prim::RaiseException, {except_val, none_const_val}, 0); |
| 364 | + if_if_block1->appendNode(exception_node); |
| 365 | + auto exception_node_2 = g->create(torch::jit::prim::RaiseException, {except_val, none_const_val}, 0); |
| 366 | + if_block0->appendNode(exception_node_2); |
| 367 | + if_block0->registerOutput(x); |
| 368 | + |
| 369 | + auto if_block1 = if_node->addBlock(); |
| 370 | + auto sum_node = g->create(torch::jit::aten::mul, {x, y}, 1); |
| 371 | + if_block1->appendNode(sum_node); |
| 372 | + if_block1->registerOutput(sum_node->output()); |
| 373 | + |
| 374 | + g->insertNode(if_node); |
| 375 | + g->registerOutput(if_node->output()); |
| 376 | + |
| 377 | + // Apply lowering pass and canonicalization to the graph |
| 378 | + torch_tensorrt::core::lowering::passes::EliminateExceptionsSafe(g); |
| 379 | + g = torch::jit::Canonicalize(g, false); |
| 380 | + |
| 381 | + auto tg = std::make_shared<torch::jit::Graph>(); |
| 382 | + torch::jit::parseIR(target_graph, tg.get()); |
| 383 | + |
| 384 | + torch::jit::ConstantPooling(tg); |
| 385 | + tg = torch::jit::Canonicalize(tg, false); |
| 386 | + |
| 387 | + // Validate identical graphs after pooling constants and canonicalizing |
| 388 | + ASSERT_TRUE((tg->toString() == g->toString())); |
| 389 | +} |
0 commit comments