cinn_launch_context.h 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// 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>
#include "cinn/hlir/framework/scope.h"
#include "cinn/hlir/framework/tensor.h"
#include "cinn/runtime/cinn_runtime.h"
#include "paddle/fluid/framework/ddim.h"
#include "paddle/fluid/framework/lod_tensor.h"
27
#include "paddle/fluid/framework/scope.h"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

namespace paddle {
namespace operators {
namespace details {

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

class CinnLaunchContext {
 public:
  explicit CinnLaunchContext(
      const std::unordered_map<std::string, std::string>& paddle2cinn_varmap,
      const std::shared_ptr<CinnScope>& cinn_scope);

43 44 45 46 47 48 49 50
  // 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;

51
  // Return whether a Paddle variable used on compiled kernels
52
  bool IsVariableUsed(const std::string& paddle_var_name) const;
53 54

  // Assign tensor buffer to input or output variables
55
  void AssignExternalVariable(const std::string& paddle_var_name);
56 57

  // Assign tensor buffer to internal variables
58
  void AssignInternalVariable(const std::string& cinn_var_name);
59 60 61 62 63 64 65 66

  // Extract internal variable names from CinnScope
  // by excluding used input and output variables
  std::unordered_set<std::string> GetInternalVariableNames();

  // Finalize all execution arguments and return them
  const std::map<std::string, cinn_pod_value_t>& FinalizeArguments() const;

67 68
  cinn_buffer_t* GetCinnBufferOfVar(const std::string& paddle_var_name);

69 70 71 72 73 74 75 76 77 78
 private:
  // Get CinnTensor with CINN variable name
  CinnTensor GetCinnTensor(const std::string& var_name);

  // Check whether tensors from Paddle and CINN of the same variable
  // are equivalent in type and dimension
  void CheckTensorEquivalent(const std::string& var_name,
                             const LoDTensor& paddle_tensor,
                             const CinnTensor& cinn_tensor);

79
  // Set an argument with (cinn name)->(cinn_buffer_t) pair
80
  void SetArgument(const std::string& cinn_var_name,
81
                   std::unique_ptr<cinn_buffer_t>&& buffer);
82 83

 private:
84 85 86 87
  const framework::Scope* cached_scope_ = nullptr;
  const platform::Place* cached_place_ = nullptr;
  std::unique_ptr<framework::Scope> cached_temp_scope_ = nullptr;

88
  // a variable name map from paddle to cinn
89 90 91
  std::unordered_map<std::string, std::string> paddle2cinn_varmap_;
  // a variable name map from cinn to paddle
  std::unordered_map<std::string, std::string> cinn2paddle_varmap_;
92 93 94
  // the variable scope of cinn
  const std::shared_ptr<CinnScope> cinn_scope_;

95
  // all names of cinn variables used by compiled executable program
96 97 98 99
  std::unordered_set<std::string> cinn_variable_names_;

  // because a cinn_pod_value_t does not own the cinn_buffer_t object,
  // an extra stroage is necessary to keep the object and it can
100
  // not be released until the runtime program finish execution.
101 102
  std::vector<std::unique_ptr<cinn_buffer_t>> hold_buffers_;

103 104
  // 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.
105 106 107 108 109 110
  std::map<std::string, cinn_pod_value_t> name2argument_;
};

}  // namespace details
}  // namespace operators
}  // namespace paddle