| 447 | } |
| 448 | |
| 449 | bool GenEagerPythonOp::GetEagerFunctionSetup(const string& indentation, |
| 450 | string* function_setup) { |
| 451 | // Validate list inputs, infer length attrs. |
| 452 | for (int i = 0; i < op_def_.attr_size(); ++i) { |
| 453 | const auto& attr(op_def_.attr(i)); |
| 454 | if (attr.type() == "int") { |
| 455 | auto arg_list = attr_to_args_.find(attr.name()); |
| 456 | if (arg_list != attr_to_args_.end()) { |
| 457 | // Inferred int attrs are the lengths of inputs. Validate those |
| 458 | // inputs are lists and have the same length. |
| 459 | for (auto iter = arg_list->second.begin(); |
| 460 | iter != arg_list->second.end(); ++iter) { |
| 461 | const string& arg_api_name = param_names_[*iter].GetRenameTo(); |
| 462 | ExpectListArg(indentation, arg_api_name, function_setup); |
| 463 | if (iter == arg_list->second.begin()) { |
| 464 | AddInferredAttr(indentation, attr.name(), |
| 465 | strings::StrCat("len(", arg_api_name, ")"), |
| 466 | function_setup, &attr_expressions_); |
| 467 | } else { |
| 468 | const auto& attr_var = attr_expressions_[attr.name()]; |
| 469 | strings::StrAppend( |
| 470 | function_setup, indentation, "if len(", arg_api_name, |
| 471 | ") != ", attr_var, ":\n", indentation, " raise ValueError(\n", |
| 472 | indentation, " \"List argument '", arg_api_name, "' to '", |
| 473 | op_name_, "' Op with length %d \"\n", indentation, |
| 474 | " \"must match length %d of argument '", |
| 475 | inferred_attrs_[attr.name()], "'.\" %\n", indentation, |
| 476 | " (len(", arg_api_name, "), ", attr_var, "))\n"); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | for (int i = 0; i < attrs_.size(); ++i) { |
| 484 | const string& attr_name = attrs_[i]; |
| 485 | const auto& param = param_names_[i + op_def_.input_arg_size()]; |
| 486 | const auto& attr = *FindAttr(attr_name, op_def_); |
| 487 | const string& attr_api_name = param.GetRenameTo(); |
| 488 | StringPiece attr_type = attr.type(); |
| 489 | attr_expressions_[attr_name] = attr_api_name; |
| 490 | const int default_index = i - (attrs_.size() - params_with_default_.size()); |
| 491 | if (default_index >= 0) { |
| 492 | const string& default_value = params_with_default_[default_index].second; |
| 493 | strings::StrAppend(function_setup, indentation, "if ", attr_api_name, |
| 494 | " is None:\n"); |
| 495 | strings::StrAppend(function_setup, indentation, " ", attr_api_name, |
| 496 | " = ", default_value, "\n"); |
| 497 | } |
| 498 | if (absl::StartsWith(attr_type, "list(")) { |
| 499 | ExpectListArg(indentation, attr_api_name, function_setup); |
| 500 | } |
| 501 | |
| 502 | if (attr_type == "string") { |
| 503 | strings::StrAppend(function_setup, indentation, attr_api_name, |
| 504 | " = _execute.make_str(", attr_api_name, ", \"", |
| 505 | attr_api_name, "\")\n"); |
| 506 | } else if (attr_type == "list(string)") { |
nothing calls this directly
no test coverage detected