infermeta_utils.cc 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include "paddle/phi/core/infermeta_utils.h"
16

17
namespace phi {
18 19 20 21 22

void InferMetaContext::SetMetaConfig(MetaConfig config) {
  config_ = std::move(config);
}

23
void InferMetaContext::EmplaceBackInput(MetaTensor input) {
24 25 26 27
  int index = inputs_.size();
  inputs_.emplace_back(std::move(input));
  input_range_.emplace_back(std::pair<int, int>(index, index + 1));
}
28
void InferMetaContext::EmplaceBackOutput(MetaTensor output) {
29 30 31 32
  int index = outputs_.size();
  outputs_.emplace_back(std::move(output));
  output_range_.emplace_back(std::pair<int, int>(index, index + 1));
}
33
void InferMetaContext::EmplaceBackAttr(Attribute attr) {
34 35 36 37
  attrs_.emplace_back(std::move(attr));
}

void InferMetaContext::EmplaceBackInputs(
C
Chen Weihang 已提交
38
    paddle::small_vector<MetaTensor, phi::kInputSmallVectorSize> inputs) {
39 40 41 42 43 44 45
  int index = inputs_.size();
  input_range_.emplace_back(std::pair<int, int>(index, index + inputs.size()));
  inputs_.insert(inputs_.end(),
                 std::make_move_iterator(inputs.begin()),
                 std::make_move_iterator(inputs.end()));
}
void InferMetaContext::EmplaceBackOutputs(
C
Chen Weihang 已提交
46
    paddle::small_vector<MetaTensor, phi::kOutputSmallVectorSize> outputs) {
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  int index = outputs_.size();
  output_range_.emplace_back(
      std::pair<int, int>(index, index + outputs.size()));
  outputs_.insert(outputs_.end(),
                  std::make_move_iterator(outputs.begin()),
                  std::make_move_iterator(outputs.end()));
}

const std::pair<int, int>& InferMetaContext::InputRangeAt(size_t idx) const {
  return input_range_.at(idx);
}
const std::pair<int, int>& InferMetaContext::OutputRangeAt(size_t idx) const {
  return output_range_.at(idx);
}

const MetaConfig& InferMetaContext::GetMetaConfig() const { return config_; }

const MetaTensor& InferMetaContext::InputAt(size_t idx) const {
65
  return inputs_.at(idx);
66
}
67

68 69 70
std::vector<const MetaTensor*> InferMetaContext::InputsBetween(
    size_t start, size_t end) const {
  std::vector<const MetaTensor*> result;
71 72 73 74 75
  // If vector only contains one input that is not initialized,
  // we should return a empty vector
  if (end - start == 1 && !inputs_.at(start).initialized()) {
    return result;
  }
76

77
  result.reserve(end - start);
78
  for (size_t i = start; i < end; ++i) {
79 80
    auto& in = inputs_.at(i);
    result.emplace_back(in.initialized() ? &in : nullptr);
81 82 83 84 85
  }

  return result;
}

86
paddle::optional<std::vector<const MetaTensor*>>
87 88 89
InferMetaContext::OptionalInputsBetween(size_t start, size_t end) const {
  const auto& first = inputs_.at(start);

90
  if (first.initialized()) {
91 92 93 94
    std::vector<const MetaTensor*> result;
    result.reserve(end - start);

    for (size_t i = start; i < end; ++i) {
95 96
      auto& in = inputs_.at(i);
      result.emplace_back(in.initialized() ? &in : nullptr);
97 98
    }

99
    return paddle::optional<std::vector<const MetaTensor*>>(std::move(result));
100
  }
101
  return paddle::none;
102 103
}

104
MetaTensor* InferMetaContext::MutableOutputAt(size_t idx) {
105 106
  auto& out = outputs_.at(idx);
  return out.initialized() ? &out : nullptr;
107 108
}

109 110 111
std::vector<MetaTensor*> InferMetaContext::MutableOutputBetween(size_t start,
                                                                size_t end) {
  std::vector<MetaTensor*> result;
112 113 114 115 116 117
  // If vector only contains one output that is not initialized,
  // we should return a empty vector
  if (end - start == 1 && !outputs_.at(start).initialized()) {
    return result;
  }

118 119
  result.reserve(end - start);
  for (size_t i = start; i < end; ++i) {
120 121
    auto& out = outputs_.at(i);
    result.emplace_back(out.initialized() ? &out : nullptr);
122 123 124 125
  }
  return result;
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
template <typename AttrType>
const AttrType& InferMetaContext::AttrAt(size_t idx) const {
  try {
    return paddle::get<AttrType>(attrs_.at(idx));
  } catch (paddle::bad_variant_access const& e) {
    PADDLE_THROW(phi::errors::InvalidArgument(
        "Attribute cast error in InferMeta Context, the expected attribute "
        "type is `%s`.",
        std::type_index(typeid(AttrType)).name()));
  }
}

template const bool& InferMetaContext::AttrAt(size_t idx) const;
template const int& InferMetaContext::AttrAt(size_t idx) const;
template const int64_t& InferMetaContext::AttrAt(size_t idx) const;
template const float& InferMetaContext::AttrAt(size_t idx) const;
template const double& InferMetaContext::AttrAt(size_t idx) const;
template const std::string& InferMetaContext::AttrAt(size_t idx) const;
template const std::vector<bool>& InferMetaContext::AttrAt(size_t idx) const;
template const std::vector<int>& InferMetaContext::AttrAt(size_t idx) const;
template const std::vector<int64_t>& InferMetaContext::AttrAt(size_t idx) const;
template const std::vector<float>& InferMetaContext::AttrAt(size_t idx) const;
template const std::vector<double>& InferMetaContext::AttrAt(size_t idx) const;
template const std::vector<std::string>& InferMetaContext::AttrAt(
    size_t idx) const;
template const Scalar& InferMetaContext::AttrAt(size_t idx) const;
template const std::vector<Scalar>& InferMetaContext::AttrAt(size_t idx) const;
template const IntArray& InferMetaContext::AttrAt(size_t idx) const;
template const DataType& InferMetaContext::AttrAt(size_t idx) const;
template const DataLayout& InferMetaContext::AttrAt(size_t idx) const;
template const Place& InferMetaContext::AttrAt(size_t idx) const;

C
Chen Weihang 已提交
158 159
MetaFnFactory& MetaFnFactory::Instance() {
  static MetaFnFactory g_meta_fn_map;
160 161 162
  return g_meta_fn_map;
}

163
}  // namespace phi