Requires: attr has already been validated.
| 85 | |
| 86 | // Requires: attr has already been validated. |
| 87 | Status ValidateAttrValue(const AttrValue& attr_value, |
| 88 | const OpDef::AttrDef& attr) { |
| 89 | // Is it a valid value? |
| 90 | TF_RETURN_WITH_CONTEXT_IF_ERROR(AttrValueHasType(attr_value, attr.type()), |
| 91 | " for attr '", attr.name(), "'"); |
| 92 | |
| 93 | // Does the value satisfy the minimum constraint in the AttrDef? |
| 94 | if (attr.has_minimum()) { |
| 95 | if (attr.type() == "int") { |
| 96 | if (attr_value.i() < attr.minimum()) { |
| 97 | return errors::InvalidArgument( |
| 98 | "Value for attr '", attr.name(), "' of ", attr_value.i(), |
| 99 | " must be at least minimum ", attr.minimum()); |
| 100 | } |
| 101 | } else { |
| 102 | int length = -1; |
| 103 | if (attr.type() == "list(string)") { |
| 104 | length = attr_value.list().s_size(); |
| 105 | } else if (attr.type() == "list(int)") { |
| 106 | length = attr_value.list().i_size(); |
| 107 | } else if (attr.type() == "list(float)") { |
| 108 | length = attr_value.list().f_size(); |
| 109 | } else if (attr.type() == "list(bool)") { |
| 110 | length = attr_value.list().b_size(); |
| 111 | } else if (attr.type() == "list(type)") { |
| 112 | length = attr_value.list().type_size(); |
| 113 | } else if (attr.type() == "list(shape)") { |
| 114 | length = attr_value.list().shape_size(); |
| 115 | } else if (attr.type() == "list(tensor)") { |
| 116 | length = attr_value.list().tensor_size(); |
| 117 | } else if (attr.type() == "list(func)") { |
| 118 | length = attr_value.list().func_size(); |
| 119 | } |
| 120 | if (length < attr.minimum()) { |
| 121 | return errors::InvalidArgument( |
| 122 | "Length for attr '", attr.name(), "' of ", length, |
| 123 | " must be at least minimum ", attr.minimum()); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Does the value satisfy the allowed_value constraint in the AttrDef? |
| 129 | if (attr.has_allowed_values()) { |
| 130 | if (attr.type() == "type") { |
| 131 | TF_RETURN_IF_ERROR(AllowedTypeValue(attr_value.type(), attr)); |
| 132 | } else if (attr.type() == "list(type)") { |
| 133 | for (int dt : attr_value.list().type()) { |
| 134 | TF_RETURN_IF_ERROR(AllowedTypeValue(static_cast<DataType>(dt), attr)); |
| 135 | } |
| 136 | } else if (attr.type() == "string") { |
| 137 | TF_RETURN_IF_ERROR(AllowedStringValue(attr_value.s(), attr)); |
| 138 | } else if (attr.type() == "list(string)") { |
| 139 | for (const string& str : attr_value.list().s()) { |
| 140 | TF_RETURN_IF_ERROR(AllowedStringValue(str, attr)); |
| 141 | } |
| 142 | } else { |
| 143 | return errors::Unimplemented( |
| 144 | "Support for allowed_values not implemented for type ", attr.type()); |
no test coverage detected