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 #ifndef TENSORFLOW_COMPILER_XLA_TEXT_LITERAL_READER_H_ 17 #define TENSORFLOW_COMPILER_XLA_TEXT_LITERAL_READER_H_ 18 19 #include <memory> 20 21 #include "tensorflow/compiler/xla/literal_util.h" 22 #include "tensorflow/compiler/xla/statusor.h" 23 #include "tensorflow/compiler/xla/types.h" 24 #include "tensorflow/compiler/xla/xla_data.pb.h" 25 #include "tensorflow/core/lib/core/stringpiece.h" 26 #include "tensorflow/core/platform/env.h" 27 #include "tensorflow/core/platform/macros.h" 28 29 namespace xla { 30 31 // Reads a textual literal from a file path. The format of the file must be: 32 // 33 // f32[1,2,3,4] 34 // (0, 0, 0, 0): 1.234 35 // (0, 0, 0, 1): 0xf00p-2 36 // ... 37 // 38 // Note that for floating values the hex output (as in the second value above) 39 // will more precisely convey the exact values. 40 class TextLiteralReader { 41 public: 42 // See class comment -- reads a file in its entirety (there must be only one 43 // literal in the text file path provided). 44 static StatusOr<std::unique_ptr<Literal>> ReadPath( 45 tensorflow::StringPiece path); 46 47 private: 48 // Ownership of file is transferred. 49 explicit TextLiteralReader(tensorflow::RandomAccessFile* file); 50 51 // Parses a shape string on the first line, followed by lines of values to the 52 // end of the file. 53 StatusOr<std::unique_ptr<Literal>> ReadAllLines(); 54 55 // Owns the file being read 56 std::unique_ptr<tensorflow::RandomAccessFile> file_; 57 58 TF_DISALLOW_COPY_AND_ASSIGN(TextLiteralReader); 59 }; 60 61 } // namespace xla 62 63 #endif // TENSORFLOW_COMPILER_XLA_TEXT_LITERAL_READER_H_ 64