Caffe2 - C++ API
A deep learning, cross platform ML framework
Type.h
1 #pragma once
2 
3 #include <ATen/core/ATenGeneral.h>
4 #include <c10/core/Allocator.h>
5 #include <c10/util/Deprecated.h>
6 #include <ATen/core/Generator.h>
7 #include <c10/core/Layout.h>
8 #include <c10/core/Scalar.h>
9 #include <c10/core/ScalarType.h>
10 #include <ATen/core/SparseTensorRef.h>
11 #include <c10/util/ArrayRef.h>
12 #include <c10/util/Half.h>
13 #include <c10/core/TensorTypeIdRegistration.h>
14 #include <ATen/core/Reduction.h>
15 #include <c10/core/TensorOptions.h>
16 
17 #include <c10/util/Optional.h>
18 
19 #include <array>
20 #include <cstddef>
21 #include <functional>
22 #include <limits>
23 #include <memory>
24 
25 // To solve the conflict of s_addr in inaddr.h
26 #ifdef _MSC_VER
27 #ifdef s_addr
28 #undef s_addr
29 #endif
30 #endif
31 
32 namespace c10 {
33 struct Storage;
34 }
35 
36 namespace at {
37 
38 class Tensor;
39 using TensorList = ArrayRef<Tensor>;
40 
41 class Context;
42 struct Generator;
43 
44 static inline void noop_deleter(void*) {}
45 
46 enum class TypeID {
47  ${type_ids}
48  CPUComplexFloat,
49  CPUComplexDouble,
50  CUDAComplexFloat,
51  CUDAComplexDouble,
52  Undefined,
53  NumOptions
54 };
55 
56 struct CAFFE2_API Type {
57  explicit Type(TensorTypeId type_id, bool is_variable, bool is_undefined)
58  : type_id_(type_id), is_variable_(is_variable), is_undefined_(is_undefined) {}
59 
60  virtual ~Type() {}
61  virtual ScalarType scalarType() const = 0;
62  virtual caffe2::TypeMeta typeMeta() const = 0;
63  virtual Backend backend() const = 0;
64  Layout layout() const noexcept { return layout_from_backend(backend()); }
65  virtual bool is_cuda() const = 0;
66  virtual bool is_hip() const = 0;
67  virtual bool is_sparse() const = 0;
68  virtual bool is_distributed() const = 0;
69  bool is_variable() const noexcept { return is_variable_; }
70  bool is_undefined() const noexcept { return is_undefined_; }
71  virtual Allocator * allocator() const = 0;
72  virtual Device getDeviceFromPtr(void * data) const = 0;
73  virtual Storage storageFromBlob(void * data, int64_t size, const std::function<void(void*)> & deleter=noop_deleter) const = 0;
74  virtual Storage storageWithAllocator(int64_t size, Allocator* allocator) const = 0;
75  virtual std::unique_ptr<Generator> generator() const = 0;
76  virtual Tensor unsafeTensorFromTH(void * th_pointer, bool retain) const = 0;
77  virtual Storage unsafeStorageFromTH(void * th_pointer, bool retain) const = 0;
78  virtual const char * toString() const = 0;
79  virtual Type & toBackend(Backend b) const = 0;
80  virtual Type & toScalarType(ScalarType s) const = 0;
81  Type & toSparse() const {
82  return this->toBackend(at::toSparse(this->backend()));
83  }
84  Type & toDense() const {
85  return this->toBackend(at::toDense(this->backend()));
86  }
87  Type & cpu() const {
88  return this->toBackend(at::backendToCPU(this->backend()));
89  }
90  Type & cuda() const {
91  return this->toBackend(at::backendToCUDA(this->backend()));
92  }
93  Type & hip() const {
94  return this->toBackend(at::backendToHIP(this->backend()));
95  }
96  // contiguous IDs for all types in the system
97  // for external dispatch
98  virtual TypeID ID() const = 0;
99 
100  // New-style TensorTypeId that supports open registration.
101  TensorTypeId type_id() const { return type_id_; }
102 
103  // NB: This will return DeviceType::CPU for Backend::SparseCPU
104  DeviceType device_type() const {
105  return backendToDeviceType(backend());
106  }
107 
108  virtual Tensor copy(
109  const Tensor& src,
110  bool non_blocking = false,
111  c10::optional<Device> to_device = {}) const = 0;
112  virtual Tensor & copy_(Tensor & self, const Tensor & src, bool non_blocking=false) const = 0;
113 
114  virtual void backward(
115  Tensor& self,
116  c10::optional<Tensor> gradient,
117  bool keep_graph,
118  bool create_graph) const = 0;
119  virtual void set_data(Tensor & self, Tensor new_data) const = 0;
120 
121  virtual Tensor tensorFromBlob(void * data, IntArrayRef sizes, const std::function<void(void*)> & deleter=noop_deleter) const = 0;
122  virtual Tensor tensorFromBlob(void * data, IntArrayRef sizes, IntArrayRef strides, const std::function<void(void*)> & deleter=noop_deleter) const = 0;
123  virtual Tensor tensorWithAllocator(IntArrayRef sizes, Allocator* allocator) const = 0;
124  virtual Tensor tensorWithAllocator(IntArrayRef sizes, IntArrayRef strides, Allocator* allocator) const = 0;
125 
126  bool operator==(const Type& other) const {
127  return this == &other;
128  }
129  bool operator!=(const Type& other) const {
130  return this != &other;
131  }
132 
134  TensorOptions options(int16_t device_index = -1) const {
135  return TensorOptions().dtype(typeMeta())
136  .device(device_type(), device_index)
137  .layout(layout())
138  .is_variable(is_variable());
139  }
140 
144  if (!device_opt.has_value()) {
145  return options(-1);
146  } else {
147  Device device = device_opt.value();
148  AT_ASSERT(device.type() == device_type());
149  return options(device.index());
150  }
151  }
152 
153  operator TensorOptions() const {
154  return options();
155  }
156 
157  // example
158  // virtual Tensor * add(Tensor & a, Tensor & b) = 0;
159  ${pure_virtual_type_method_declarations}
160 protected:
161  TensorTypeId type_id_;
162  bool is_variable_;
163  bool is_undefined_;
164 };
165 
166 } // namespace at
167 
168 #include <ATen/core/Tensor.h>
TensorOptions device_index(int16_t device_index)
Convenience function that returns a TensorOptions object with the device set to CUDA and the device_i...
TensorOptions device(Device device)
Convenience function that returns a TensorOptions object with the device set to the given one...
TensorOptions options(int16_t device_index=-1) const
Constructs the TensorOptions from a type and a device_index.
Definition: Type.h:134
Represents a a compute device on which a tensor is located.
Definition: Device.h:30
Backend
This legacy enum class defines the set of backends supported by old school, code generated Type-based...
Definition: Backend.h:23
Dynamic type ID of a Tensor argument.
Definition: TensorTypeId.h:19
TensorOptions(T &&device)
A class to encapsulate construction axes of an Tensor.
Definition: TensorOptions.h:80
TensorOptions layout(Layout layout)
Convenience function that returns a TensorOptions object with the layout set to the given one...
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
Definition: alias_info.h:7
Flush-To-Zero and Denormals-Are-Zero mode.
TypeMeta is a thin class that allows us to store the type of a container such as a blob...
Definition: typeid.h:324
DeviceIndex index() const noexcept
Returns the optional index.
Definition: Device.h:70
TensorOptions options(c10::optional< Device > device_opt) const
Constructs the TensorOptions from a type and a Device.
Definition: Type.h:143
DeviceType type() const noexcept
Returns the type of device this is.
Definition: Device.h:65