layer.cc 3.0 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 18 19
#include "paddle/fluid/framework/variable.h"

#include "paddle/fluid/jit/compilation_unit.h"
20
#include "paddle/fluid/jit/function/base_function.h"
21
#include "paddle/fluid/jit/function_schema.h"
22 23
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/errors.h"
24

25 26
namespace paddle {
namespace jit {
27 28 29 30 31

Layer::Layer(const Name2VariableMap& params_dict,
             const Name2VariableMap& attrs_dict,
             const phi::Place& place)
    : params_dict_(params_dict), attrs_dict_(attrs_dict) {
32
  unit_.reset(new CompilationUnit());
33 34
}

35
std::shared_ptr<BaseFunction> Layer::Function(const std::string& name) const {
36
  return unit_->Function(name);
37 38
}

39 40 41 42 43 44 45
std::vector<Tensor> Layer::forward(const std::vector<Tensor>& inputs) {
  auto func = Function("forward");
  return (*func)(inputs);
}

std::vector<DenseTensor> Layer::forward(
    const std::vector<DenseTensor>& inputs) {
46
  auto func = Function("forward");
47 48 49
  return (*func)(inputs);
}

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

void Layer::SetFunction(const std::string& name,
                        const std::shared_ptr<BaseFunction>& function) {
54
  unit_->SetFunction(name, function);
55 56
}

57
std::vector<std::string> Layer::FunctionNames() const {
58
  return unit_->FunctionNames();
59 60 61
}

const Name2FunctionMap& Layer::FunctionMap() const {
62
  return unit_->FunctionMap();
63 64
}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#define PD_SPECIALZE_ATTRIBUTE_TYPE(T)                                \
  template <>                                                         \
  T Layer::Attribute<T>(const std::string& name) const {              \
    if (attrs_dict_.find(name) == attrs_dict_.end()) {                \
      PADDLE_THROW(phi::errors::NotFound(                             \
          "Attribute can not found %s, please check if it exists.")); \
      return T();                                                     \
    }                                                                 \
    auto var = attrs_dict_.at(name);                                  \
    T ret = var->Get<T>();                                            \
    return ret;                                                       \
  }

PD_SPECIALZE_ATTRIBUTE_TYPE(int)
PD_SPECIALZE_ATTRIBUTE_TYPE(float)
PD_SPECIALZE_ATTRIBUTE_TYPE(std::string)
PD_SPECIALZE_ATTRIBUTE_TYPE(std::vector<int>)
PD_SPECIALZE_ATTRIBUTE_TYPE(std::vector<float>)
PD_SPECIALZE_ATTRIBUTE_TYPE(std::vector<std::string>)

85 86
}  // namespace jit
}  // namespace paddle