infershaped_kernel_launcher.h 1.9 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) 2022 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 <llvm/ADT/SmallVector.h>

#include "paddle/infrt/host_context/kernel_frame.h"
#include "paddle/infrt/host_context/value.h"

namespace infrt {
22
namespace kernel {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

struct InferShapedKernelLauncher {
  virtual void Invoke(host_context::KernelFrame* frame) = 0;

  virtual ~InferShapedKernelLauncher() = default;

 protected:
  //! Initialize the kernel frame for InferShape kernel.
  // This method will create a new KernelFrame with all the Tensors(currently
  // only DenseHostTensor) converted into MetaTensors so that the infer-shape
  // function can work with.
  // @frame: the frame containing argument list that is same with the ones of
  // the corresponding kernel.
  void CreateKernelFrameForInferShape(host_context::KernelFrame* frame);

  //! Build or update the infer-shape cache using the latest shape from
  //! InferShapeFrame.
40
  void BuildInferShapeCache(const uint16_t num_inputs);
41 42

  //! Compare the latest shape with the shape cache.
43
  bool IsShapeChanged(const uint16_t num_inputs) const;
44 45 46

  // values to hold the TensorMeta.
  llvm::SmallVector<host_context::ValueRef, 3> values;
47
  llvm::SmallVector<::pten::DDim, 3> tensor_shape_cache;
48 49 50
  host_context::KernelFrameBuilder infershape_kernel_frame_builder;
};

51
}  // namespace kernel
52
}  // namespace infrt