static */
| 280 | } |
| 281 | |
| 282 | /* static */ StatusOr<Literal> MutableLiteralBase::CreateFromProto( |
| 283 | const LiteralProto& proto, bool prohibit_empty_literal) { |
| 284 | if (!proto.has_shape()) { |
| 285 | return InvalidArgument("LiteralProto has no shape"); |
| 286 | } |
| 287 | Shape shape(proto.shape()); |
| 288 | if (ShapeUtil::HasPrimitiveType(shape, OPAQUE_TYPE)) { |
| 289 | return InvalidArgument( |
| 290 | "Literal shape cannot include OPAQUE_TYPE sub-shape"); |
| 291 | } |
| 292 | if (!LayoutUtil::HasLayout(shape)) { |
| 293 | return InvalidArgument("LiteralProto has no layout"); |
| 294 | } |
| 295 | |
| 296 | TF_RETURN_IF_ERROR(ShapeUtil::ValidateShapeWithOptionalLayout(shape)); |
| 297 | |
| 298 | Literal literal(shape); |
| 299 | |
| 300 | TF_RETURN_IF_ERROR(literal.root_piece_->ForEachMutableSubpieceWithStatus( |
| 301 | [&](const ShapeIndex& index, Piece* piece) { |
| 302 | const LiteralProto* proto_element = &proto; |
| 303 | for (int64 i : index) { |
| 304 | CHECK(i < proto_element->tuple_literals_size()); |
| 305 | proto_element = &proto_element->tuple_literals(i); |
| 306 | } |
| 307 | |
| 308 | if (piece->subshape().IsTuple()) { |
| 309 | if (proto_element->tuple_literals_size() != |
| 310 | ShapeUtil::TupleElementCount(piece->subshape())) { |
| 311 | return InvalidArgument( |
| 312 | "Expected %d tuple elements in LiteralProto, has %d", |
| 313 | ShapeUtil::TupleElementCount(piece->subshape()), |
| 314 | proto_element->tuple_literals_size()); |
| 315 | } |
| 316 | return Status::OK(); |
| 317 | } |
| 318 | if (piece->subshape().element_type() == TOKEN) { |
| 319 | return Status::OK(); |
| 320 | } |
| 321 | |
| 322 | CHECK(piece->subshape().IsArray()); |
| 323 | |
| 324 | // When prohibit_empty_literal is false (allowing literal with no |
| 325 | // values), only copy from proto if the literal proto has values. This |
| 326 | // mode is used for a learned cost model. |
| 327 | if (prohibit_empty_literal || LiteralProtoHasValues(*proto_element)) { |
| 328 | TF_RETURN_IF_ERROR(piece->CopyFromProto(*proto_element)); |
| 329 | } |
| 330 | |
| 331 | return Status::OK(); |
| 332 | })); |
| 333 | |
| 334 | return std::move(literal); |
| 335 | } |
| 336 | |
| 337 | std::vector<Literal> Literal::DecomposeTuple() { |
| 338 | CHECK(shape().IsTuple()); |
nothing calls this directly
no test coverage detected