| 40 | PackedLiteralReader::~PackedLiteralReader() { delete file_; } |
| 41 | |
| 42 | StatusOr<Literal> PackedLiteralReader::Read(const Shape& shape, |
| 43 | const Layout* layout) { |
| 44 | VLOG(3) << "reading shape from file: " << ShapeUtil::HumanString(shape) |
| 45 | << " layout: " << (layout == nullptr ? "<none>" : layout->ToString()); |
| 46 | Shape literal_shape = shape; |
| 47 | if (layout != nullptr) { |
| 48 | TF_RETURN_IF_ERROR( |
| 49 | LayoutUtil::ValidateLayoutForShape(*layout, literal_shape)); |
| 50 | *literal_shape.mutable_layout() = *layout; |
| 51 | } |
| 52 | |
| 53 | if (shape.element_type() != F32) { |
| 54 | return Unimplemented( |
| 55 | "not yet implemented element type for packed literal reading: %s", |
| 56 | PrimitiveType_Name(shape.element_type())); |
| 57 | } |
| 58 | |
| 59 | Literal result(literal_shape); |
| 60 | result.PopulateWithValue(std::numeric_limits<float>::quiet_NaN()); |
| 61 | |
| 62 | int64 elements = ShapeUtil::ElementsIn(shape); |
| 63 | absl::Span<const float> field = result.data<float>(); |
| 64 | char* data = absl::bit_cast<char*>(field.data()); |
| 65 | uint64 bytes = elements * sizeof(float); |
| 66 | absl::string_view sp; |
| 67 | auto s = file_->Read(offset_, bytes, &sp, data); |
| 68 | offset_ += sp.size(); |
| 69 | if (!s.ok()) { |
| 70 | return s; |
| 71 | } else { |
| 72 | // Success: make sure we move the data into the right place if the Read |
| 73 | // call decided to return data in somewhere other than "data". |
| 74 | CHECK_EQ(sp.size(), bytes); |
| 75 | if (sp.data() != data) { |
| 76 | memcpy(data, sp.data(), sp.size()); |
| 77 | } |
| 78 | } |
| 79 | VLOG(3) << "read shape from file: " << ShapeUtil::HumanString(shape); |
| 80 | return std::move(result); |
| 81 | } |
| 82 | |
| 83 | bool PackedLiteralReader::IsExhausted() const { |
| 84 | // Try to read a single byte from offset_. If we can't, we've |