From 31d7c933af77a9266e23da35ad5ea85e3cae887d Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Tue, 8 Jan 2019 20:22:40 -0800 Subject: [PATCH] Implement c10::Tensor (#14819) 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 | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ caffe2/core/tensor.h | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 c10/core/Tensor.h diff --git a/c10/core/Tensor.h b/c10/core/Tensor.h new file mode 100644 index 0000000..c8f9c11 --- /dev/null +++ b/c10/core/Tensor.h @@ -0,0 +1,53 @@ +#pragma once + +#include +#include +#include + +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; +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 diff --git a/caffe2/core/tensor.h b/caffe2/core/tensor.h index 9f24e2d..dbd3851 100644 --- a/caffe2/core/tensor.h +++ b/caffe2/core/tensor.h @@ -4,7 +4,7 @@ #include "caffe2/core/storage.h" #include "caffe2/core/tensor_impl.h" -#include +#include #include #include "ATen/core/Tensor.h" #include -- 2.7.4