kernel_context.h 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//   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

17
#include <iterator>
18 19
#include <utility>

20 21 22 23
#include "paddle/phi/core/device_context.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/tensor_base.h"
#include "paddle/phi/core/tensor_utils.h"
24 25 26
#include "paddle/utils/any.h"
#include "paddle/utils/small_vector.h"

27
namespace phi {
28 29

/**
30
 * Note: KernelContext doesn't manage the life of DeviceContext and Tensor
31 32 33 34 35 36 37
 *
 * Note: KernelContext does not couple the concept of framework,
 *       its constructor can only take the members it needs as parameters,
 *       not Scope, RuntimeContext, etc. as parameters
 */
class KernelContext {
 public:
38 39 40 41
  KernelContext() = default;
  explicit KernelContext(DeviceContext* dev_ctx) : dev_ctx_(dev_ctx) {}

  void SetDeviceContext(DeviceContext* dev_ctx) { dev_ctx_ = dev_ctx; }
42 43 44

  template <typename CtxType>
  const CtxType& GetDeviceContext() const {
45
    return static_cast<const CtxType&>(*dev_ctx_);
46 47
  }

48
  void EmplaceBackInput(const TensorBase* input);
49

50
  void EmplaceBackInputWithoutSetRange(const TensorBase* input);
51

52
  void EmplaceBackInputs(paddle::SmallVector<const TensorBase*> inputs);
53

54
  void EmplaceBackOutput(TensorBase* output);
55

56
  void EmplaceBackOutputWithoutSetRange(TensorBase* output);
57

58
  void EmplaceBackOutputs(paddle::SmallVector<TensorBase*> outputs);
59

60 61 62 63 64 65
  void EmplaceBackAttr(paddle::any attr);

  const std::pair<int, int>& InputRangeAt(size_t idx) const;

  const std::pair<int, int>& OutputRangeAt(size_t idx) const;

66
  void AssignInputRange(std::pair<int, int>&& range, size_t idx);
67

68
  void AssignOutputRange(std::pair<int, int>&& range, size_t idx);
69 70 71 72 73 74

  template <typename TensorType>
  const TensorType& InputAt(size_t idx) const {
    return static_cast<const TensorType&>(*(inputs_.at(idx)));
  }

75 76 77 78 79 80 81 82
  template <typename TensorType>
  paddle::optional<const TensorType&> OptionalInputAt(size_t idx) const {
    const auto& input = inputs_.at(idx);
    return input ? paddle::optional<const TensorType&>{static_cast<
                       const TensorType&>(*input)}
                 : paddle::optional<const TensorType&>{paddle::none};
  }

83
  template <typename TensorType>
84
  std::vector<TensorType> MoveInputsBetween(size_t start, size_t end) {
85 86
    std::vector<TensorType> v;
    for (size_t i = start; i < end; ++i) {
87
      auto t = static_cast<const TensorType*>(inputs_.at(i));
88
      v.emplace_back(*t);
89
      inputs_[i] = nullptr;
90 91 92 93
    }
    return v;
  }

94 95
  template <typename TensorType>
  TensorType* MutableOutputAt(size_t idx) {
96
    return static_cast<TensorType*>(outputs_.at(idx));
97 98
  }

99 100 101 102
  template <typename TensorType>
  std::vector<TensorType*> MutableOutputBetween(size_t start, size_t end) {
    std::vector<TensorType*> v;
    for (size_t i = start; i < end; ++i) {
103
      v.emplace_back(static_cast<TensorType*>(outputs_.at(i)));
104 105 106 107
    }
    return v;
  }

108 109 110 111 112
  template <typename AttrType>
  AttrType AttrAt(size_t idx) const {
    try {
      return paddle::any_cast<AttrType>(attrs_.at(idx));
    } catch (paddle::bad_any_cast&) {
113
      PADDLE_THROW(phi::errors::InvalidArgument(
114 115 116 117
          "Attribute cast error in Op Kernel Context."));
    }
  }

118 119 120 121
  size_t InputsSize() const { return inputs_.size(); }
  size_t OutputsSize() const { return outputs_.size(); }
  size_t AttrsSize() const { return attrs_.size(); }

122
 private:
123
  DeviceContext* dev_ctx_;
124

125 126
  paddle::SmallVector<const TensorBase*> inputs_;
  paddle::SmallVector<TensorBase*> outputs_;
127 128 129 130 131 132
  paddle::SmallVector<paddle::any> attrs_;

  paddle::SmallVector<std::pair<int, int>> input_range_;
  paddle::SmallVector<std::pair<int, int>> output_range_;
};

133
}  // namespace phi