Caffe2 - C++ API
A deep learning, cross platform ML framework
variable_factories.h
1 #pragma once
2 
3 // ${generated_comment}
4 
5 #include <ATen/ATen.h>
6 #include <c10/util/ArrayRef.h>
7 #include <torch/csrc/autograd/variable.h>
8 #include <torch/csrc/jit/tracer.h>
9 
10 #include <functional>
11 #include <initializer_list>
12 #include <utility>
13 
14 namespace torch {
15 
16 #define TENSOR(T, S, _1) \
17  inline at::Tensor tensor( \
18  at::ArrayRef<T> values, const at::TensorOptions& options) { \
19  at::Tensor result = \
20  at::tensor(values, at::TensorOptions(options).is_variable(false)); \
21  return autograd::make_variable(result, options.requires_grad()); \
22  } \
23  inline at::Tensor tensor( \
24  std::initializer_list<T> values, const at::TensorOptions& options) { \
25  return torch::tensor(at::ArrayRef<T>(values), options); \
26  } \
27  inline at::Tensor tensor(T value, const at::TensorOptions& options) { \
28  return torch::tensor(at::ArrayRef<T>(value), options); \
29  } \
30  inline at::Tensor tensor(at::ArrayRef<T> values) { \
31  return torch::tensor(std::move(values), at::dtype(at::k##S)); \
32  } \
33  inline at::Tensor tensor(std::initializer_list<T> values) { \
34  return torch::tensor(at::ArrayRef<T>(values)); \
35  } \
36  inline at::Tensor tensor(T value) { \
37  return torch::tensor(at::ArrayRef<T>(value)); \
38  }
39 AT_FORALL_SCALAR_TYPES_EXCEPT_HALF(TENSOR)
40 #undef TENSOR
41 
43 using Deleter = std::function<void(void*)>;
44 
52 inline at::Tensor from_blob(
53  void* data,
54  at::IntArrayRef sizes,
55  at::IntArrayRef strides,
56  const Deleter& deleter,
57  const at::TensorOptions& options = at::TensorOptions()) {
58  at::Tensor tensor =
59  at::from_blob(data, sizes, strides, deleter, options.is_variable(false));
60  return autograd::make_variable(tensor, options.requires_grad());
61 }
62 
68 inline at::Tensor from_blob(
69  void* data,
70  at::IntArrayRef sizes,
71  at::IntArrayRef strides,
72  const at::TensorOptions& options = at::TensorOptions()) {
73  return torch::from_blob(
74  data,
75  sizes,
76  strides,
77  /*deleter=*/[](void*) {},
78  options);
79 }
80 
87 inline at::Tensor from_blob(
88  void* data,
89  at::IntArrayRef sizes,
90  const Deleter& deleter,
91  const at::TensorOptions& options = at::TensorOptions()) {
92  at::Tensor tensor =
93  at::from_blob(data, sizes, deleter, options.is_variable(false));
94  return autograd::make_variable(tensor, options.requires_grad());
95 }
96 
101 inline at::Tensor from_blob(
102  void* data,
103  at::IntArrayRef sizes,
104  const at::TensorOptions& options = at::TensorOptions()) {
105  return torch::from_blob(data, sizes, /*deleter=*/[](void*) {}, options);
106 }
107 
108 ${function_definitions}
109 
110 } // namespace torch
Definition: jit_type.h:17