| 423 | } |
| 424 | |
| 425 | Status BundleWriter::Add(StringPiece key, const Tensor& val) { |
| 426 | if (!status_.ok()) return status_; |
| 427 | CHECK_NE(key, kHeaderEntryKey); |
| 428 | const string key_string(key); |
| 429 | if (entries_.find(key_string) != entries_.end()) { |
| 430 | status_ = errors::InvalidArgument("Adding duplicate key: ", key); |
| 431 | return status_; |
| 432 | } |
| 433 | |
| 434 | BundleEntryProto* entry = &entries_[key_string]; |
| 435 | entry->set_dtype(val.dtype()); |
| 436 | val.shape().AsProto(entry->mutable_shape()); |
| 437 | entry->set_shard_id(0); |
| 438 | entry->set_offset(size_); |
| 439 | |
| 440 | // Updates the data file. |
| 441 | size_t data_bytes_written = 0; |
| 442 | uint32 crc32c = 0; |
| 443 | out_->clear_crc32c(); |
| 444 | if (val.dtype() == DT_STRING) { |
| 445 | status_ = WriteStringTensor(val, out_.get(), &data_bytes_written, &crc32c); |
| 446 | } else if (val.dtype() == DT_VARIANT) { |
| 447 | status_ = WriteVariantTensor(val, out_.get(), &data_bytes_written, &crc32c); |
| 448 | } else { |
| 449 | status_ = WriteTensor(val, out_.get(), &data_bytes_written); |
| 450 | crc32c = out_->crc32c(); |
| 451 | } |
| 452 | |
| 453 | if (status_.ok()) { |
| 454 | entry->set_size(data_bytes_written); |
| 455 | entry->set_crc32c(crc32c::Mask(crc32c)); |
| 456 | size_ += data_bytes_written; |
| 457 | status_ = PadAlignment(out_.get(), options_.data_alignment, &size_); |
| 458 | } |
| 459 | return status_; |
| 460 | } |
| 461 | |
| 462 | Status BundleWriter::AddSlice(StringPiece full_tensor_key, |
| 463 | const TensorShape& full_tensor_shape, |