Caffe2 - C++ API
A deep learning, cross platform ML framework
utils.cpp
1 #include <torch/csrc/autograd/functions/utils.h>
2 
3 #include <torch/csrc/autograd/edge.h>
4 #include <torch/csrc/autograd/function.h>
5 #include <torch/csrc/autograd/variable.h>
6 
7 #include <sstream>
8 #include <vector>
9 
10 namespace torch { namespace autograd {
11 
12 variable_list wrap_outputs(const variable_list& inputs, tensor_list&& outputs,
13  const function_constructor& ctr) {
14  variable_list result;
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, /*requires_grad=*/false));
20  } else {
21  result.emplace_back();
22  }
23  }
24  } else {
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, /*requires_grad=*/false);
29  autograd::create_gradient_edge(variable, grad_fn);
30  result.push_back(std::move(variable));
31  } else {
32  grad_fn->add_input_metadata(Function::undefined_input());
33  result.emplace_back();
34  }
35  }
36  }
37  return result;
38 }
39 
40 void check_input_variables(const char* name, const variable_list& inputs, int args, int required_args) {
41  if (required_args == -1) {
42  required_args = args;
43  }
44  if (inputs.size() != (size_t)args) {
45  std::stringstream ss;
46  ss << name << ": expected " << args << " arguments (got " << inputs.size();
47  ss << ")";
48  throw std::runtime_error(ss.str());
49  }
50  for (int i = 0; i < required_args; ++i) {
51  if (!inputs[i].defined()) {
52  std::stringstream ss;
53  ss << name << ": expected Tensor at argument " << i << " (got None)";
54  throw std::runtime_error(ss.str());
55  }
56  }
57 }
58 }} // namespace torch::autograd
Definition: jit_type.h:17