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