computation.h 6.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2021 CINN 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.

15
#pragma once
16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>

#include "paddle/cinn/frontend/net_builder.h"
#include "paddle/cinn/frontend/syntax.h"
#include "paddle/cinn/hlir/framework/graph_compiler.h"
#include "paddle/cinn/hlir/framework/tensor.h"

namespace cinn {
namespace frontend {

struct ComputationContext;

class CinnComputation {
 public:
30 31 32 33
  struct CompileOptions
      : public hlir::framework::GraphCompiler::CompileOptions {
    bool use_decomposer = false;
    bool do_prerun = true;
34 35 36 37 38 39 40
    bool use_default_passes = true;
    std::vector<std::string> passes;
  };

  inline static CompileOptions DefaultCompileOptions() {
    CompileOptions options;
    options.with_instantiate_variables = true;
41 42 43 44
    options.use_decomposer = false;
    options.passes = {};
    options.do_prerun = true;
    options.use_default_passes = true;
45 46 47 48
    return options;
  }

  /**
49 50
   * build program from NetBuilder, then compile it. NetBuilder is normally
   * NetBuilder or CINNBuilder.
51 52 53
   * @param target the target to run the program
   * @param builder program builder (NetBuilder or CINNBuilder)
   * @param options CompileOptions, config the compilation steps
54 55 56 57
   * @param outputs program output variables, if outputs is empty, then the
   * output variable of the last instruction of the program is used
   * @param stream CUDA stream, the value is meaningful only when target is
   * NVGPU
58 59
   * @return shared_ptr pointing to CinnComputation instance
   */
60 61 62 63 64 65
  static std::shared_ptr<CinnComputation> BuildAndCompile(
      const Target &target,
      NetBuilder &builder,
      const CompileOptions &options = DefaultCompileOptions(),
      const std::vector<Variable> &outputs = {},
      void *stream = nullptr);
66 67 68
  /**
   * compile the program
   * @param target the target to run the program
69 70
   * @param program program (usually generated by a Builder, or converted from
   * Paddle model)
71
   * @param options CompileOptions, config the compilation steps
72 73 74 75
   * @param outputs program output variables, if outputs is empty, then the
   * output variable of the last instruction of the program is used
   * @param stream CUDA stream, the value is meaningful only when target is
   * NVGpu
76 77
   * @return shared_ptr pointing to CinnComputation instance
   */
78 79 80 81 82 83
  static std::shared_ptr<CinnComputation> Compile(
      const Target &target,
      Program &program,
      const CompileOptions &options = DefaultCompileOptions(),
      const std::vector<Variable> &outputs = {},
      void *stream = nullptr);
84 85 86 87 88 89 90 91
  /**
   * convert a paddle model to program, then compile it.
   * @param target the target to run the program
   * @param model_path the path of the paddle model
   * @param input_names input variable names of paddle model
   * @param input_shapes input variable shapes of paddle model
   * @param params_combined whether params are stored combined
   * @param options CompileOptions, config the compilation steps
92 93
   * @param stream CUDA stream, the value is meaningful only when target is
   * NVGpu
94 95
   * @return shared_ptr pointing to CinnComputation instance
   */
96 97 98 99 100 101 102 103
  static std::shared_ptr<CinnComputation> CompilePaddleModel(
      const Target &target,
      const std::string &model_path,
      const std::vector<std::string> &input_names,
      const std::vector<hlir::framework::shape_t> &input_shapes,
      bool params_combined,
      const CompileOptions &options = DefaultCompileOptions(),
      void *stream = nullptr);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

  /**
   * get all variable names in the program
   */
  std::vector<std::string> GetAllTensorNames();

  /**
   * get tensor by name
   * @param name tensor name
   */
  hlir::framework::Tensor GetTensor(const std::string &name);

  /**
   * get input tensors
   */
  std::vector<hlir::framework::Tensor> GetInputTensors();

  /**
   * get output tensors
   */
  std::vector<hlir::framework::Tensor> GetOutputTensors();

  /**
   * set the data of a tensor from user specified buffer.
   * if tensor is in NVGPU device memory, cudaMemcpy is used.
   * @param t the tensor
   * @param data address of the memory buffer to store tensor's data
   * @param size size of the memory buffer
   */
  void SetTensorData(hlir::framework::Tensor &t, void *data, size_t size);

  /**
136 137
   * set the data of a tensor (specified by it's name) from user specified
   * buffer. if tensor is in NVGPU device memory, cudaMemcpy is used.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
   * @param tname name of the tensor
   * @param data address of the memory buffer to store tensor's data
   * @param size size of the memory buffer
   */
  void SetTensorData(const std::string &tname, void *data, size_t size);

  /**
   * copy the data of a tensor to user specified buffer.
   * if tensor is in NVGPU device memory, cudaMemcpy is used.
   * @param t the tensor
   * @param data address of the memory buffer to store tensor's data
   * @param size size of the memory buffer
   */
  void GetTensorData(hlir::framework::Tensor &t, void *data, size_t size);
  /**
153 154
   * copy the data of a tensor (specified by it's name) to user specified
   * buffer. if tensor is in NVGPU device memory, cudaMemcpy is used.
155 156 157 158 159 160 161 162 163
   * @param tname name of the tensor
   * @param data address of the memory buffer to store tensor's data
   * @param size size of the memory buffer
   */
  void GetTensorData(const std::string &tname, void *data, size_t size);

  /**
   * run the compiled program
   */
164 165
  void Execute(
      const std::map<std::string, cinn_pod_value_t> *name2podargs = nullptr);
166 167 168 169 170 171 172

 private:
  std::shared_ptr<ComputationContext> context_;
};

}  // namespace frontend
}  // namespace cinn