op_tester_config.h 7.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

#include <istream>
Y
Yiqun Liu 已提交
18
#include <sstream>
19
#include <string>
20
#include <unordered_map>
21 22 23 24 25 26 27 28 29 30
#include <vector>

namespace paddle {
namespace operators {
namespace benchmark {

struct OpInputConfig {
  OpInputConfig() {}
  explicit OpInputConfig(std::istream& is);

Y
Yiqun Liu 已提交
31 32
  void ParseDType(std::istream& is);
  void ParseInitializer(std::istream& is);
33 34 35
  void ParseDims(std::istream& is);
  void ParseLoD(std::istream& is);

36
  std::string name;
Y
Yiqun Liu 已提交
37
  std::string dtype{"fp32"};  // int32/int, int64/long, fp32/float, fp64/double
38 39
  std::string initializer{"random"};  // random, natural, zeros, file
  std::string filename{""};
40
  std::vector<int64_t> dims;
41
  std::vector<std::vector<size_t>> lod;
42 43 44 45 46
};

struct OpTesterConfig {
  OpTesterConfig() {}
  explicit OpTesterConfig(const std::string& filename);
47 48 49 50

  bool Init(std::istream& is);

  bool ParseAttrs(std::istream& is);
51 52 53 54 55

  const OpInputConfig* GetInput(const std::string& name);

  std::string op_type;
  std::vector<OpInputConfig> inputs;
56
  std::unordered_map<std::string, std::string> attrs;
57 58 59 60 61 62 63
  int device_id{-1};  // CPU: -1
  int repeat{1};
  int profile{0};
  int print_debug_string{0};
  double runtime{0.0};
};

Y
Yiqun Liu 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
static bool Has(const std::vector<std::string>& vec, const std::string& item) {
  for (size_t i = 0; i < vec.size(); ++i) {
    if (vec[i] == item) {
      return true;
    }
  }
  return false;
}

template <typename T>
T StringTo(const std::string& str) {
  std::istringstream is(str);
  T value;
  is >> value;
  return value;
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
static const char kStartSeparator[] = "{";
static const char kEndSeparator[] = "}";
static const char kSepBetweenItems[] = ";";

static bool StartWith(const std::string& str, const std::string& substr) {
  return str.find(substr) == 0;
}

static bool EndWith(const std::string& str, const std::string& substr) {
  return str.rfind(substr) == (str.length() - substr.length());
}

static void EraseEndSep(std::string* str,
                        std::string substr = kSepBetweenItems) {
  if (EndWith(*str, substr)) {
    str->erase(str->length() - substr.length(), str->length());
  }
}

OpInputConfig::OpInputConfig(std::istream& is) {
  std::string sep;
  is >> sep;
  if (sep == kStartSeparator) {
    while (sep != kEndSeparator) {
      is >> sep;
      if (sep == "name" || sep == "name:") {
        is >> name;
        EraseEndSep(&name);
      } else if (sep == "dtype" || sep == "dtype:") {
        ParseDType(is);
      } else if (sep == "initializer" || sep == "initializer:") {
        ParseInitializer(is);
      } else if (sep == "dims" || sep == "dims:") {
        ParseDims(is);
      } else if (sep == "lod" || sep == "lod:") {
        ParseLoD(is);
      } else if (sep == "filename") {
        is >> filename;
        EraseEndSep(&filename);
      }
    }
  }
}

void OpInputConfig::ParseDType(std::istream& is) {
  std::string dtype_str;
  is >> dtype_str;
  EraseEndSep(&dtype_str);

  if (dtype_str == "int32" || dtype_str == "int") {
    dtype = "int32";
  } else if (dtype_str == "int64" || dtype_str == "long") {
    dtype = "int64";
  } else if (dtype_str == "fp32" || dtype_str == "float") {
    dtype = "fp32";
  } else if (dtype_str == "fp64" || dtype_str == "double") {
    dtype = "fp64";
  } else {
    PADDLE_THROW(platform::errors::Unimplemented(
        "Unsupported dtype %s in OpInputConfig.", dtype_str.c_str()));
  }
  VLOG(4) << "dtype of input " << name << " is: " << dtype;
}

void OpInputConfig::ParseInitializer(std::istream& is) {
  std::string initializer_str;
  is >> initializer_str;
  EraseEndSep(&initializer_str);

  const std::vector<std::string> supported_initializers = {
      "random", "natural", "zeros", "file"};
  if (!Has(supported_initializers, initializer_str)) {
    PADDLE_THROW(platform::errors::Unimplemented(
        "Unsupported initializer %s in OpInputConfig.",
        initializer_str.c_str()));
  }

  initializer = initializer_str;
  VLOG(4) << "initializer of input " << name << " is: " << initializer;
}

void OpInputConfig::ParseDims(std::istream& is) {
  std::string dims_str;
  is >> dims_str;

  dims.clear();
  std::string token;
  std::istringstream token_stream(dims_str);
  while (std::getline(token_stream, token, 'x')) {
    dims.push_back(std::stoi(token));
  }
}

void OpInputConfig::ParseLoD(std::istream& is) {
  std::string lod_str;
  std::string start_sep =
      std::string(kStartSeparator) + std::string(kStartSeparator);
  std::string end_sep = std::string(kEndSeparator) + std::string(kEndSeparator);

  std::string sep;
  is >> sep;
  if (StartWith(sep, start_sep)) {
    lod_str += sep;
    while (!EndWith(sep, end_sep)) {
      is >> sep;
      lod_str += sep;
    }
  }
  EraseEndSep(&lod_str);
  PADDLE_ENFORCE_GE(
      lod_str.length(),
      4U,
      platform::errors::InvalidArgument(
          "The length of lod string should be "
          "equal to or larger than 4. But length of lod string is %zu.",
          lod_str.length()));
  VLOG(4) << "lod: " << lod_str << ", length: " << lod_str.length();

  // Parse the lod_str
  lod.clear();
  for (size_t i = 1; i < lod_str.length() - 1;) {
    if (lod_str[i] == '{') {
      std::vector<size_t> level;
      while (lod_str[i] != '}') {
        ++i;

        std::string number;
        while (lod_str[i] >= '0' && lod_str[i] <= '9') {
          number += lod_str[i];
          ++i;
        }
        level.push_back(StringTo<size_t>(number));
      }
      lod.push_back(level);
    } else if (lod_str[i] == '}') {
      ++i;
    }
  }
}

OpTesterConfig::OpTesterConfig(const std::string& filename) {
  std::ifstream fin(filename, std::ios::in | std::ios::binary);
  PADDLE_ENFORCE_EQ(
      static_cast<bool>(fin),
      true,
      platform::errors::InvalidArgument("OpTesterConfig cannot open file %s.",
                                        filename.c_str()));

  Init(fin);
}

bool OpTesterConfig::Init(std::istream& is) {
  std::string sep;
  is >> sep;
  if (sep == kStartSeparator) {
    while (sep != kEndSeparator) {
      is >> sep;
      if (sep == "op_type" || sep == "op_type:") {
        is >> op_type;
      } else if (sep == "device_id" || sep == "device_id:") {
        is >> device_id;
      } else if (sep == "repeat" || sep == "repeat:") {
        is >> repeat;
      } else if (sep == "profile" || sep == "profile:") {
        is >> profile;
      } else if (sep == "print_debug_string" || sep == "print_debug_string:") {
        is >> print_debug_string;
      } else if (sep == "input" || sep == "input:") {
        OpInputConfig input_config(is);
        inputs.push_back(input_config);
      } else if (sep == "attrs" || sep == "attrs:") {
        ParseAttrs(is);
      } else {
        if (sep != kEndSeparator) {
          return false;
        }
      }
    }
  } else {
    return false;
  }
  return true;
}

bool OpTesterConfig::ParseAttrs(std::istream& is) {
  std::string sep;
  is >> sep;
  if (sep == kStartSeparator) {
    while (true) {
      std::string key;
      is >> key;
      if (key == kEndSeparator) {
        break;
      }

      std::string value;
      is >> value;
      EraseEndSep(&key, ":");
      EraseEndSep(&value);
      VLOG(4) << "attrs: " << key << ", " << value;

      attrs[key] = value;
    }
  }
  return true;
}

const OpInputConfig* OpTesterConfig::GetInput(const std::string& name) {
  for (size_t i = 0; i < inputs.size(); ++i) {
    if (inputs[i].name == name) {
      return &inputs[i];
    }
  }
  return nullptr;
}

297 298 299
}  // namespace benchmark
}  // namespace operators
}  // namespace paddle