layer.cc 3.4 KB
Newer Older
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/jit/layer.h"

17
#include "paddle/fluid/framework/variable.h"
18 19
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/errors.h"
20 21

#include "paddle/fluid/jit/compilation_unit.h"
22 23
#include "paddle/fluid/jit/engine/base_engine.h"
#include "paddle/fluid/jit/function.h"
24 25
#include "paddle/fluid/jit/function_schema.h"

26 27
namespace paddle {
namespace jit {
28

29 30 31
Layer::Layer(const VariableMap& params_map,
             const VariableMap& attrs_map,
             const FunctionInfoMap& info_map,
32
             const phi::Place& place)
33
    : params_map_(params_map), attrs_map_(attrs_map), info_map_(info_map) {
34
  unit_.reset(new CompilationUnit());
35 36
}

37 38
jit::Function Layer::Function(const std::string& name) const {
  return jit::Function(unit_->GetEngine(name).get());
39 40
}

41
std::vector<Tensor> Layer::forward(const std::vector<Tensor>& inputs) {
42 43
  auto func = this->Function("forward");
  return func(inputs);
44 45 46 47
}

std::vector<DenseTensor> Layer::forward(
    const std::vector<DenseTensor>& inputs) {
48 49
  auto func = this->Function("forward");
  return func(inputs);
50 51
}

52 53
void Layer::to(const phi::Place& place) {}

54 55 56
void Layer::SetEngine(const std::string& name,
                      const std::shared_ptr<BaseEngine>& engine) {
  unit_->SetEngine(name, engine);
57 58
}

59 60 61 62 63 64
const std::shared_ptr<jit::FunctionInfo>& Layer::FunctionInfo(
    const std::string& name) const {
  PADDLE_ENFORCE_EQ(
      info_map_.count(name),
      1,
      phi::errors::InvalidArgument(
65
          "FuncitonInfo named %s is not existed in info_map_.", name));
66
  return info_map_.at(name);
67 68
}

69 70 71 72 73 74 75 76
std::vector<std::string> Layer::FunctionNames() const {
  std::vector<std::string> names;
  for (auto it = info_map_.begin(); it != info_map_.end(); ++it) {
    names.emplace_back(it->first);
  }
  return names;
}

77 78 79
#define PD_SPECIALZE_ATTRIBUTE_TYPE(T)                                \
  template <>                                                         \
  T Layer::Attribute<T>(const std::string& name) const {              \
80
    if (attrs_map_.find(name) == attrs_map_.end()) {                  \
81 82 83 84
      PADDLE_THROW(phi::errors::NotFound(                             \
          "Attribute can not found %s, please check if it exists.")); \
      return T();                                                     \
    }                                                                 \
85
    auto var = attrs_map_.at(name);                                   \
86 87 88 89 90 91
    T ret = var->Get<T>();                                            \
    return ret;                                                       \
  }

PD_SPECIALZE_ATTRIBUTE_TYPE(int)
PD_SPECIALZE_ATTRIBUTE_TYPE(float)
92
PD_SPECIALZE_ATTRIBUTE_TYPE(framework::String)
93 94 95 96
PD_SPECIALZE_ATTRIBUTE_TYPE(std::vector<int>)
PD_SPECIALZE_ATTRIBUTE_TYPE(std::vector<float>)
PD_SPECIALZE_ATTRIBUTE_TYPE(std::vector<std::string>)

97 98
}  // namespace jit
}  // namespace paddle