3 #include <torch/csrc/python_headers.h> 6 #include <torch/csrc/utils/python_arg_parser.h> 8 namespace torch {
namespace autograd {
namespace utils {
13 parse_to_conversion(PyObject *args, PyObject *kwargs,
bool allow_copy) {
14 static PythonArgParser parser({
15 "to(Device device=None, ScalarType dtype=None, bool non_blocking=False, bool copy=False)",
16 "to(ScalarType dtype, bool non_blocking=False, bool copy=False)",
17 "to(Tensor tensor, bool non_blocking=False, bool copy=False)",
19 ParsedArgs<4> parsed_args;
20 auto r = parser.parse(args, kwargs, parsed_args);
22 if (!allow_copy && !r.isNone(3))
23 throw std::runtime_error(
".to() does not accept copy argument");
24 return std::make_tuple(r.deviceOptional(0), r.scalartypeOptional(1), r.toBool(2), r.toBool(3));
25 }
else if (r.idx == 1) {
26 if (!allow_copy && !r.isNone(2))
27 throw std::runtime_error(
".to() does not accept copy argument");
28 return std::make_tuple(c10::nullopt, r.scalartype(0), r.toBool(1), r.toBool(2));
30 auto tensor = r.tensor(0);
31 if (!allow_copy && !r.isNone(2))
32 throw std::runtime_error(
".to() does not accept copy argument");
33 return std::make_tuple(