light_api.h 2.0 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright (c) 2019 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.

/*
 * This file implements a light-weight API which can run on mobile. We limit the
 * dependencies and the runtime computation complexity.
 */
#pragma once

#include <memory>
#include <string>
#include <utility>
#include <vector>
C
Chunwei 已提交
25
#include "paddle/fluid/lite/core/compatible_tensor.h"
T
tensor-tang 已提交
26 27 28 29 30 31 32 33 34
#include "paddle/fluid/lite/core/context.h"
#include "paddle/fluid/lite/core/program.h"
#include "paddle/fluid/lite/core/types.h"
#include "paddle/fluid/lite/model_parser/model_parser.h"
#include "paddle/fluid/lite/model_parser/pb/op_desc.h"

namespace paddle {
namespace lite {

C
Chunwei 已提交
35 36 37 38
/*
 * The light weight predictor, mainly for mobile. It loads an optimized model,
 * and will not depend on the MIR or perform latter optimization.
 */
T
tensor-tang 已提交
39 40
class LightPredictor {
 public:
C
Chunwei 已提交
41
  explicit LightPredictor(const std::string& model_dir);
T
tensor-tang 已提交
42 43 44

  void Run() { program_->Run(); }

C
Chunwei 已提交
45 46
  // Get offset-th col of feed inputs.
  Tensor* GetInput(size_t offset);
T
tensor-tang 已提交
47

C
Chunwei 已提交
48 49
  // Get offset-th col of fetch outputs.
  const Tensor* GetOutput(size_t offset);
T
tensor-tang 已提交
50

C
Chunwei 已提交
51 52 53 54 55
  const lite::Tensor* GetTensor(const std::string& name) const {
    auto* var = program_->exec_scope()->FindVar(name);
    return &var->Get<lite::Tensor>();
  }

T
tensor-tang 已提交
56
 private:
C
Chunwei 已提交
57 58
  void Build(const std::string& model_dir);
  void BuildRuntimeProgram(const framework::proto::ProgramDesc& prog);
T
tensor-tang 已提交
59 60 61 62 63 64 65 66

 private:
  std::shared_ptr<Scope> scope_;
  std::unique_ptr<RuntimeProgram> program_;
};

}  // namespace lite
}  // namespace paddle