Home | History | Annotate | Download | only in xla
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/compiler/xla/packed_literal_reader.h"
     17 
     18 #include <limits>
     19 #include <string>
     20 #include <utility>
     21 
     22 #include "tensorflow/compiler/xla/layout_util.h"
     23 #include "tensorflow/compiler/xla/literal_util.h"
     24 #include "tensorflow/compiler/xla/ptr_util.h"
     25 #include "tensorflow/compiler/xla/shape_util.h"
     26 #include "tensorflow/compiler/xla/status_macros.h"
     27 #include "tensorflow/compiler/xla/types.h"
     28 #include "tensorflow/compiler/xla/util.h"
     29 #include "tensorflow/compiler/xla/xla_data.pb.h"
     30 #include "tensorflow/core/lib/core/casts.h"
     31 #include "tensorflow/core/platform/logging.h"
     32 #include "tensorflow/core/platform/protobuf.h"
     33 #include "tensorflow/core/platform/types.h"
     34 
     35 namespace xla {
     36 
     37 PackedLiteralReader::PackedLiteralReader(tensorflow::RandomAccessFile* file)
     38     : file_(file), offset_(0) {}
     39 
     40 PackedLiteralReader::~PackedLiteralReader() { delete file_; }
     41 
     42 StatusOr<std::unique_ptr<Literal>> PackedLiteralReader::Read(
     43     const Shape& shape, const Layout* layout) {
     44   VLOG(3) << "reading shape from file: " << ShapeUtil::HumanString(shape)
     45           << " layout: "
     46           << (layout == nullptr ? "<none>" : layout->ShortDebugString());
     47   Shape literal_shape = shape;
     48   if (layout != nullptr) {
     49     TF_RETURN_IF_ERROR(
     50         LayoutUtil::ValidateLayoutForShape(*layout, literal_shape));
     51     *literal_shape.mutable_layout() = *layout;
     52   }
     53 
     54   if (shape.element_type() != F32) {
     55     return Unimplemented(
     56         "not yet implemented element type for packed literal reading: %s",
     57         PrimitiveType_Name(shape.element_type()).c_str());
     58   }
     59 
     60   auto result = MakeUnique<Literal>(literal_shape);
     61   result->PopulateWithValue(std::numeric_limits<float>::quiet_NaN());
     62 
     63   int64 elements = ShapeUtil::ElementsIn(shape);
     64   tensorflow::gtl::ArraySlice<float> field = result->data<float>();
     65   char* data = tensorflow::bit_cast<char*>(field.data());
     66   uint64 bytes = elements * sizeof(float);
     67   tensorflow::StringPiece sp;
     68   auto s = file_->Read(offset_, bytes, &sp, data);
     69   offset_ += sp.size();
     70   if (!s.ok()) {
     71     return s;
     72   } else {
     73     // Success: make sure we move the data into the right place if the Read
     74     // call decided to return data in somewhere other than "data".
     75     CHECK_EQ(sp.size(), bytes);
     76     if (sp.data() != data) {
     77       memcpy(data, sp.data(), sp.size());
     78     }
     79   }
     80   VLOG(3) << "read shape from file: " << ShapeUtil::HumanString(shape);
     81   return std::move(result);
     82 }
     83 
     84 bool PackedLiteralReader::IsExhausted() const {
     85   // Try to read a single byte from offset_.  If we can't, we've
     86   // exhausted the data.
     87   char single_byte[1];
     88   tensorflow::StringPiece sp;
     89   auto s = file_->Read(offset_, sizeof(single_byte), &sp, single_byte);
     90   return !s.ok();
     91 }
     92 
     93 }  // namespace xla
     94