arg_map_context.h 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 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 <ostream>
#include <string>
#include <tuple>

H
hong 已提交
21
#include "paddle/phi/common/place.h"
22
#include "paddle/phi/core/type_defs.h"
23
#include "paddle/utils/any.h"
24 25 26
#include "paddle/utils/flat_hash_map.h"
#include "paddle/utils/small_vector.h"

27
namespace phi {
28 29

// tuple(input_names, attr_names, output_names)
30 31 32
using KernelArgsTuple = std::tuple<paddle::SmallVector<const char*>,
                                   paddle::SmallVector<const char*>,
                                   paddle::SmallVector<const char*>>;
33 34

struct KernelSignature {
35
  const char* name;
36 37 38
  paddle::SmallVector<const char*> input_names;
  paddle::SmallVector<const char*> attr_names;
  paddle::SmallVector<const char*> output_names;
39 40

  KernelSignature() = default;
41

42 43 44 45
  KernelSignature(const char* kernel_name,
                  paddle::SmallVector<const char*>&& inputs,
                  paddle::SmallVector<const char*>&& attrs,
                  paddle::SmallVector<const char*>&& outputs)
46 47 48 49
      : name(kernel_name),
        input_names(std::move(inputs)),
        attr_names(std::move(attrs)),
        output_names(std::move(outputs)) {}
50 51 52 53
  KernelSignature(const char* kernel_name,
                  const paddle::SmallVector<const char*>& inputs,
                  const paddle::SmallVector<const char*>& attrs,
                  const paddle::SmallVector<const char*>& outputs)
54 55 56 57
      : name(kernel_name),
        input_names(inputs),
        attr_names(attrs),
        output_names(outputs) {}
58 59 60 61 62

  // TODO(chenweihang): add assign constructor to solve windows compile
  // problem, remove it later
  KernelSignature& operator=(const KernelSignature& other) {
    name = other.name;
63 64 65
    input_names = other.input_names;
    attr_names = other.attr_names;
    output_names = other.output_names;
66 67
    return *this;
  }
68 69 70 71
};

std::ostream& operator<<(std::ostream& os, KernelSignature signature);

72 73
// TODO(chenweihang): Add more methods if needed in future
class ArgumentMappingContext {
74
 public:
75
  virtual ~ArgumentMappingContext() = default;
76

77 78
  virtual bool HasInput(const std::string& name) const = 0;
  virtual bool HasOutput(const std::string& name) const = 0;
79
  virtual bool HasAttr(const std::string& name) const = 0;
80

81
  // now we can't use Attribute here, it will cause phi relay on
82 83
  // boost::variant and BlockDesc
  virtual paddle::any Attr(const std::string& name) const = 0;
84

85 86
  virtual size_t InputSize(const std::string& name) const = 0;
  virtual size_t OutputSize(const std::string& name) const = 0;
87

88 89
  virtual bool IsDenseTensorInput(const std::string& name) const = 0;
  virtual bool IsSelectedRowsInput(const std::string& name) const = 0;
90 91
  // For compatibility with LoDTensorArray
  virtual bool IsDenseTensorVectorInput(const std::string& name) const = 0;
92 93 94

  virtual bool IsDenseTensorOutput(const std::string& name) const = 0;
  virtual bool IsSelectedRowsOutput(const std::string& name) const = 0;
95 96 97 98

  // use this function to mark it comes from InferShapeArgumentMappingContext
  // and will be used in infershape
  virtual bool IsForInferShape() const = 0;
99 100 101 102 103 104 105

  // NOTE(paddle-dev): [ Why do we export this interface? ]
  // In old Fluid framework, some operators' Attribute can be a Tensor or
  // TensorList. In this case, the InferShape logic will be different
  // under CompileTime and RuntimeTime. So we export this interface to
  // handle it conveniently. See "gaussian_random_sig.cc" for details.
  virtual bool IsRuntime() const { return true; }
106 107
};

108
}  // namespace phi