1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
#include <torch/csrc/utils/invalid_arguments.h>
#include <torch/csrc/utils/memory.h>
#include <torch/csrc/utils/python_strings.h>
#include <c10/util/irange.h>
#include <algorithm>
#include <memory>
#include <unordered_map>
namespace torch {
namespace {
std::string py_typename(PyObject* object) {
return Py_TYPE(object)->tp_name;
}
struct Type {
virtual bool is_matching(PyObject* object) = 0;
virtual ~Type() = default;
};
struct SimpleType : public Type {
SimpleType(std::string& name) : name(name){};
bool is_matching(PyObject* object) override {
return py_typename(object) == name;
}
std::string name;
};
struct MultiType : public Type {
MultiType(std::initializer_list<std::string> accepted_types)
: types(accepted_types){};
bool is_matching(PyObject* object) override {
auto it = std::find(types.begin(), types.end(), py_typename(object));
return it != types.end();
}
std::vector<std::string> types;
};
struct NullableType : public Type {
NullableType(std::unique_ptr<Type> type) : type(std::move(type)){};
bool is_matching(PyObject* object) override {
return object == Py_None || type->is_matching(object);
}
std::unique_ptr<Type> type;
};
struct TupleType : public Type {
TupleType(std::vector<std::unique_ptr<Type>> types)
: types(std::move(types)){};
bool is_matching(PyObject* object) override {
if (!PyTuple_Check(object))
return false;
auto num_elements = PyTuple_GET_SIZE(object);
if (num_elements != (long)types.size())
return false;
for (const auto i : c10::irange(num_elements)) {
if (!types[i]->is_matching(PyTuple_GET_ITEM(object, i)))
return false;
}
return true;
}
std::vector<std::unique_ptr<Type>> types;
};
struct SequenceType : public Type {
SequenceType(std::unique_ptr<Type> type) : type(std::move(type)){};
bool is_matching(PyObject* object) override {
if (!PySequence_Check(object))
return false;
auto num_elements = PySequence_Length(object);
for (const auto i : c10::irange(num_elements)) {
if (!type->is_matching(PySequence_GetItem(object, i)))
return false;
}
return true;
}
std::unique_ptr<Type> type;
};
struct Argument {
Argument(std::string name, std::unique_ptr<Type> type)
: name(std::move(name)), type(std::move(type)){};
std::string name;
std::unique_ptr<Type> type;
};
struct Option {
Option(std::vector<Argument> arguments, bool is_variadic, bool has_out)
: arguments(std::move(arguments)),
is_variadic(is_variadic),
has_out(has_out){};
Option(bool is_variadic, bool has_out)
: arguments(), is_variadic(is_variadic), has_out(has_out){};
Option(const Option&) = delete;
Option(Option&& other)
: arguments(std::move(other.arguments)),
is_variadic(other.is_variadic),
has_out(other.has_out){};
std::vector<Argument> arguments;
bool is_variadic;
bool has_out;
};
std::vector<std::string> _splitString(
const std::string& s,
const std::string& delim) {
std::vector<std::string> tokens;
size_t start = 0;
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
size_t end;
while ((end = s.find(delim, start)) != std::string::npos) {
tokens.push_back(s.substr(start, end - start));
start = end + delim.length();
}
tokens.push_back(s.substr(start));
return tokens;
}
std::unique_ptr<Type> _buildType(std::string type_name, bool is_nullable) {
std::unique_ptr<Type> result;
if (type_name == "float") {
result = torch::make_unique<MultiType>(MultiType{"float", "int", "long"});
} else if (type_name == "int") {
result = torch::make_unique<MultiType>(MultiType{"int", "long"});
} else if (type_name.find("tuple[") == 0) {
auto type_list = type_name.substr(6);
type_list.pop_back();
std::vector<std::unique_ptr<Type>> types;
for (auto& type : _splitString(type_list, ","))
types.emplace_back(_buildType(type, false));
result = torch::make_unique<TupleType>(std::move(types));
} else if (type_name.find("sequence[") == 0) {
auto subtype = type_name.substr(9);
subtype.pop_back();
result = torch::make_unique<SequenceType>(_buildType(subtype, false));
} else {
result = torch::make_unique<SimpleType>(type_name);
}
if (is_nullable)
result = torch::make_unique<NullableType>(std::move(result));
return result;
}
std::pair<Option, std::string> _parseOption(
const std::string& _option_str,
const std::unordered_map<std::string, PyObject*>& kwargs) {
if (_option_str == "no arguments")
return std::pair<Option, std::string>(Option(false, false), _option_str);
bool has_out = false;
std::vector<Argument> arguments;
std::string printable_option = _option_str;
std::string option_str = _option_str.substr(1, _option_str.length() - 2);
/// XXX: this is a hack only for the out arg in TensorMethods
auto out_pos = printable_option.find('#');
if (out_pos != std::string::npos) {
if (kwargs.count("out") > 0) {
std::string kwonly_part = printable_option.substr(out_pos + 1);
printable_option.erase(out_pos);
printable_option += "*, ";
printable_option += kwonly_part;
} else if (out_pos >= 2) {
printable_option.erase(out_pos - 2);
printable_option += ")";
} else {
printable_option.erase(out_pos);
printable_option += ")";
}
has_out = true;
}
for (auto& arg : _splitString(option_str, ", ")) {
bool is_nullable = false;
auto type_start_idx = 0;
if (arg[type_start_idx] == '#') {
type_start_idx++;
}
if (arg[type_start_idx] == '[') {
is_nullable = true;
type_start_idx++;
arg.erase(arg.length() - std::string(" or None]").length());
}
auto type_end_idx = arg.find_last_of(' ');
auto name_start_idx = type_end_idx + 1;
// "type ... name" => "type ... name"
// ^ ^
auto dots_idx = arg.find("...");
if (dots_idx != std::string::npos)
type_end_idx -= 4;
std::string type_name =
arg.substr(type_start_idx, type_end_idx - type_start_idx);
std::string name = arg.substr(name_start_idx);
arguments.emplace_back(name, _buildType(type_name, is_nullable));
}
bool is_variadic = option_str.find("...") != std::string::npos;
return std::pair<Option, std::string>(
Option(std::move(arguments), is_variadic, has_out),
std::move(printable_option));
}
bool _argcountMatch(
const Option& option,
const std::vector<PyObject*>& arguments,
const std::unordered_map<std::string, PyObject*>& kwargs) {
auto num_expected = option.arguments.size();
auto num_got = arguments.size() + kwargs.size();
// Note: variadic functions don't accept kwargs, so it's ok
if (option.has_out && kwargs.count("out") == 0)
num_expected--;
return num_got == num_expected ||
(option.is_variadic && num_got > num_expected);
}
std::string _formattedArgDesc(
const Option& option,
const std::vector<PyObject*>& arguments,
const std::unordered_map<std::string, PyObject*>& kwargs) {
std::string red;
std::string reset_red;
std::string green;
std::string reset_green;
if (isatty(1) && isatty(2)) {
red = "\33[31;1m";
reset_red = "\33[0m";
green = "\33[32;1m";
reset_green = "\33[0m";
} else {
red = "!";
reset_red = "!";
green = "";
reset_green = "";
}
auto num_args = arguments.size() + kwargs.size();
std::string result = "(";
for (const auto i : c10::irange(num_args)) {
bool is_kwarg = i >= arguments.size();
PyObject* arg =
is_kwarg ? kwargs.at(option.arguments[i].name) : arguments[i];
bool is_matching = false;
if (i < option.arguments.size()) {
is_matching = option.arguments[i].type->is_matching(arg);
} else if (option.is_variadic) {
is_matching = option.arguments.back().type->is_matching(arg);
}
if (is_matching)
result += green;
else
result += red;
if (is_kwarg)
result += option.arguments[i].name + "=";
result += py_typename(arg);
if (is_matching)
result += reset_green;
else
result += reset_red;
result += ", ";
}
if (arguments.size() > 0)
result.erase(result.length() - 2);
result += ")";
return result;
}
std::string _argDesc(
const std::vector<PyObject*>& arguments,
const std::unordered_map<std::string, PyObject*>& kwargs) {
std::string result = "(";
for (auto& arg : arguments)
result += std::string(py_typename(arg)) + ", ";
for (auto& kwarg : kwargs)
result += kwarg.first + "=" + py_typename(kwarg.second) + ", ";
if (arguments.size() > 0)
result.erase(result.length() - 2);
result += ")";
return result;
}
std::vector<std::string> _tryMatchKwargs(
const Option& option,
const std::unordered_map<std::string, PyObject*>& kwargs) {
std::vector<std::string> unmatched;
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
int64_t start_idx = option.arguments.size() - kwargs.size();
if (option.has_out && kwargs.count("out") == 0)
start_idx--;
if (start_idx < 0)
start_idx = 0;
for (auto& entry : kwargs) {
bool found = false;
for (unsigned int i = start_idx; i < option.arguments.size(); i++) {
if (option.arguments[i].name == entry.first) {
found = true;
break;
}
}
if (!found)
unmatched.push_back(entry.first);
}
return unmatched;
}
} // anonymous namespace
std::string format_invalid_args(
PyObject* given_args,
PyObject* given_kwargs,
const std::string& function_name,
const std::vector<std::string>& options) {
std::vector<PyObject*> args;
std::unordered_map<std::string, PyObject*> kwargs;
std::string error_msg;
error_msg.reserve(2000);
error_msg += function_name;
error_msg += " received an invalid combination of arguments - ";
Py_ssize_t num_args = PyTuple_Size(given_args);
for (const auto i : c10::irange(num_args)) {
PyObject* arg = PyTuple_GET_ITEM(given_args, i);
args.push_back(arg);
}
bool has_kwargs = given_kwargs && PyDict_Size(given_kwargs) > 0;
if (has_kwargs) {
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(given_kwargs, &pos, &key, &value)) {
kwargs.emplace(THPUtils_unpackString(key), value);
}
}
if (options.size() == 1) {
auto pair = _parseOption(options[0], kwargs);
auto& option = pair.first;
auto& option_str = pair.second;
std::vector<std::string> unmatched_kwargs;
if (has_kwargs)
unmatched_kwargs = _tryMatchKwargs(option, kwargs);
if (unmatched_kwargs.size()) {
error_msg += "got unrecognized keyword arguments: ";
for (auto& kwarg : unmatched_kwargs)
error_msg += kwarg + ", ";
error_msg.erase(error_msg.length() - 2);
} else {
error_msg += "got ";
if (_argcountMatch(option, args, kwargs)) {
error_msg += _formattedArgDesc(option, args, kwargs);
} else {
error_msg += _argDesc(args, kwargs);
}
error_msg += ", but expected ";
error_msg += option_str;
}
} else {
error_msg += "got ";
error_msg += _argDesc(args, kwargs);
error_msg += ", but expected one of:\n";
for (auto& option_str : options) {
auto pair = _parseOption(option_str, kwargs);
auto& option = pair.first;
auto& printable_option_str = pair.second;
error_msg += " * ";
error_msg += printable_option_str;
error_msg += "\n";
if (_argcountMatch(option, args, kwargs)) {
std::vector<std::string> unmatched_kwargs;
if (has_kwargs)
unmatched_kwargs = _tryMatchKwargs(option, kwargs);
if (unmatched_kwargs.size() > 0) {
error_msg +=
" didn't match because some of the keywords were incorrect: ";
for (auto& kwarg : unmatched_kwargs)
error_msg += kwarg + ", ";
error_msg.erase(error_msg.length() - 2);
error_msg += "\n";
} else {
error_msg +=
" didn't match because some of the arguments have invalid types: ";
error_msg += _formattedArgDesc(option, args, kwargs);
error_msg += "\n";
}
}
}
}
return error_msg;
}
} // namespace torch
|