desc_tensor.h 2.3 KB
Newer Older
J
Jiabin Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#include "paddle/fluid/framework/convert_utils.h"
#include "paddle/fluid/framework/var_desc.h"
#include "paddle/phi/core/ddim.h"
#include "paddle/phi/core/extended_tensor.h"
#include "paddle/phi/core/utils/data_type.h"
#include "paddle/utils/any.h"

namespace paddle {
namespace prim {

class DescTensor : public phi::ExtendedTensor,
                   public phi::TypeInfoTraits<phi::TensorBase, DescTensor> {
 public:
  explicit DescTensor(framework::VarDesc* desc)
      : desc_ptr_(desc), dims_(phi::make_ddim(desc->GetShape())) {}
  static const char* name() { return "DescTensor"; }

  std::string Name() const { return desc_ptr_->Name(); }

  std::vector<int64_t> shape() const { return desc_ptr_->GetShape(); }

  const phi::DDim& dims() const override {
    dims_ = phi::make_ddim(desc_ptr_->GetShape());
    return dims_;
  }

H
heyanru 已提交
42 43
  int64_t numel() const override { return product(dims()); }

J
Jiabin Yang 已提交
44 45 46 47 48 49
  DataType dtype() const override {
    return paddle::framework::TransToPhiDataType(desc_ptr_->GetDataType());
  }

  framework::VarDesc* get_ptr() { return desc_ptr_; }

50 51
  const phi::Place& place() const override { return place_; }

52 53
  bool initialized() const override { return desc_ptr_ != nullptr; }

J
Jiabin Yang 已提交
54 55 56 57 58 59 60 61 62 63
  // TODO(jiabin): override more operators here.

 private:
  // VarDesc's lifetime is holded by block and it's program, so we just conceal
  // its funcs instead of its life.
  framework::VarDesc* desc_ptr_;
  // TODO(jiabin): This is really ugly, but we have to hold a dims here so that
  // we can inherient from ExtendedTensor Rmove this when we make VarDesc's as
  // same as Tensor, or make Tensor's dims more lightly.
  mutable phi::DDim dims_;
64
  phi::Place place_;
J
Jiabin Yang 已提交
65 66 67 68
};

}  // namespace prim
}  // namespace paddle