Implement c10::Tensor (#14819)
authorSebastian Messmer <messmer@fb.com>
Wed, 9 Jan 2019 04:22:40 +0000 (20:22 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 9 Jan 2019 04:31:40 +0000 (20:31 -0800)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/14819

This is a minimal wrapper for a c10::TensorImpl,
maybe destined for greatness later when we move caffe2::Tensor or at::Tensor into c10.

Reviewed By: dzhulgakov

Differential Revision: D13348039

fbshipit-source-id: 874f515358e94f35dc7a4c3e55b35fde59c51ff1

c10/core/Tensor.h [new file with mode: 0644]
caffe2/core/tensor.h

diff --git a/c10/core/Tensor.h b/c10/core/Tensor.h
new file mode 100644 (file)
index 0000000..c8f9c11
--- /dev/null
@@ -0,0 +1,53 @@
+#pragma once
+
+#include <c10/core/TensorImpl.h>
+#include <c10/core/UndefinedTensorImpl.h>
+#include <c10/macros/Macros.h>
+
+namespace c10 {
+
+/**
+ * This is a minimal Tensor class for use in c10 code.
+ * The plan on record is to eventually merge at::Tensor and caffe2::Tensor
+ * and move that merged class to c10, replacing this one.
+ *
+ * At time of writing this, we couldn't do that yet, because their APIs are
+ * not clean enough to make it in c10 and because they have dependencies we want
+ * to avoid, for example at::Tensor depends on at::Type.
+ */
+class C10Tensor final {
+private:
+  using TensorImplPtr = intrusive_ptr<TensorImpl, UndefinedTensorImpl>;
+public:
+  explicit C10Tensor(TensorImplPtr impl) noexcept;
+
+  C10Tensor(const C10Tensor&) = default;
+  C10Tensor(C10Tensor&&) noexcept = default;
+  C10Tensor& operator=(const C10Tensor&) = default;
+  C10Tensor& operator=(C10Tensor&&) noexcept = default;
+
+  const TensorImplPtr &impl() const & noexcept;
+  TensorImplPtr impl() && noexcept;
+
+  TensorTypeId type_id() const;
+
+private:
+  TensorImplPtr impl_;
+};
+
+inline C10Tensor::C10Tensor(TensorImplPtr impl) noexcept
+: impl_(std::move(impl)) {}
+
+inline const C10Tensor::TensorImplPtr &C10Tensor::impl() const & noexcept {
+  return impl_;
+}
+
+inline C10Tensor::TensorImplPtr C10Tensor::impl() && noexcept {
+  return std::move(impl_);
+}
+
+inline TensorTypeId C10Tensor::type_id() const {
+  return impl_->type_id();
+}
+
+} // namespace c10
index 9f24e2d..dbd3851 100644 (file)
@@ -4,7 +4,7 @@
 #include "caffe2/core/storage.h"
 #include "caffe2/core/tensor_impl.h"
 
-#include <c10/core/UndefinedTensorImpl.h>
+#include <ATen/core/UndefinedTensorImpl.h>
 #include <c10/util/intrusive_ptr.h>
 #include "ATen/core/Tensor.h"
 #include <c10/core/TensorOptions.h>