| 1361 | } |
| 1362 | |
| 1363 | std::unordered_map<instruction_ref, std::string> |
| 1364 | module::print_py(std::ostream& os, |
| 1365 | const std::string& mname, |
| 1366 | std::unordered_map<instruction_ref, std::string> names) const |
| 1367 | { |
| 1368 | // cppcheck-suppress variableScope |
| 1369 | unsigned long seed = names.size(); |
| 1370 | auto last = std::prev(this->end()); |
| 1371 | names = this->print( |
| 1372 | [&](auto ins, auto ins_names) { |
| 1373 | std::vector<std::string> input_vars; |
| 1374 | std::transform(ins->inputs().begin(), |
| 1375 | ins->inputs().end(), |
| 1376 | std::back_inserter(input_vars), |
| 1377 | [&](auto input) { return cpp_var_name(ins_names.at(input)); }); |
| 1378 | if(ins != last) |
| 1379 | os << cpp_var_name(ins_names.at(ins)) << " = "; |
| 1380 | if(ins->name() == "@literal") |
| 1381 | { |
| 1382 | os << mname << ".add_literal("; |
| 1383 | if(ins->get_shape().elements() < 1024) |
| 1384 | { |
| 1385 | os << "migraphx.create_argument("; |
| 1386 | print_py_shape(os, ins->get_shape()); |
| 1387 | os << ", [" << ins->get_literal() << "])"; |
| 1388 | } |
| 1389 | else |
| 1390 | { |
| 1391 | const bool use_abs = false; |
| 1392 | // Disable abs for now |
| 1393 | // ins->get_literal().visit([&](auto v) { |
| 1394 | // use_abs = std::none_of(v.begin(), v.end(), [](auto x) { return x < 0; }); |
| 1395 | // }); |
| 1396 | if(use_abs) |
| 1397 | os << "migraphx.abs_literal("; |
| 1398 | os << "migraphx.generate_argument("; |
| 1399 | print_py_shape(os, ins->get_shape()); |
| 1400 | os << ", " << seed << ")"; |
| 1401 | if(use_abs) |
| 1402 | os << ")"; |
| 1403 | seed++; |
| 1404 | } |
| 1405 | os << ")" << std::endl; |
| 1406 | } |
| 1407 | else if(ins->name() == "@param") |
| 1408 | { |
| 1409 | std::string name = any_cast<builtin::param>(ins->get_operator()).parameter; |
| 1410 | os << mname << ".add_parameter(" << enclose_name(name) << ", "; |
| 1411 | print_py_shape(os, ins->get_shape()); |
| 1412 | os << ")" << std::endl; |
| 1413 | } |
| 1414 | else if(ins->name() == "@return") |
| 1415 | { |
| 1416 | os << mname << ".add_return([" << join_strings(input_vars, ", ") << "])" |
| 1417 | << std::endl; |
| 1418 | } |
| 1419 | else |
| 1420 | { |
no test coverage detected