infershape_utils.cc 28.3 KB
Newer Older
C
Chen Weihang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#include "paddle/fluid/framework/infershape_utils.h"

17
#include <algorithm>
18 19
#include <string>

20
#include "paddle/fluid/framework/convert_utils.h"
C
Chen Weihang 已提交
21
#include "paddle/fluid/framework/framework.pb.h"
22
#include "paddle/fluid/framework/phi_utils.h"
C
Chen Weihang 已提交
23
#include "paddle/fluid/platform/enforce.h"
24
#include "paddle/phi/common/int_array.h"
25
#include "paddle/phi/common/scalar.h"
26 27 28 29 30 31
#include "paddle/phi/core/compat/arg_map_context.h"
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/compat/op_utils.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/core/tensor_utils.h"
C
Chen Weihang 已提交
32 33 34 35

namespace paddle {
namespace framework {

36
class InferShapeArgumentMappingContext : public phi::ArgumentMappingContext {
C
Chen Weihang 已提交
37 38 39 40 41 42 43 44 45 46 47 48
 public:
  explicit InferShapeArgumentMappingContext(const InferShapeContext& ctx)
      : ctx_(ctx) {}

  bool HasInput(const std::string& name) const override {
    return ctx_.HasInput(name);
  }

  bool HasOutput(const std::string& name) const override {
    return ctx_.HasOutput(name);
  }

49 50 51 52
  bool HasAttr(const std::string& name) const override {
    return ctx_.HasAttr(name);
  }

C
Chen Weihang 已提交
53 54 55 56 57 58
  paddle::any Attr(const std::string& name) const override {
    auto& attr = ctx_.Attrs().GetAttr(name);
    return GetAttrValue(attr);
  }

  size_t InputSize(const std::string& name) const override {
59 60 61 62 63 64
    if (ctx_.HasInputs(name)) {
      return ctx_.Inputs(name).size();
    } else if (ctx_.HasInput(name)) {
      return 1;
    }
    return 0;
C
Chen Weihang 已提交
65 66 67 68 69 70 71 72
  }

  size_t OutputSize(const std::string& name) const override {
    return ctx_.Outputs(name).size();
  }

  bool IsDenseTensorInput(const std::string& name) const override {
    auto var_types = ctx_.GetInputsVarType(name);
73 74 75 76
    return std::all_of(var_types.begin(), var_types.end(),
                       [](const proto::VarType::Type& type) {
                         return type == proto::VarType::LOD_TENSOR;
                       });
C
Chen Weihang 已提交
77 78 79 80
  }

  bool IsSelectedRowsInput(const std::string& name) const override {
    auto var_types = ctx_.GetInputsVarType(name);
81 82 83 84
    return std::all_of(var_types.begin(), var_types.end(),
                       [](const proto::VarType::Type& type) {
                         return type == proto::VarType::SELECTED_ROWS;
                       });
C
Chen Weihang 已提交
85 86
  }

87 88
  bool IsDenseTensorVectorInput(const std::string& name) const override {
    auto var_types = ctx_.GetInputsVarType(name);
89 90 91 92
    return std::all_of(var_types.begin(), var_types.end(),
                       [](const proto::VarType::Type& type) {
                         return type == proto::VarType::LOD_TENSOR_ARRAY;
                       });
93 94
  }

95 96
  bool IsDenseTensorOutput(const std::string& name) const override {
    auto var_types = ctx_.GetOutputsVarType(name);
97 98 99 100
    return std::all_of(var_types.begin(), var_types.end(),
                       [](const proto::VarType::Type& type) {
                         return type == proto::VarType::LOD_TENSOR;
                       });
101 102 103 104
  }

  bool IsSelectedRowsOutput(const std::string& name) const override {
    auto var_types = ctx_.GetOutputsVarType(name);
105 106 107 108
    return std::all_of(var_types.begin(), var_types.end(),
                       [](const proto::VarType::Type& type) {
                         return type == proto::VarType::SELECTED_ROWS;
                       });
109 110
  }

111 112
  bool IsForInferShape() const override { return true; }

113 114
  bool IsRuntime() const override { return ctx_.IsRuntime(); }

C
Chen Weihang 已提交
115 116 117 118
 private:
  const InferShapeContext& ctx_;
};

119 120 121 122 123 124 125
int64_t CompatMetaTensor::numel() const {
  if (is_runtime_) {
    auto* var = BOOST_GET_CONST(Variable*, var_);
    return var->Get<Tensor>().numel();
  } else {
    auto* var = BOOST_GET_CONST(VarDesc*, var_);
    return var->ElementSize();
C
Chen Weihang 已提交
126
  }
127
}
C
Chen Weihang 已提交
128

129 130 131 132 133 134 135 136 137 138 139
DDim CompatMetaTensor::dims() const {
  if (is_runtime_) {
    auto* var = BOOST_GET_CONST(Variable*, var_);
    if (var->IsType<phi::DenseTensor>()) {
      return var->Get<phi::DenseTensor>().dims();
    } else if (var->IsType<phi::SelectedRows>()) {
      return var->Get<phi::SelectedRows>().dims();
    } else if (var->IsType<framework::LoDTensorArray>()) {
      // use tensor array size as dims
      auto& tensor_array = var->Get<framework::LoDTensorArray>();
      return phi::make_ddim({static_cast<int64_t>(tensor_array.size())});
C
Chen Weihang 已提交
140
    } else {
141 142 143
      PADDLE_THROW(platform::errors::Unimplemented(
          "Currently, only can get dims from DenseTensor or SelectedRows or "
          "DenseTensorArray."));
C
Chen Weihang 已提交
144
    }
145 146 147 148 149
  } else {
    auto* var = BOOST_GET_CONST(VarDesc*, var_);

    return var->GetShape().empty() ? phi::make_ddim({0UL})
                                   : phi::make_ddim(var->GetShape());
C
Chen Weihang 已提交
150
  }
151
}
C
Chen Weihang 已提交
152

153 154 155 156 157 158 159 160 161 162 163
phi::DataType CompatMetaTensor::dtype() const {
  if (is_runtime_) {
    auto* var = BOOST_GET_CONST(Variable*, var_);
    if (var->IsType<phi::DenseTensor>()) {
      return var->Get<phi::DenseTensor>().dtype();
    } else if (var->IsType<phi::SelectedRows>()) {
      return var->Get<phi::SelectedRows>().dtype();
    } else if (var->IsType<framework::LoDTensorArray>()) {
      // NOTE(chenweihang): do nothing
      // Unsupported get dtype from LoDTensorArray now
      return phi::DataType::UNDEFINED;
C
Chen Weihang 已提交
164
    } else {
165 166
      PADDLE_THROW(platform::errors::Unimplemented(
          "Currently, only can get dtype from DenseTensor or SelectedRows."));
C
Chen Weihang 已提交
167
    }
168 169 170
  } else {
    auto* var = BOOST_GET_CONST(VarDesc*, var_);
    return paddle::framework::TransToPhiDataType(var->GetDataType());
C
Chen Weihang 已提交
171
  }
172
}
C
Chen Weihang 已提交
173

174 175 176 177 178 179 180 181
DataLayout CompatMetaTensor::layout() const {
  if (is_runtime_) {
    auto* var = BOOST_GET_CONST(Variable*, var_);
    if (var->IsType<phi::DenseTensor>()) {
      return var->Get<phi::DenseTensor>().layout();
    } else if (var->IsType<phi::SelectedRows>()) {
      return var->Get<phi::SelectedRows>().layout();
    } else if (var->IsType<framework::LoDTensorArray>()) {
182
      // NOTE(chenweihang): do nothing
183 184 185 186 187 188
      // Unsupported get layout from LoDTensorArray now
      return phi::DataLayout::UNDEFINED;
    } else {
      PADDLE_THROW(platform::errors::Unimplemented(
          "Currently, only can get layout from DenseTensor or "
          "SelectedRows."));
C
Chen Weihang 已提交
189
    }
190 191 192 193
  } else {
    // NOTE(chenweihang): do nothing
    // Unsupported get layout for VarDesc now
    return DataLayout::UNDEFINED;
C
Chen Weihang 已提交
194
  }
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
}

void CompatMetaTensor::set_dims(const DDim& dims) {
  if (is_runtime_) {
    auto* var = BOOST_GET(Variable*, var_);
    if (var->IsType<phi::DenseTensor>()) {
      auto* tensor = var->GetMutable<phi::DenseTensor>();
      phi::DenseTensorUtils::GetMutableMeta(tensor)->dims = dims;
    } else if (var->IsType<phi::SelectedRows>()) {
      auto* tensor = var->GetMutable<phi::SelectedRows>()->mutable_value();
      phi::DenseTensorUtils::GetMutableMeta(tensor)->dims = dims;
    } else if (var->IsType<framework::LoDTensorArray>()) {
      auto* tensor_array = var->GetMutable<framework::LoDTensorArray>();
      // Note: Here I want enforce `tensor_array->size() == 0UL`, because
      // inplace using on LoDTensorArray is dangerous, but the unittest
      // `test_list` contains this behavior
      PADDLE_ENFORCE_EQ(dims.size(), 1UL,
                        platform::errors::InvalidArgument(
                            "LoDTensorArray can only have one dimension."));
      // only set the array size for LoDTensorArray input
      tensor_array->resize(dims[0]);
C
Chen Weihang 已提交
216
    } else {
217 218
      PADDLE_THROW(platform::errors::Unimplemented(
          "Currently, only can set dims from DenseTensor or SelectedRows."));
C
Chen Weihang 已提交
219
    }
220 221 222
  } else {
    auto* var = BOOST_GET(VarDesc*, var_);
    var->SetShape(vectorize(dims));
C
Chen Weihang 已提交
223
  }
224 225 226 227 228 229 230 231 232 233 234 235 236 237
}

void CompatMetaTensor::set_dtype(phi::DataType dtype) {
  if (is_runtime_) {
    auto* var = BOOST_GET(Variable*, var_);
    if (var->IsType<phi::DenseTensor>()) {
      auto* tensor = var->GetMutable<phi::DenseTensor>();
      phi::DenseTensorUtils::GetMutableMeta(tensor)->dtype = dtype;
    } else if (var->IsType<phi::SelectedRows>()) {
      auto* tensor = var->GetMutable<phi::SelectedRows>()->mutable_value();
      phi::DenseTensorUtils::GetMutableMeta(tensor)->dtype = dtype;
    } else if (var->IsType<framework::LoDTensorArray>()) {
      // NOTE(chenweihang): do nothing
      // Unsupported set dtype for LoDTensorArray now
C
Chen Weihang 已提交
238
    } else {
239 240
      PADDLE_THROW(platform::errors::Unimplemented(
          "Currently, only can set dtype from DenseTensor or SelectedRows."));
C
Chen Weihang 已提交
241
    }
242 243 244
  } else {
    auto* var = BOOST_GET(VarDesc*, var_);
    var->SetDataType(paddle::framework::TransToProtoVarType(dtype));
C
Chen Weihang 已提交
245
  }
246 247 248 249 250 251 252 253 254 255 256 257
}

void CompatMetaTensor::set_layout(DataLayout layout) {
  if (is_runtime_) {
    auto* var = BOOST_GET(Variable*, var_);
    if (var->IsType<phi::DenseTensor>()) {
      auto* tensor = var->GetMutable<phi::DenseTensor>();
      phi::DenseTensorUtils::GetMutableMeta(tensor)->layout = layout;
    } else if (var->IsType<phi::SelectedRows>()) {
      auto* tensor = var->GetMutable<phi::SelectedRows>()->mutable_value();
      phi::DenseTensorUtils::GetMutableMeta(tensor)->layout = layout;
    } else if (var->IsType<framework::LoDTensorArray>()) {
258
      // NOTE(chenweihang): do nothing
259 260 261 262 263
      // Unsupported set dtype for LoDTensorArray now
    } else {
      PADDLE_THROW(platform::errors::Unimplemented(
          "Currently, only can set layout from DenseTensor or "
          "SelectedRows."));
C
Chen Weihang 已提交
264
    }
265 266 267
  } else {
    // NOTE(chenweihang): do nothing
    // Unsupported set layout for VarDesc now
C
Chen Weihang 已提交
268
  }
269 270 271 272 273 274 275 276 277
}

void CompatMetaTensor::share_lod(const MetaTensor& meta_tensor) {
  if (is_runtime_) {
    auto* var = BOOST_GET(Variable*, var_);
    if (var->IsType<phi::DenseTensor>()) {
      auto* tensor = var->GetMutable<phi::DenseTensor>();
      phi::DenseTensorUtils::GetMutableMeta(tensor)->lod =
          static_cast<const CompatMetaTensor&>(meta_tensor).GetRuntimeLoD();
C
Chen Weihang 已提交
278
    } else {
279 280
      // NOTE(chenweihang): do nothing
      // only LoDTensor need to share lod
C
Chen Weihang 已提交
281
    }
282 283 284 285
  } else {
    auto* var = BOOST_GET(VarDesc*, var_);
    var->SetLoDLevel(
        static_cast<const CompatMetaTensor&>(meta_tensor).GetCompileTimeLoD());
C
Chen Weihang 已提交
286
  }
287 288 289 290 291 292 293 294 295 296 297 298
}

void CompatMetaTensor::share_dims(const MetaTensor& meta_tensor) {
  set_dims(meta_tensor.dims());
  if (is_runtime_) {
    auto* var = BOOST_GET(Variable*, var_);
    if (var->IsType<phi::SelectedRows>()) {
      auto* selected_rows = var->GetMutable<phi::SelectedRows>();
      auto& input_selected_rows =
          static_cast<const CompatMetaTensor&>(meta_tensor).GetSelectedRows();
      selected_rows->set_rows(input_selected_rows.rows());
      selected_rows->set_height(input_selected_rows.height());
299
    }
300
  }
301 302 303 304 305 306 307 308 309
}

void CompatMetaTensor::share_meta(const MetaTensor& meta_tensor) {
  share_dims(meta_tensor);
  set_dtype(meta_tensor.dtype());
  set_layout(meta_tensor.layout());
  // special case: share lod of LoDTensor
  share_lod(meta_tensor);
}
C
Chen Weihang 已提交
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
void CompatInferMetaContext::EmplaceBackInput(CompatMetaTensor input) {
  int index = compat_inputs_.size();
  compat_inputs_.emplace_back(std::move(input));
  input_range_.emplace_back(std::pair<int, int>(index, index + 1));
}
void CompatInferMetaContext::EmplaceBackOutput(CompatMetaTensor output) {
  int index = compat_outputs_.size();
  compat_outputs_.emplace_back(std::move(output));
  output_range_.emplace_back(std::pair<int, int>(index, index + 1));
}

void CompatInferMetaContext::EmplaceBackInputs(
    paddle::SmallVector<CompatMetaTensor, phi::kInputSmallVectorSize> inputs) {
  int index = compat_inputs_.size();
  input_range_.emplace_back(std::pair<int, int>(index, index + inputs.size()));
  compat_inputs_.insert(compat_inputs_.end(),
                        std::make_move_iterator(inputs.begin()),
                        std::make_move_iterator(inputs.end()));
}

void CompatInferMetaContext::EmplaceBackOutputs(
    paddle::SmallVector<CompatMetaTensor, phi::kOutputSmallVectorSize>
        outputs) {
  int index = compat_outputs_.size();
  output_range_.emplace_back(
      std::pair<int, int>(index, index + outputs.size()));
  compat_outputs_.insert(compat_outputs_.end(),
                         std::make_move_iterator(outputs.begin()),
                         std::make_move_iterator(outputs.end()));
}

const phi::MetaTensor& CompatInferMetaContext::InputAt(size_t idx) const {
  return compat_inputs_.at(idx);
}

paddle::optional<const phi::MetaTensor&>
CompatInferMetaContext::OptionalInputAt(size_t idx) const {
  const auto& input = compat_inputs_.at(idx);
  return input.initialized()
             ? paddle::optional<const phi::MetaTensor&>{input}
             : paddle::optional<const phi::MetaTensor&>{paddle::none};
}

std::vector<const phi::MetaTensor*> CompatInferMetaContext::InputsBetween(
    size_t start, size_t end) const {
  std::vector<const phi::MetaTensor*> result;
  result.reserve(end - start);

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

  return result;
}

paddle::optional<const std::vector<const phi::MetaTensor*>>
CompatInferMetaContext::OptionalInputsBetween(size_t start, size_t end) const {
  const auto& first = compat_inputs_.at(start);

  if (first.initialized()) {
    std::vector<const phi::MetaTensor*> result;
    result.reserve(end - start);

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

    return paddle::optional<const std::vector<const phi::MetaTensor*>>(result);
  }
  return paddle::optional<const std::vector<const phi::MetaTensor*>>(
      paddle::none);
}

phi::MetaTensor* CompatInferMetaContext::MutableOutputAt(size_t idx) {
  auto& out = compat_outputs_.at(idx);
  return out.initialized() ? &out : nullptr;
}

std::vector<phi::MetaTensor*> CompatInferMetaContext::MutableOutputBetween(
    size_t start, size_t end) {
  std::vector<phi::MetaTensor*> result;
  result.reserve(end - start);
  for (size_t i = start; i < end; ++i) {
    auto& out = compat_outputs_.at(i);
    result.emplace_back(out.initialized() ? &out : nullptr);
  }
  return result;
}

CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx,
                                             const std::string& op_type) {
404
  // 1. get kernel args
405
  auto* arg_map_fn = ctx->GetPhiArgumentMappingFn();
406
  InferShapeArgumentMappingContext arg_map_context(*ctx);
407 408 409
  phi::KernelSignature signature = arg_map_fn
                                       ? (*arg_map_fn)(arg_map_context)
                                       : *ctx->GetPhiDefaultKernelSignature();
410 411 412
  VLOG(3) << "BuildInferMetaContext: op kernel signature - " << signature;

  // 2. build infermeta context
413
  CompatInferMetaContext infer_meta_context(
F
From00 已提交
414
      {ctx->IsRuntime(), ctx->IsRunMKLDNNKernel()});
415

416 417 418
  const auto& input_names = signature.input_names;
  const auto& attr_names = signature.attr_names;
  const auto& output_names = signature.output_names;
419

420 421 422
  const auto& args_def =
      phi::KernelFactory::Instance().GetFirstKernelArgsDef(signature.name);
  const auto& attr_defs = args_def.attribute_defs();
423

424
  for (auto& in_name : input_names) {
425
    if (ctx->HasInputs(in_name)) {
426
      auto input_var = std::move(ctx->GetInputVarPtrs(in_name));
427 428
      if (input_var.size() == 1) {
        infer_meta_context.EmplaceBackInput(
429
            std::move(CompatMetaTensor(input_var[0], ctx->IsRuntime())));
430
      } else {
431 432
        paddle::SmallVector<CompatMetaTensor, phi::kInputSmallVectorSize>
            inputs;
433
        for (const auto& in : input_var) {
434 435
          inputs.emplace_back(
              std::move(CompatMetaTensor(in, ctx->IsRuntime())));
436 437 438
        }
        infer_meta_context.EmplaceBackInputs(std::move(inputs));
      }
439
    } else {
440 441
      infer_meta_context.EmplaceBackInput(
          std::move(CompatMetaTensor(ctx->IsRuntime())));
442
    }
443
  }
444

445 446
  VLOG(6) << "BuildInferMetaContext: Done inputs";

447
  auto attr_reader = ctx->Attrs();
448
  for (size_t i = 0; i < attr_names.size(); ++i) {
449
    auto& attr_name = attr_names[i];
450 451
    if (attr_defs[i].type_index == std::type_index(typeid(phi::IntArray))) {
      // When attr is a vector_tensor or tensor, transform it to IntArray
452
      if (ctx->HasInputs(attr_name) || ctx->HasInput(attr_name)) {
453
        auto infershape_inputs = std::move(ctx->GetInputVarPtrs(attr_name));
454
        if (ctx->IsRuntime()) {
455
          // If is in runtime, we will get tensor's value for IntArray
456 457 458 459 460 461 462 463
          // and push it into attrs
          std::vector<Variable*> vars;
          vars.reserve(infershape_inputs.size());
          for (size_t i = 0; i < infershape_inputs.size(); i++) {
            vars.push_back(BOOST_GET_CONST(Variable*, infershape_inputs[i]));
          }
          if (infershape_inputs.size() != 1) {
            infer_meta_context.EmplaceBackAttr(
464
                std::move(experimental::MakePhiIntArrayFromVarList(vars)));
465 466
          } else {
            infer_meta_context.EmplaceBackAttr(
467
                std::move(experimental::MakePhiIntArrayFromVar(*vars[0])));
468 469
          }
        } else {
470
          // If is not in runtime, we will set default value(-1) for IntArray
471 472
          std::vector<VarDesc*> vars;
          vars.reserve(infershape_inputs.size());
473
          for (size_t i = 0; i < infershape_inputs.size(); ++i) {
474 475
            vars.push_back(BOOST_GET_CONST(VarDesc*, infershape_inputs[i]));
          }
476

477
          int64_t num_ele = 0;
478 479 480
          if (vars.size() == 1) {
            num_ele = 1;
            const auto& tensor_dims = vars[0]->GetShape();
481 482 483
            for (size_t i = 0; i < tensor_dims.size(); ++i) {
              num_ele *= tensor_dims[i];
            }
484 485 486

            if (num_ele <= 0) {
              PADDLE_THROW(platform::errors::Unimplemented(
487
                  "Invalid number for construct phi::IntArray, expected "
488 489 490 491
                  "number > 0, but actually is %d. ",
                  num_ele));
            }

492
          } else {
493
            num_ele = vars.size();
494
          }
495
          phi::IntArray tensor_attr(std::vector<int32_t>(num_ele, -1));
496 497 498 499 500 501 502 503
          tensor_attr.SetFromTensor(true);
          infer_meta_context.EmplaceBackAttr(std::move(tensor_attr));
        }
      } else if (ctx->HasAttr(attr_name)) {
        auto& attr = attr_reader.GetAttr(attr_name);
        if (std::type_index(attr.type()) ==
            std::type_index(typeid(std::vector<int32_t>))) {
          infer_meta_context.EmplaceBackAttr(std::move(
504
              phi::IntArray(BOOST_GET_CONST(std::vector<int32_t>, attr))));
505 506 507
        } else if (std::type_index(attr.type()) ==
                   std::type_index(typeid(std::vector<int64_t>))) {
          infer_meta_context.EmplaceBackAttr(std::move(
508
              phi::IntArray(BOOST_GET_CONST(std::vector<int64_t>, attr))));
509 510 511
        } else if (std::type_index(attr.type()) ==
                   std::type_index(typeid(int))) {
          infer_meta_context.EmplaceBackAttr(
512
              phi::IntArray({BOOST_GET_CONST(int, attr)}));
513 514
        } else {
          PADDLE_THROW(platform::errors::Unimplemented(
515
              "Unsupported cast op attribute `%s` to IntArray when "
516
              "construct InferMetaContext.",
517 518 519
              attr_name));
        }
      }
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    } else if (attr_defs[i].type_index ==
               std::type_index(typeid(phi::Scalar))) {
      if (ctx->HasAttr(attr_name)) {
        // TODO(chentianyu03): support other attrs later
        auto& attr = attr_reader.GetAttr(attr_name);
        if (std::type_index(attr.type()) == std::type_index(typeid(float))) {
          infer_meta_context.EmplaceBackAttr(
              phi::Scalar(BOOST_GET_CONST(float, attr)));
        } else if (std::type_index(attr.type()) ==
                   std::type_index(typeid(std::string))) {
          infer_meta_context.EmplaceBackAttr(
              phi::Scalar(BOOST_GET_CONST(std::string, attr)));
        } else if (std::type_index(attr.type()) ==
                   std::type_index(typeid(int))) {
          infer_meta_context.EmplaceBackAttr(
              phi::Scalar(BOOST_GET_CONST(int, attr)));
        } else {
          PADDLE_THROW(platform::errors::Unimplemented(
              "Unsupported cast op attribute `%s` to Scalar when construct "
              "InferMetaContext.",
              attr_name));
        }
      } else if (ctx->HasInput(attr_name)) {
543
        auto infershape_input = std::move(ctx->GetInputVarPtrs(attr_name));
544 545 546 547
        if (infershape_input.size() == 1) {
          if (ctx->IsRuntime()) {
            Variable* var = BOOST_GET_CONST(Variable*, infershape_input[0]);
            infer_meta_context.EmplaceBackAttr(
548
                std::move(experimental::MakePhiScalarFromVar(*var)));
549 550 551 552 553 554 555 556 557 558 559 560
          } else {
            phi::Scalar tensor_scalar(-1);
            tensor_scalar.SetFromTensor(true);
            infer_meta_context.EmplaceBackAttr(std::move(tensor_scalar));
          }
        } else {
          PADDLE_THROW(platform::errors::InvalidArgument(
              "Invalid input.size() when cast op attribute `%s` to Scalar, "
              "expected 1, but actually is %d .",
              attr_name, infershape_input.size()));
        }
      }
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
    } else if (attr_defs[i].type_index ==
               std::type_index(typeid(std::vector<phi::Scalar>))) {
      auto& attr = attr_reader.GetAttr(attr_name);
      if (std::type_index(attr.type()) ==
          std::type_index(typeid(std::vector<int32_t>))) {
        const auto& vec = BOOST_GET_CONST(std::vector<int32_t>, attr);
        std::vector<phi::Scalar> scalar_list;
        scalar_list.reserve(vec.size());
        for (const auto& val : vec) {
          scalar_list.emplace_back(val);
        }
        infer_meta_context.EmplaceBackAttr(std::move(scalar_list));
      } else if (std::type_index(attr.type()) ==
                 std::type_index(typeid(std::vector<int64_t>))) {
        const auto& vec = BOOST_GET_CONST(std::vector<int64_t>, attr);
        std::vector<phi::Scalar> scalar_list;
        scalar_list.reserve(vec.size());
        for (const auto& val : vec) {
          scalar_list.emplace_back(val);
        }
        infer_meta_context.EmplaceBackAttr(std::move(scalar_list));
      } else if (std::type_index(attr.type()) ==
                 std::type_index(typeid(std::vector<float>))) {
        const auto& vec = BOOST_GET_CONST(std::vector<float>, attr);
        std::vector<phi::Scalar> scalar_list;
        scalar_list.reserve(vec.size());
        for (const auto& val : vec) {
          scalar_list.emplace_back(val);
        }
        infer_meta_context.EmplaceBackAttr(std::move(scalar_list));
      } else if (std::type_index(attr.type()) ==
                 std::type_index(typeid(std::vector<double>))) {
        const auto& vec = BOOST_GET_CONST(std::vector<double>, attr);
        std::vector<phi::Scalar> scalar_list;
        scalar_list.reserve(vec.size());
        for (const auto& val : vec) {
          scalar_list.emplace_back(val);
        }
        infer_meta_context.EmplaceBackAttr(std::move(scalar_list));
      } else {
        PADDLE_THROW(platform::errors::Unimplemented(
            "Unsupported cast op attribute `%s` to vector<Scalar> when "
            "construct InferMetaContext.",
            attr_names[i]));
      }
606 607
    } else if (ctx->HasAttr(attr_name)) {
      // Emplace Back Attr according to the type of attr.
608
      auto& attr = attr_reader.GetAttr(attr_name);
609
      if (attr_defs[i].type_index == std::type_index(typeid(bool))) {
610
        infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(bool, attr));
611
      } else if (attr_defs[i].type_index == std::type_index(typeid(int))) {
612
        infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(int, attr));
613
      } else if (attr_defs[i].type_index == std::type_index(typeid(int64_t))) {
614
        infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(int64_t, attr));
615
      } else if (attr_defs[i].type_index == std::type_index(typeid(float))) {
616
        infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(float, attr));
617
      } else if (attr_defs[i].type_index ==
618 619
                 std::type_index(typeid(std::string))) {
        infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(std::string, attr));
620
      } else if (attr_defs[i].type_index ==
621 622 623
                 std::type_index(typeid(std::vector<bool>))) {
        infer_meta_context.EmplaceBackAttr(
            BOOST_GET_CONST(std::vector<bool>, attr));
624
      } else if (attr_defs[i].type_index ==
625 626 627
                 std::type_index(typeid(std::vector<int>))) {
        infer_meta_context.EmplaceBackAttr(
            BOOST_GET_CONST(std::vector<int>, attr));
628
      } else if (attr_defs[i].type_index ==
629
                 std::type_index(typeid(std::vector<int64_t>))) {
630 631 632 633 634 635 636 637 638 639 640 641
        if (std::type_index(attr.type()) ==
            std::type_index(typeid(std::vector<int>))) {
          // Emplace Back Attr according to the type of Phi_Kernel args.
          const auto& vector_int_attr = BOOST_GET_CONST(std::vector<int>, attr);
          const std::vector<int64_t> vector_int64_attr(vector_int_attr.begin(),
                                                       vector_int_attr.end());
          infer_meta_context.EmplaceBackAttr(vector_int64_attr);
        } else {
          infer_meta_context.EmplaceBackAttr(
              BOOST_GET_CONST(std::vector<int64_t>, attr));
        }
      } else if (attr_defs[i].type_index ==
642 643 644
                 std::type_index(typeid(std::vector<float>))) {
        infer_meta_context.EmplaceBackAttr(
            BOOST_GET_CONST(std::vector<float>, attr));
645
      } else if (attr_defs[i].type_index ==
646 647 648
                 std::type_index(typeid(std::vector<double>))) {
        infer_meta_context.EmplaceBackAttr(
            BOOST_GET_CONST(std::vector<double>, attr));
649
      } else if (attr_defs[i].type_index ==
650 651 652
                 std::type_index(typeid(std::vector<std::string>))) {
        infer_meta_context.EmplaceBackAttr(
            BOOST_GET_CONST(std::vector<std::string>, attr));
653 654
      } else if (attr_defs[i].type_index ==
                 std::type_index(typeid(phi::DataType))) {
655
        auto data_type = paddle::framework::TransToPhiDataType(
656 657 658
            static_cast<framework::proto::VarType::Type>(
                BOOST_GET_CONST(int, attr)));
        infer_meta_context.EmplaceBackAttr(data_type);
659
      } else {
660 661 662
        PADDLE_THROW(platform::errors::Unimplemented(
            "Unsupported attribute type is received when call "
            "InferShapeFunctor."));
663
      }
H
hong 已提交
664 665 666 667
    } else if (ctx->HasInput(attr_name)) {
      // convert from data
      if (attr_defs[i].type_index == std::type_index(typeid(int32_t))) {
        if (ctx->IsRuntime()) {
668
          auto infershape_inputs = std::move(ctx->GetInputVarPtrs(attr_name));
H
hong 已提交
669 670 671 672 673 674 675 676 677 678 679
          auto var_temp = BOOST_GET_CONST(Variable*, infershape_inputs[i]);
          auto val = experimental::MakePhiScalarFromVar(*var_temp);
          int32_t val_int = val.template to<int32_t>();
          infer_meta_context.EmplaceBackAttr(val_int);
        } else {
          infer_meta_context.EmplaceBackAttr(-1);
        }
      } else {
        PADDLE_THROW(platform::errors::Unimplemented(
            "Get value from variable only support int yet"));
      }
680 681 682
    }
  }

683 684
  VLOG(6) << "BuildInferMetaContext: Done attrs";

685
  for (auto& out_name : output_names) {
686
    if (ctx->HasOutputs(out_name, true)) {
687
      auto output_var = std::move(ctx->GetOutputVarPtrs(out_name));
688
      if (output_var.size() == 1) {
689 690
        infer_meta_context.EmplaceBackOutput(
            std::move(CompatMetaTensor(output_var[0], ctx->IsRuntime())));
691
      } else {
692 693
        paddle::SmallVector<CompatMetaTensor, phi::kOutputSmallVectorSize>
            outputs;
694
        for (const auto& out : output_var) {
695 696 697
          if (ctx->IsRuntime()) {
            if (BOOST_GET_CONST(Variable*, out)) {
              outputs.emplace_back(
698
                  std::move(CompatMetaTensor(out, ctx->IsRuntime())));
699 700 701 702
              continue;
            }
          } else if (BOOST_GET_CONST(VarDesc*, out)) {
            outputs.emplace_back(
703
                std::move(CompatMetaTensor(out, ctx->IsRuntime())));
704 705
            continue;
          }
706
          outputs.emplace_back(std::move(CompatMetaTensor(ctx->IsRuntime())));
707 708 709 710
        }
        infer_meta_context.EmplaceBackOutputs(std::move(outputs));
      }
    } else {
711 712
      infer_meta_context.EmplaceBackOutput(
          std::move(CompatMetaTensor(ctx->IsRuntime())));
713
    }
714 715
  }

716 717
  VLOG(6) << "BuildInferMetaContext: Done outputs";

718 719 720
  return infer_meta_context;
}

C
Chen Weihang 已提交
721 722
}  // namespace framework
}  // namespace paddle