| 22 | namespace xla { |
| 23 | |
| 24 | Shape::Shape(const ShapeProto& shape_proto) { |
| 25 | set_element_type(shape_proto.element_type()); |
| 26 | dimensions_.reserve(shape_proto.dimensions_size()); |
| 27 | for (const int64 dimension : shape_proto.dimensions()) { |
| 28 | add_dimensions(dimension); |
| 29 | } |
| 30 | // A malformed proto may have different is_dynamic_dimension_size and |
| 31 | // dimensions_size. Since C++ is evil, and we have no good way of bailing out |
| 32 | // in a constructor, conservatively trim the is_dynamic_dimension size. |
| 33 | // TODO(b/120111794): Make this a hard error when we have a factory method |
| 34 | // instead of a constructor. |
| 35 | if (shape_proto.dimensions_size() != |
| 36 | shape_proto.is_dynamic_dimension_size()) { |
| 37 | if (shape_proto.is_dynamic_dimension_size() != 0) { |
| 38 | LOG(ERROR) << "Malformed shape proto: number of is_dynamic_dimension " |
| 39 | "fields does not match number of dimension fields"; |
| 40 | } else { |
| 41 | LOG(WARNING) << "Malformed shape proto: is_dynamic_dimension is empty"; |
| 42 | } |
| 43 | } |
| 44 | int64 num_dynamic_dimension_fields = std::min( |
| 45 | shape_proto.dimensions_size(), shape_proto.is_dynamic_dimension_size()); |
| 46 | for (int i = 0; i < num_dynamic_dimension_fields; i++) { |
| 47 | dynamic_dimensions_[i] = shape_proto.is_dynamic_dimension(i); |
| 48 | } |
| 49 | tuple_shapes_.reserve(shape_proto.tuple_shapes_size()); |
| 50 | for (const ShapeProto& element_shape : shape_proto.tuple_shapes()) { |
| 51 | *add_tuple_shapes() = Shape(element_shape); |
| 52 | } |
| 53 | if (shape_proto.has_layout()) { |
| 54 | *mutable_layout() = Layout::CreateFromProto(shape_proto.layout()); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | ShapeProto Shape::ToProto() const { |
| 59 | ShapeProto proto; |
nothing calls this directly
no test coverage detected