cinn_launch_context.h 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright (c) 2021 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.

#pragma once

#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
22
#include <vector>
23
#include "paddle/fluid/framework/lod_tensor.h"
24
#include "paddle/fluid/framework/scope.h"
25 26 27 28 29 30 31 32 33 34
#include "paddle/fluid/platform/place.h"

// type declaration forward
struct cinn_buffer_t;
struct cinn_pod_value_t;
namespace cinn::hlir::framework {
class Tensor;
class Scope;
class Program;
}  // namespace cinn::hlir::framework
35 36

namespace paddle {
37
namespace operators::details {
38 39 40 41

using CinnTensor = ::cinn::hlir::framework::Tensor;
using CinnScope = ::cinn::hlir::framework::Scope;

42 43 44 45 46 47 48
// This class is used to cache some reusable data among repeated
// executions for efficiency and it also provides easy interfaces
// to get details of the compilation result.
// A object of this class is constructed and saved in the
// compilation cache once a graph compiled by CINN.
// Generally speaking, here, a variable is refer to a Paddle
// Variable while a CINN variable is called an Argument.
49 50 51 52 53 54
class CinnLaunchContext {
 public:
  explicit CinnLaunchContext(
      const std::unordered_map<std::string, std::string>& paddle2cinn_varmap,
      const std::shared_ptr<CinnScope>& cinn_scope);

55 56 57 58 59 60 61 62
  // explicitly update several environment variables captured
  // by callback of execution arguments
  void UpdateCapturedEnv(const framework::Scope& scope,
                         const platform::Place& place);

  // Return whether execution arguments has been initialized
  bool IsArgumentsInitialized() const;

63 64
  // Return whether a Paddle variable used in cinn execution
  bool IsVariableUsed(const std::string& var_name) const;
65 66

  // Assign tensor buffer to input or output variables
67
  void AssignExternalVariable(const std::string& var_name);
68 69

  // Assign tensor buffer to internal variables
70
  void AssignInternalVariable(const std::string& var_name);
71

72 73 74 75 76
  // Extract internal variable names from all applied variables
  // in execution by excluding the input and output variables
  std::unordered_set<std::string> ExtractInternalVarNames(
      const std::vector<std::string>& input_var_names,
      const std::vector<std::string>& output_var_names);
77

78
  // Finalize all execution arguments and return the name->argument map
79 80
  const std::map<std::string, cinn_pod_value_t>& FinalizeArguments() const;

81 82
  // Return the cinn_buffer_t* of a specific variable
  cinn_buffer_t* GetCinnBufferOfVar(const std::string& var_name);
83

84
 private:
85 86 87 88 89 90 91 92 93 94
  // Get CinnTensor with CINN argument name
  CinnTensor GetCinnTensor(const std::string& arg_name);
  // Build the name maps of paddle->cinn and cinn->paddle
  // in reverse for all variables used in cinn execution
  void BuildVarNameMap(
      const std::unordered_map<std::string, std::string>& compiled_varmap,
      const std::unordered_set<std::string>& argument_names);

  // Check whether the tensor in Paddle and the compiled
  // tensor returned by CINN of a same variable
95 96
  // are equivalent in type and dimension
  void CheckTensorEquivalent(const std::string& var_name,
97
                             const framework::LoDTensor& paddle_tensor,
98 99
                             const CinnTensor& cinn_tensor);

100 101 102
  // Append an argument with (cinn name)->(cinn_buffer_t) pair
  void AppendArgument(const std::string& arg_name,
                      std::unique_ptr<cinn_buffer_t>&& buffer);
103 104

 private:
105 106 107 108
  const framework::Scope* cached_scope_ = nullptr;
  const platform::Place* cached_place_ = nullptr;
  std::unique_ptr<framework::Scope> cached_temp_scope_ = nullptr;

109
  // a name map from paddle variables to cinn execution arguments
110
  std::unordered_map<std::string, std::string> paddle2cinn_varmap_;
111
  // a name map from cinn execution arguments to paddle variables
112
  std::unordered_map<std::string, std::string> cinn2paddle_varmap_;
113 114 115
  // the names of the cinn arguments used in compiled executable program
  std::unordered_set<std::string> cinn_argument_names_;
  // the variable scope compiled from cinn
116 117
  const std::shared_ptr<CinnScope> cinn_scope_;

118 119
  // because a cinn_pod_value_t does not own a cinn_buffer_t object,
  // an extra stroage is necessary to keep those objects and they can
120
  // not be released until the runtime program finish execution.
121 122
  std::vector<std::unique_ptr<cinn_buffer_t>> hold_buffers_;

123 124
  // this map saves all execution arguments with their cinn names as key,
  // and it is passed to the Execute interface of a cinn runtime program.
125 126 127
  std::map<std::string, cinn_pod_value_t> name2argument_;
};

128
}  // namespace operators::details
129
}  // namespace paddle