From a0ce05df1eb6ae394ea538f1a0bcc27fdbb1852d Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Fri, 22 Sep 2017 11:26:04 -0700 Subject: [PATCH] Remove `numel` field in tensor * It is duplicated with `dim_`. We can use `dim_` to calculate `numel` everytime. It does not cost too much. * `numel` is not initialized by constructor. Also, `numel` is hard to synchronize with `dim_`. So just remove it. --- paddle/framework/tensor.h | 6 ------ paddle/framework/tensor_impl.h | 3 +-- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/paddle/framework/tensor.h b/paddle/framework/tensor.h index 4b5a2ae523..7bce8e4ff2 100644 --- a/paddle/framework/tensor.h +++ b/paddle/framework/tensor.h @@ -165,12 +165,6 @@ class Tensor { /*! points to dimensions of memory block. */ DDim dims_; - /** - * A cache of the number of elements in a tensor. - * Would be 0 for an uninitialized tensor. - */ - int64_t numel_; - /** * @brief A PlaceHolder may be shared by more than one tensor. * diff --git a/paddle/framework/tensor_impl.h b/paddle/framework/tensor_impl.h index 6d2c14f4c4..a5405f9c31 100644 --- a/paddle/framework/tensor_impl.h +++ b/paddle/framework/tensor_impl.h @@ -147,13 +147,12 @@ inline Tensor Tensor::Slice(const int& begin_idx, const int& end_idx) const { inline Tensor& Tensor::Resize(const DDim& dims) { dims_ = dims; - numel_ = product(dims_); return *this; } inline const DDim& Tensor::dims() const { return dims_; } -inline int64_t Tensor::numel() const { return numel_; } +inline int64_t Tensor::numel() const { return product(dims_); } template inline Tensor ReshapeToMatrix(const Tensor& src, int num_col_dims) {