Home | History | Annotate | Download | only in tools
      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 <stdio.h>
     17 #include <string>
     18 #include <vector>
     19 
     20 #include "tensorflow/compiler/xla/types.h"
     21 #include "tensorflow/core/lib/core/casts.h"
     22 #include "tensorflow/core/lib/core/status.h"
     23 #include "tensorflow/core/lib/core/stringpiece.h"
     24 #include "tensorflow/core/lib/io/buffered_inputstream.h"
     25 #include "tensorflow/core/lib/io/random_inputstream.h"
     26 #include "tensorflow/core/platform/env.h"
     27 #include "tensorflow/core/platform/init_main.h"
     28 #include "tensorflow/core/platform/logging.h"
     29 #include "tensorflow/core/util/command_line_flags.h"
     30 
     31 using xla::string;
     32 
     33 int main(int argc, char** argv) {
     34   // Flags
     35   string input_file = "";
     36   string output_file = "";
     37   const std::vector<tensorflow::Flag> flag_list = {
     38       tensorflow::Flag("input_file", &input_file, "file to convert"),
     39       tensorflow::Flag("output_file", &output_file, "converted file"),
     40   };
     41   string usage = tensorflow::Flags::Usage(argv[0], flag_list);
     42   bool parse_ok = tensorflow::Flags::Parse(&argc, argv, flag_list);
     43   tensorflow::port::InitMain(argv[0], &argc, &argv);
     44   if (argc != 1 || !parse_ok) {
     45     LOG(QFATAL) << usage;
     46   }
     47 
     48   if (input_file.empty()) {
     49     LOG(QFATAL) << "--input_file is required";
     50   }
     51   if (output_file.empty()) {
     52     LOG(QFATAL) << "--output_file is required";
     53   }
     54 
     55   std::unique_ptr<tensorflow::RandomAccessFile> file;
     56   TF_CHECK_OK(
     57       tensorflow::Env::Default()->NewRandomAccessFile(input_file, &file));
     58 
     59   std::vector<float> floats;
     60   string line;
     61   tensorflow::io::RandomAccessInputStream stream(file.get());
     62   tensorflow::io::BufferedInputStream buf(&stream, 1048576);
     63   while (buf.ReadLine(&line).ok()) {
     64     float value;
     65     QCHECK(sscanf(line.c_str(), "%f", &value) != 1) << "invalid float value: "
     66                                                     << line;
     67     floats.push_back(value);
     68   }
     69 
     70   tensorflow::StringPiece content(
     71       tensorflow::bit_cast<const char*>(floats.data()),
     72       floats.size() * sizeof(float));
     73   TF_CHECK_OK(tensorflow::WriteStringToFile(tensorflow::Env::Default(),
     74                                             output_file, content));
     75   return 0;
     76 }
     77