• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/text_literal_reader.h"
17 
18 #include <limits>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 #include "absl/memory/memory.h"
24 #include "absl/strings/match.h"
25 #include "absl/strings/numbers.h"
26 #include "absl/strings/str_split.h"
27 #include "absl/strings/string_view.h"
28 #include "absl/strings/strip.h"
29 #include "tensorflow/compiler/xla/literal.h"
30 #include "tensorflow/compiler/xla/service/hlo_parser.h"
31 #include "tensorflow/compiler/xla/shape_util.h"
32 #include "tensorflow/compiler/xla/status_macros.h"
33 #include "tensorflow/compiler/xla/types.h"
34 #include "tensorflow/compiler/xla/util.h"
35 #include "tensorflow/compiler/xla/xla_data.pb.h"
36 #include "tensorflow/core/lib/io/buffered_inputstream.h"
37 #include "tensorflow/core/lib/io/random_inputstream.h"
38 #include "tensorflow/core/platform/protobuf.h"
39 #include "tensorflow/core/platform/types.h"
40 
41 namespace xla {
42 
ReadPath(absl::string_view path)43 StatusOr<Literal> TextLiteralReader::ReadPath(absl::string_view path) {
44   CHECK(!absl::EndsWith(path, ".gz"))
45       << "TextLiteralReader no longer supports reading .gz files";
46   std::unique_ptr<tensorflow::RandomAccessFile> file;
47   Status s =
48       tensorflow::Env::Default()->NewRandomAccessFile(std::string(path), &file);
49   if (!s.ok()) {
50     return s;
51   }
52 
53   TextLiteralReader reader(file.release());
54   return reader.ReadAllLines();
55 }
56 
TextLiteralReader(tensorflow::RandomAccessFile * file)57 TextLiteralReader::TextLiteralReader(tensorflow::RandomAccessFile* file)
58     : file_(file) {}
59 
ReadAllLines()60 StatusOr<Literal> TextLiteralReader::ReadAllLines() {
61   tensorflow::io::RandomAccessInputStream stream(file_.get());
62   tensorflow::io::BufferedInputStream buf(&stream, 65536);
63   string shape_string;
64   Status s = buf.ReadLine(&shape_string);
65   if (!s.ok()) {
66     return s;
67   }
68 
69   absl::StripAsciiWhitespace(&shape_string);
70   TF_ASSIGN_OR_RETURN(Shape shape, ParseShape(shape_string));
71   if (shape.element_type() != F32) {
72     return Unimplemented(
73         "unsupported element type for text literal reading: %s",
74         ShapeUtil::HumanString(shape));
75   }
76 
77   Literal result(shape);
78   const float fill = std::numeric_limits<float>::quiet_NaN();
79   result.PopulateWithValue<float>(fill);
80   std::vector<absl::string_view> pieces;
81   std::vector<absl::string_view> coordinates;
82   std::vector<int64> coordinate_values;
83   string line;
84   while (buf.ReadLine(&line).ok()) {
85     pieces = absl::StrSplit(line, ':');
86     absl::string_view coordinates_string =
87         absl::StripAsciiWhitespace(pieces[0]);
88     absl::string_view value_string = absl::StripAsciiWhitespace(pieces[1]);
89     if (!absl::ConsumePrefix(&coordinates_string, "(")) {
90       return InvalidArgument(
91           "expected '(' at the beginning of coordinates: \"%s\"", line);
92     }
93     if (!absl::ConsumeSuffix(&coordinates_string, ")")) {
94       return InvalidArgument("expected ')' at the end of coordinates: \"%s\"",
95                              line);
96     }
97     float value;
98     if (!absl::SimpleAtof(value_string, &value)) {
99       return InvalidArgument("could not parse value as float: \"%s\"",
100                              value_string);
101     }
102     coordinates = absl::StrSplit(coordinates_string, ',');
103     coordinate_values.clear();
104     for (absl::string_view piece : coordinates) {
105       int64 coordinate_value;
106       if (!absl::SimpleAtoi(piece, &coordinate_value)) {
107         return InvalidArgument(
108             "could not parse coordinate member as int64: \"%s\"",
109             std::string(piece));
110       }
111       coordinate_values.push_back(coordinate_value);
112     }
113     if (coordinate_values.size() != shape.dimensions_size()) {
114       return InvalidArgument(
115           "line did not have expected number of coordinates; want %d got %u: "
116           "\"%s\"",
117           shape.dimensions_size(), coordinate_values.size(), line);
118     }
119     result.Set<float>(coordinate_values, value);
120   }
121   return std::move(result);
122 }
123 
124 }  // namespace xla
125