infershape_utils.cc 21.2 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 18
#include <string>

19
#include "paddle/fluid/framework/convert_utils.h"
C
Chen Weihang 已提交
20
#include "paddle/fluid/framework/framework.pb.h"
21
#include "paddle/fluid/framework/phi_utils.h"
C
Chen Weihang 已提交
22
#include "paddle/fluid/platform/enforce.h"
23
#include "paddle/phi/common/scalar.h"
24
#include "paddle/phi/common/scalar_array.h"
25 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/meta_tensor.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 73 74 75 76 77 78 79 80
  }

  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);
    return var_types[0] == proto::VarType::LOD_TENSOR;
  }

  bool IsSelectedRowsInput(const std::string& name) const override {
    auto var_types = ctx_.GetInputsVarType(name);
    return var_types[0] == proto::VarType::SELECTED_ROWS;
  }

81 82 83 84 85 86 87 88 89 90
  bool IsDenseTensorOutput(const std::string& name) const override {
    auto var_types = ctx_.GetOutputsVarType(name);
    return var_types[0] == proto::VarType::LOD_TENSOR;
  }

  bool IsSelectedRowsOutput(const std::string& name) const override {
    auto var_types = ctx_.GetOutputsVarType(name);
    return var_types[0] == proto::VarType::SELECTED_ROWS;
  }

91 92
  bool IsForInferShape() const override { return true; }

93 94
  bool IsRuntime() const override { return ctx_.IsRuntime(); }

C
Chen Weihang 已提交
95 96 97 98 99
 private:
  const InferShapeContext& ctx_;
};

// TODO(chenweihang): Support TensorArray later
100
class CompatMetaTensor : public phi::MetaTensor {
C
Chen Weihang 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
 public:
  CompatMetaTensor(InferShapeVarPtr var, bool is_runtime)
      : var_(std::move(var)), is_runtime_(is_runtime) {}

  CompatMetaTensor() = default;
  CompatMetaTensor(const CompatMetaTensor&) = default;
  CompatMetaTensor(CompatMetaTensor&&) = default;
  CompatMetaTensor& operator=(const CompatMetaTensor&) = delete;
  CompatMetaTensor& operator=(CompatMetaTensor&&) = delete;

  int64_t numel() const override {
    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();
    }
  }

  DDim dims() const override {
    if (is_runtime_) {
      auto* var = BOOST_GET_CONST(Variable*, var_);
124 125 126 127
      if (var->IsType<phi::DenseTensor>()) {
        return var->Get<phi::DenseTensor>().dims();
      } else if (var->IsType<phi::SelectedRows>()) {
        return var->Get<phi::SelectedRows>().dims();
128 129 130 131
      } else {
        PADDLE_THROW(platform::errors::Unimplemented(
            "Currently, only can get dims from DenseTensor or SelectedRows."));
      }
C
Chen Weihang 已提交
132 133
    } else {
      auto* var = BOOST_GET_CONST(VarDesc*, var_);
134 135 136

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

140
  phi::DataType dtype() const override {
C
Chen Weihang 已提交
141 142
    if (is_runtime_) {
      auto* var = BOOST_GET_CONST(Variable*, var_);
143 144 145 146
      if (var->IsType<phi::DenseTensor>()) {
        return var->Get<phi::DenseTensor>().dtype();
      } else if (var->IsType<phi::SelectedRows>()) {
        return var->Get<phi::SelectedRows>().dtype();
147 148 149 150
      } else {
        PADDLE_THROW(platform::errors::Unimplemented(
            "Currently, only can get dtype from DenseTensor or SelectedRows."));
      }
C
Chen Weihang 已提交
151 152
    } else {
      auto* var = BOOST_GET_CONST(VarDesc*, var_);
153
      return paddle::framework::TransToPhiDataType(var->GetDataType());
C
Chen Weihang 已提交
154 155 156 157 158 159 160 161
    }
  }

  DataLayout layout() const override {
    if (is_runtime_) {
      auto* var = BOOST_GET_CONST(Variable*, var_);
      return var->Get<LoDTensor>().layout();
    } else {
162 163 164
      // NOTE(chenweihang): do nothing
      // Unsupported get layout for VarDesc now
      return DataLayout::UNDEFINED;
C
Chen Weihang 已提交
165 166 167 168 169 170
    }
  }

  void set_dims(const DDim& dims) override {
    if (is_runtime_) {
      auto* var = BOOST_GET(Variable*, var_);
171 172 173 174 175 176
      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;
177 178 179 180
      } else {
        PADDLE_THROW(platform::errors::Unimplemented(
            "Currently, only can set dims from DenseTensor or SelectedRows."));
      }
C
Chen Weihang 已提交
181 182 183 184 185 186
    } else {
      auto* var = BOOST_GET(VarDesc*, var_);
      var->SetShape(vectorize(dims));
    }
  }

187
  void set_dtype(phi::DataType dtype) override {
C
Chen Weihang 已提交
188 189
    if (is_runtime_) {
      auto* var = BOOST_GET(Variable*, var_);
190 191 192 193 194 195
      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;
196 197 198 199
      } else {
        PADDLE_THROW(platform::errors::Unimplemented(
            "Currently, only can set dtype from DenseTensor or SelectedRows."));
      }
C
Chen Weihang 已提交
200 201
    } else {
      auto* var = BOOST_GET(VarDesc*, var_);
202
      var->SetDataType(paddle::framework::TransToProtoVarType(dtype));
C
Chen Weihang 已提交
203 204 205 206 207 208 209
    }
  }

  void set_layout(DataLayout layout) override {
    if (is_runtime_) {
      auto* var = BOOST_GET(Variable*, var_);
      LoDTensor* tensor = var->GetMutable<LoDTensor>();
210 211
      phi::DenseTensorUtils::GetMutableMeta(
          static_cast<phi::DenseTensor*>(tensor))
C
Chen Weihang 已提交
212 213
          ->layout = layout;
    } else {
214 215
      // NOTE(chenweihang): do nothing
      // Unsupported set layout for VarDesc now
C
Chen Weihang 已提交
216 217 218 219 220 221
    }
  }

  void share_lod(const MetaTensor& meta_tensor) override {
    if (is_runtime_) {
      auto* var = BOOST_GET(Variable*, var_);
222 223 224
      if (var->IsType<phi::DenseTensor>()) {
        auto* tensor = var->GetMutable<phi::DenseTensor>();
        phi::DenseTensorUtils::GetMutableMeta(tensor)->lod =
225 226 227 228 229
            static_cast<const CompatMetaTensor&>(meta_tensor).GetRuntimeLoD();
      } else {
        // NOTE(chenweihang): do nothing
        // only LoDTensor need to share lod
      }
C
Chen Weihang 已提交
230 231 232 233 234 235 236
    } else {
      auto* var = BOOST_GET(VarDesc*, var_);
      var->SetLoDLevel(static_cast<const CompatMetaTensor&>(meta_tensor)
                           .GetCompileTimeLoD());
    }
  }

Y
YuanRisheng 已提交
237
  void share_dims(const MetaTensor& meta_tensor) override {
238
    set_dims(meta_tensor.dims());
239 240
    if (is_runtime_) {
      auto* var = BOOST_GET(Variable*, var_);
241 242
      if (var->IsType<phi::SelectedRows>()) {
        auto* selected_rows = var->GetMutable<phi::SelectedRows>();
243 244 245 246 247 248
        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());
      }
    }
249 250
  }

Y
YuanRisheng 已提交
251
  void share_meta(const MetaTensor& meta_tensor) override {
C
Chen Weihang 已提交
252
    share_dims(meta_tensor);
Y
YuanRisheng 已提交
253 254 255 256
    set_dtype(meta_tensor.dtype());
    // VarDesc doesn't contains layout, so we cannot share layout
    // set_layout(meta_tensor.layout());

C
Chen Weihang 已提交
257
    // special case: share lod of LoDTensor
Y
YuanRisheng 已提交
258 259 260
    share_lod(meta_tensor);
  }

C
Chen Weihang 已提交
261 262 263 264 265
 private:
  const LoD& GetRuntimeLoD() const {
    auto* var = BOOST_GET_CONST(Variable*, var_);
    return var->Get<LoDTensor>().lod();
  }
266

C
Chen Weihang 已提交
267 268 269 270 271
  int32_t GetCompileTimeLoD() const {
    auto* var = BOOST_GET_CONST(VarDesc*, var_);
    return var->GetLoDLevel();
  }

272
  const phi::SelectedRows& GetSelectedRows() const {
273 274 275 276
    PADDLE_ENFORCE_EQ(is_runtime_, true,
                      platform::errors::Unavailable(
                          "Only can get Tensor from MetaTensor in rumtime."));
    auto* var = BOOST_GET_CONST(Variable*, var_);
277
    PADDLE_ENFORCE_EQ(var->IsType<phi::SelectedRows>(), true,
278 279
                      platform::errors::Unavailable(
                          "The Tensor in MetaTensor is not SelectedRows."));
280
    return var->Get<phi::SelectedRows>();
281 282
  }

C
Chen Weihang 已提交
283 284 285 286
  InferShapeVarPtr var_;
  bool is_runtime_;
};

287 288
phi::InferMetaContext BuildInferMetaContext(InferShapeContext* ctx,
                                            const std::string& op_type) {
289 290
  // 1. get kernel args
  InitDefaultKernelSignatureMap();
291
  auto arg_map_fn = phi::OpUtilsMap::Instance().GetArgumentMappingFn(op_type);
292 293 294 295 296 297 298 299
  PADDLE_ENFORCE_NOT_NULL(
      arg_map_fn, platform::errors::NotFound(
                      "The ArgumentMappingFn of %s op is not found.", op_type));
  InferShapeArgumentMappingContext arg_map_context(*ctx);
  auto signature = arg_map_fn(arg_map_context);
  VLOG(3) << "BuildInferMetaContext: op kernel signature - " << signature;

  // 2. build infermeta context
F
From00 已提交
300 301
  phi::InferMetaContext infer_meta_context(
      {ctx->IsRuntime(), ctx->IsRunMKLDNNKernel()});
302 303

  auto& input_names = std::get<0>(signature.args);
304
  auto& attr_names = std::get<1>(signature.args);
305 306
  auto& output_names = std::get<2>(signature.args);

307 308 309 310 311 312 313 314 315 316
  auto kernels_map =
      phi::KernelFactory::Instance().SelectKernelMap(signature.name);
  if (kernels_map.size() == 0) {
    PADDLE_THROW(
        platform::errors::Unimplemented("Not find `%s` kernels when construct "
                                        "InferMetaContext.",
                                        signature.name));
  }
  auto attr_defs = kernels_map.cbegin()->second.args_def().attribute_defs();

317
  // TODO(chenweihang): support multiple inputs and outputs later
318
  phi::InferMetaContext infer_mete_context;
319
  for (auto& in_name : input_names) {
320 321 322 323 324 325 326 327 328 329 330 331 332 333
    if (ctx->HasInputs(in_name)) {
      auto input_var = ctx->GetInputVarPtrs(in_name);
      if (input_var.size() == 1) {
        infer_meta_context.EmplaceBackInput(
            std::make_shared<CompatMetaTensor>(input_var[0], ctx->IsRuntime()));
      } else {
        paddle::SmallVector<std::shared_ptr<phi::MetaTensor>> inputs;
        inputs.reserve(input_var.size());
        for (const auto& in : input_var) {
          inputs.push_back(
              std::make_shared<CompatMetaTensor>(in, ctx->IsRuntime()));
        }
        infer_meta_context.EmplaceBackInputs(std::move(inputs));
      }
334 335 336
    } else {
      infer_meta_context.EmplaceBackInput({nullptr});
    }
337
  }
338 339

  auto attr_reader = ctx->Attrs();
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
  for (size_t i = 0; i < attr_names.size(); ++i) {
    auto attr_name = attr_names[i];
    if (attr_defs[i].type_index == std::type_index(typeid(phi::ScalarArray))) {
      // When attr is a vector_tensor or tensor, transform it to ScalarArray
      if (ctx->HasInputs(attr_name) || ctx->HasInput(attr_name)) {
        const auto& infershape_inputs = ctx->GetInputVarPtrs(attr_name);
        if (ctx->IsRuntime()) {
          // If is in runtime, we will get tensor's value for ScalarArray
          // 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(
356
                std::move(experimental::MakePhiScalarArrayFromVarList(vars)));
357 358
          } else {
            infer_meta_context.EmplaceBackAttr(
359
                std::move(experimental::MakePhiScalarArrayFromVar(*vars[0])));
360 361 362 363 364
          }
        } else {
          // If is not in runtime, we will set default value(-1) for ScalarArray
          std::vector<VarDesc*> vars;
          vars.reserve(infershape_inputs.size());
365
          for (size_t i = 0; i < infershape_inputs.size(); ++i) {
366 367
            vars.push_back(BOOST_GET_CONST(VarDesc*, infershape_inputs[i]));
          }
368

369
          int64_t num_ele = 0;
370 371 372
          if (vars.size() == 1) {
            num_ele = 1;
            const auto& tensor_dims = vars[0]->GetShape();
373 374 375
            for (size_t i = 0; i < tensor_dims.size(); ++i) {
              num_ele *= tensor_dims[i];
            }
376
          } else {
377
            num_ele = vars.size();
378 379 380 381 382 383 384 385 386 387 388
          }
          phi::ScalarArray tensor_attr(std::vector<int32_t>(num_ele, -1));
          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(
              phi::ScalarArray(BOOST_GET_CONST(std::vector<int32_t>, attr))));
389 390 391 392
        } else if (std::type_index(attr.type()) ==
                   std::type_index(typeid(std::vector<int64_t>))) {
          infer_meta_context.EmplaceBackAttr(std::move(
              phi::ScalarArray(BOOST_GET_CONST(std::vector<int64_t>, attr))));
393 394 395 396
        } else if (std::type_index(attr.type()) ==
                   std::type_index(typeid(int))) {
          infer_meta_context.EmplaceBackAttr(
              phi::ScalarArray({BOOST_GET_CONST(int, attr)}));
397 398 399
        } else {
          PADDLE_THROW(platform::errors::Unimplemented(
              "Unsupported cast op attribute `%s` to ScalarArray when "
400
              "construct InferMetaContext.",
401 402 403
              attr_name));
        }
      }
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
    } 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)) {
        const auto& infershape_input = ctx->GetInputVarPtrs(attr_name);
        if (infershape_input.size() == 1) {
          if (ctx->IsRuntime()) {
            Variable* var = BOOST_GET_CONST(Variable*, infershape_input[0]);
            infer_meta_context.EmplaceBackAttr(
432
                std::move(experimental::MakePhiScalarFromVar(*var)));
433 434 435 436 437 438 439 440 441 442 443 444
          } 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()));
        }
      }
445 446
    } else if (ctx->HasAttr(attr_name)) {
      // Emplace Back Attr according to the type of attr.
447
      auto& attr = attr_reader.GetAttr(attr_name);
448
      if (attr_defs[i].type_index == std::type_index(typeid(bool))) {
449
        infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(bool, attr));
450
      } else if (attr_defs[i].type_index == std::type_index(typeid(int))) {
451
        infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(int, attr));
452
      } else if (attr_defs[i].type_index == std::type_index(typeid(int64_t))) {
453
        infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(int64_t, attr));
454
      } else if (attr_defs[i].type_index == std::type_index(typeid(float))) {
455
        infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(float, attr));
456
      } else if (attr_defs[i].type_index ==
457 458
                 std::type_index(typeid(std::string))) {
        infer_meta_context.EmplaceBackAttr(BOOST_GET_CONST(std::string, attr));
459
      } else if (attr_defs[i].type_index ==
460 461 462
                 std::type_index(typeid(std::vector<bool>))) {
        infer_meta_context.EmplaceBackAttr(
            BOOST_GET_CONST(std::vector<bool>, attr));
463
      } else if (attr_defs[i].type_index ==
464 465 466
                 std::type_index(typeid(std::vector<int>))) {
        infer_meta_context.EmplaceBackAttr(
            BOOST_GET_CONST(std::vector<int>, attr));
467
      } else if (attr_defs[i].type_index ==
468
                 std::type_index(typeid(std::vector<int64_t>))) {
469 470 471 472 473 474 475 476 477 478 479 480
        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 ==
481 482 483
                 std::type_index(typeid(std::vector<float>))) {
        infer_meta_context.EmplaceBackAttr(
            BOOST_GET_CONST(std::vector<float>, attr));
484
      } else if (attr_defs[i].type_index ==
485 486 487
                 std::type_index(typeid(std::vector<double>))) {
        infer_meta_context.EmplaceBackAttr(
            BOOST_GET_CONST(std::vector<double>, attr));
488
      } else if (attr_defs[i].type_index ==
489 490 491
                 std::type_index(typeid(std::vector<std::string>))) {
        infer_meta_context.EmplaceBackAttr(
            BOOST_GET_CONST(std::vector<std::string>, attr));
492 493
      } else if (attr_defs[i].type_index ==
                 std::type_index(typeid(phi::DataType))) {
494
        auto data_type = paddle::framework::TransToPhiDataType(
495 496 497
            static_cast<framework::proto::VarType::Type>(
                BOOST_GET_CONST(int, attr)));
        infer_meta_context.EmplaceBackAttr(data_type);
498
      } else {
499 500 501
        PADDLE_THROW(platform::errors::Unimplemented(
            "Unsupported attribute type is received when call "
            "InferShapeFunctor."));
502
      }
H
hong 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
    } else if (ctx->HasInput(attr_name)) {
      // convert from data
      if (attr_defs[i].type_index == std::type_index(typeid(int32_t))) {
        if (ctx->IsRuntime()) {
          const auto& infershape_inputs = ctx->GetInputVarPtrs(attr_name);
          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"));
      }
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
    }
  }

  for (auto& out_name : output_names) {
    if (ctx->HasOutputs(out_name)) {
      auto output_var = ctx->GetOutputVarPtrs(out_name);
      if (output_var.size() == 1) {
        infer_meta_context.EmplaceBackOutput(std::make_shared<CompatMetaTensor>(
            output_var[0], ctx->IsRuntime()));
      } else {
        paddle::SmallVector<std::shared_ptr<phi::MetaTensor>> outputs;
        outputs.reserve(output_var.size());
        for (const auto& out : output_var) {
          outputs.emplace_back(
              std::make_shared<CompatMetaTensor>(out, ctx->IsRuntime()));
        }
        infer_meta_context.EmplaceBackOutputs(std::move(outputs));
      }
    } else {
      infer_meta_context.EmplaceBackOutput({nullptr});
539
    }
540 541 542 543 544
  }

  return infer_meta_context;
}

C
Chen Weihang 已提交
545 546
}  // namespace framework
}  // namespace paddle