sparse_manual_op.cc 9.7 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
/* 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. */

#include <memory>
#include <string>

#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/binary.h"
23
#include "paddle/phi/infermeta/multiary.h"
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
#include "paddle/phi/infermeta/sparse/binary.h"
#include "paddle/phi/infermeta/sparse/unary.h"
#include "paddle/phi/infermeta/unary.h"

namespace paddle {
namespace operators {

class SparseSparseCooTensorOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("values", "(Tensor), input 0 of sparse_coo_tensor op.");
    AddInput("indices", "(Tensor), input 1 of sparse_coo_tensor op.");
    AddOutput("out", "(Tensor), output 0 of sparse_coo_tensor op.");
    AddAttr<std::vector<int>>(
        "dense_shape", "(vector<int>), attribute 0 for sparse_coo_tensor op.");
    AddComment(R"DOC(
TODO: Documentation of sparse_coo_tensor op.
)DOC");
  }
};

class SparseSparseCooTensorOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

DECLARE_INFER_SHAPE_FUNCTOR(
    sparse_sparse_coo_tensor,
    SparseSparseCooTensorInferShapeFunctor,
    PD_INFER_META(phi::sparse::SparseCooTensorInferMeta));

class SparseValuesOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("x", "(Tensor), input 0 of sparse_values op.");
    AddOutput("out", "(Tensor), output 0 of sparse_values op.");
    AddComment(R"DOC(
TODO: Documentation of sparse_values op.
)DOC");
  }
};

class SparseValuesOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

DECLARE_INFER_SHAPE_FUNCTOR(sparse_values,
                            SparseValuesInferShapeFunctor,
                            PD_INFER_META(phi::sparse::ValuesInferMeta));

class SparseIndicesOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("x", "(Tensor), input 0 of sparse_indices op.");
    AddOutput("out", "(Tensor), output 0 of sparse_indices op.");
    AddComment(R"DOC(
TODO: Documentation of sparse_indices op.
)DOC");
  }
};

class SparseIndicesOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

DECLARE_INFER_SHAPE_FUNCTOR(sparse_indices,
                            SparseIndicesInferShapeFunctor,
                            PD_INFER_META(phi::sparse::IndicesInferMeta));

class SparseToDenseOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("x", "(Tensor), input 0 of sparse_to_dense op.");
    AddOutput("out", "(Tensor), output 0 of sparse_to_dense op.");
    AddComment(R"DOC(
TODO: Documentation of sparse_to_dense op.
)DOC");
  }
};

class SparseToDenseOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

DECLARE_INFER_SHAPE_FUNCTOR(sparse_to_dense,
                            SparseToDenseInferShapeFunctor,
                            PD_INFER_META(phi::UnchangedInferMeta));

class SparseReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("x", "(Tensor), input 0 of sparse_relu op.");
    AddOutput("out", "(Tensor), output 0 of sparse_relu op.");
    AddComment(R"DOC(
TODO: Documentation of sparse_relu op.
)DOC");
  }
};

class SparseReluOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

DECLARE_INFER_SHAPE_FUNCTOR(sparse_relu,
                            SparseReluInferShapeFunctor,
                            PD_INFER_META(phi::UnchangedInferMeta));

class SparseConv3dOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("x", "(Tensor), input 0 of sparse_conv3d op.");
    AddInput("kernel", "(Tensor), input 1 of sparse_conv3d op.");
    AddOutput("out", "(Tensor), output 0 of sparse_conv3d op.");
    AddOutput("rulebook", "(Tensor), output 1 of sparse_conv3d op.");
    AddOutput("counter", "(Tensor), output 2 of sparse_conv3d op.");
    AddAttr<std::vector<int>>(
        "paddings", "(vector<int>), attribute 0 for sparse_conv3d op.");
    AddAttr<std::vector<int>>(
        "dilations", "(vector<int>), attribute 1 for sparse_conv3d op.");
    AddAttr<std::vector<int>>(
        "strides", "(vector<int>), attribute 2 for sparse_conv3d op.");
    AddAttr<int>("groups", "(int), attribute 3 for sparse_conv3d op.");
    AddAttr<bool>("subm", "(bool), attribute 4 for conv3d_coo op.");
    AddAttr<std::string>("key", "(string), attribute 5 for sparse_conv3d op.")
        .SetDefault("");
    AddComment(R"DOC(
TODO: Documentation of sparse_conv3d op.
)DOC");
  }
};

class SparseConv3dOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

DECLARE_INFER_SHAPE_FUNCTOR(sparse_conv3d,
                            SparseConv3dInferShapeFunctor,
                            PD_INFER_META(phi::sparse::Conv3dInferMeta));

class SparseAddOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("x", "(Tensor), input 0 of sparse_add op.");
    AddInput("y", "(Tensor), input 1 of sparse_add op.");
    AddOutput("out", "(Tensor), output 0 of sparse_add op.");
    AddComment(R"DOC(
TODO: Documentation of sparse_add op.
)DOC");
  }
};

class SparseAddOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

DECLARE_INFER_SHAPE_FUNCTOR(sparse_add,
                            SparseAddInferShapeFunctor,
                            PD_INFER_META(phi::UnchangedInferMeta));

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
class SparseBatchNormOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("x", "(Tensor), input 0 of sparse_batch_norm op.");
    AddInput("scale", "(Tensor), input 1 of sparse_batch_norm op.");
    AddInput("bias", "(Tensor), input 2 of sparse_batch_norm op.");
    AddInput("mean", "(Tensor), input 3 of sparse_batch_norm op.");
    AddInput("variance", "(Tensor), input 4 of sparse_batch_norm op.");
    AddOutput("y", "(Tensor), output 0 of sparse_batch_norm op.");
    AddOutput("mean_out", "(Tensor), output 1 of sparse_batch_norm op.");
    AddOutput("variance_out", "(Tensor), output 2 of sparse_batch_norm op.");
    AddOutput("saved_mean", "(Tensor), output 3 of sparse_batch_norm op.");
    AddOutput("saved_variance", "(Tensor), output 4 of sparse_batch_norm op.");
    AddOutput("reserve_space", "(Tensor), output 5 of sparse_batch_norm op.");
    AddAttr<float>("momentum",
                   "(float), attribute 0 for sparse_batch_norm op.");
    AddAttr<float>("epsilon", "(float), attribute 1 for sparse_batch_norm op.");
    AddAttr<std::string>("data_layout",
                         "(string), attribute 2 for sparse_batch_norm op.");
    AddAttr<bool>("is_test", "(bool), attribute 3 for sparse_batch_norm op.");
    AddAttr<bool>("use_global_stats",
                  "(bool), attribute 4 for sparse_batch_norm op.");
    AddAttr<bool>("trainable_statistics",
                  "(bool), attribute 4 for sparse_batch_norm op.");
    AddAttr<bool>("fuse_with_relu",
                  "(bool), attribute 4 for sparse_batch_norm op.");
    AddComment(R"DOC(
Z
zhangkaihuo 已提交
216
TODO: Documentation of sparse_batch_norm op.
217 218 219 220 221 222 223 224 225 226 227 228 229
)DOC");
  }
};

class SparseBatchNormOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

DECLARE_INFER_SHAPE_FUNCTOR(sparse_batch_norm,
                            SparseBatchNormInferShapeFunctor,
                            PD_INFER_META(phi::BatchNormInferMeta));

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

REGISTER_OPERATOR(sparse_sparse_coo_tensor,
                  ops::SparseSparseCooTensorOp,
                  ops::SparseSparseCooTensorOpMaker,
                  ops::SparseSparseCooTensorInferShapeFunctor);

REGISTER_OPERATOR(sparse_values,
                  ops::SparseValuesOp,
                  ops::SparseValuesOpMaker,
                  ops::SparseValuesInferShapeFunctor);

REGISTER_OPERATOR(sparse_indices,
                  ops::SparseIndicesOp,
                  ops::SparseIndicesOpMaker,
                  ops::SparseIndicesInferShapeFunctor);

REGISTER_OPERATOR(sparse_to_dense,
                  ops::SparseToDenseOp,
                  ops::SparseToDenseOpMaker,
                  ops::SparseToDenseInferShapeFunctor);

REGISTER_OPERATOR(sparse_relu,
                  ops::SparseReluOp,
                  ops::SparseReluOpMaker,
                  ops::SparseReluInferShapeFunctor);

REGISTER_OPERATOR(sparse_conv3d,
                  ops::SparseConv3dOp,
                  ops::SparseConv3dOpMaker,
                  ops::SparseConv3dInferShapeFunctor);

REGISTER_OPERATOR(sparse_add,
                  ops::SparseAddOp,
                  ops::SparseAddOpMaker,
                  ops::SparseAddInferShapeFunctor);
269 270 271 272 273

REGISTER_OPERATOR(sparse_batch_norm,
                  ops::SparseBatchNormOp,
                  ops::SparseBatchNormOpMaker,
                  ops::SparseBatchNormInferShapeFunctor);