| 361 | } |
| 362 | |
| 363 | void GenEagerPythonOp::HandleGraphMode(const string& function_setup) { |
| 364 | strings::StrAppend(&result_, " # Add nodes to the TensorFlow graph.\n"); |
| 365 | strings::StrAppend(&result_, function_setup); |
| 366 | if (api_def_.visibility() == ApiDef::VISIBLE) { |
| 367 | strings::StrAppend(&result_, " try:\n "); |
| 368 | } |
| 369 | strings::StrAppend(&result_, " _, _, _op = _op_def_lib._apply_op_helper(\n"); |
| 370 | AddBodyNoReturn(strings::StrCat(" \"", op_def_.name(), "\", ")); |
| 371 | AddDispatch(" "); |
| 372 | |
| 373 | if (num_outs_ > 0) { |
| 374 | strings::StrAppend(&result_, " _result = _op.outputs[:]\n"); |
| 375 | // Special case handling for stateful op with single list output |
| 376 | // that might be empty. |
| 377 | if (num_outs_ == 1 && op_def_.is_stateful() && |
| 378 | (!op_def_.output_arg(0).number_attr().empty() || |
| 379 | !op_def_.output_arg(0).type_list_attr().empty())) { |
| 380 | // TODO(josh11b): Can skip this if the number_attr/type_list_attr has |
| 381 | // a constraint indicating that this can never be empty. |
| 382 | strings::StrAppend(&result_, |
| 383 | " if not _result:\n" |
| 384 | " return _op\n"); |
| 385 | } |
| 386 | strings::StrAppend(&result_, " _inputs_flat = _op.inputs\n"); |
| 387 | |
| 388 | // Compute graph-mode attrs. |
| 389 | if (op_def_.attr_size() > 0) { |
| 390 | string attr_values; |
| 391 | for (int i = 0; i < op_def_.attr_size(); ++i) { |
| 392 | if (i > 0) strings::StrAppend(&attr_values, ", "); |
| 393 | const auto& attr_name(op_def_.attr(i).name()); |
| 394 | if (op_def_.attr(i).type() == "type") { |
| 395 | strings::StrAppend(&attr_values, "\"", attr_name, |
| 396 | "\", _op._get_attr_type(\"", attr_name, "\")"); |
| 397 | } else { |
| 398 | strings::StrAppend(&attr_values, "\"", attr_name, |
| 399 | "\", _op.get_attr(\"", attr_name, "\")"); |
| 400 | } |
| 401 | } |
| 402 | strings::StrAppend(&attr_values, ")"); |
| 403 | strings::StrAppend( |
| 404 | &result_, WordWrap(" _attrs = (", attr_values, kRightMargin), "\n"); |
| 405 | } else { |
| 406 | strings::StrAppend(&result_, " _attrs = None\n"); |
| 407 | } |
| 408 | } else { |
| 409 | strings::StrAppend(&result_, " return _op\n"); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | string GenEagerPythonOp::GetEagerNotAllowedError() { |
| 414 | bool eager_allowed = true; |