1 #include <torch/csrc/autograd/functions/utils.h> 3 #include <torch/csrc/autograd/edge.h> 4 #include <torch/csrc/autograd/function.h> 5 #include <torch/csrc/autograd/variable.h> 10 namespace torch {
namespace autograd {
12 variable_list wrap_outputs(
const variable_list& inputs, tensor_list&& outputs,
13 const function_constructor& ctr) {
15 result.reserve(outputs.size());
16 if (!any_variable_requires_grad(inputs)) {
17 for (
auto& output : outputs) {
18 if (output.defined()) {
19 result.push_back(make_variable(output,
false));
21 result.emplace_back();
25 auto grad_fn = ctr(collect_next_edges(inputs));
26 for (
auto& output : outputs) {
27 if (output.defined()) {
28 auto variable = autograd::make_variable(output,
false);
29 autograd::create_gradient_edge(variable, grad_fn);
30 result.push_back(std::move(variable));
32 grad_fn->add_input_metadata(Function::undefined_input());
33 result.emplace_back();
40 void check_input_variables(
const char* name,
const variable_list& inputs,
int args,
int required_args) {
41 if (required_args == -1) {
44 if (inputs.size() != (size_t)args) {
46 ss << name <<
": expected " << args <<
" arguments (got " << inputs.size();
48 throw std::runtime_error(ss.str());
50 for (
int i = 0; i < required_args; ++i) {
51 if (!inputs[i].defined()) {
53 ss << name <<
": expected Tensor at argument " << i <<
" (got None)";
54 throw std::runtime_error(ss.str());