marker_op.cu 2.2 KB
Newer Older
Y
Yuang Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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. */

#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/tensor_util.h"
C
chenjian 已提交
19
#include "paddle/fluid/platform/profiler/event_tracing.h"
Y
Yuang Liu 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

namespace paddle {
namespace operators {

template <typename T>
__global__ void SimpleMarkerKernel(T* in, T* out, int ndim) {
  int idx = threadIdx.x + blockIdx.x * blockDim.x;
  for (; idx < ndim; idx += blockDim.x * gridDim.x) {
    out[idx] = in[idx];
  }
}

template <typename T>
class MarkerOpCUDAKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto& dev_ctx = ctx.template device_context<platform::CUDADeviceContext>();

    auto marker_role = ctx.Attr<std::string>("marker_role");
    auto marker_pos = ctx.Attr<std::string>("marker_pos");
    VLOG(3) << "marker role: " << marker_role
            << " marker position: " << marker_pos;

    framework::Tensor A;
    framework::Tensor B;
    auto* in_temp = A.mutable_data<T>({32, 1}, ctx.GetPlace());
    auto* out_temp = B.mutable_data<T>({32, 1}, ctx.GetPlace());
    platform::RecordEvent record_event(
C
chenjian 已提交
48 49 50
        "MarkerCUDA", "marker_" + marker_role + "_" + marker_pos,
        platform::TracerEventType::OperatorInner, 1,
        platform::EventRole::kInnerOp);
Y
Yuang Liu 已提交
51 52 53 54 55 56 57 58 59 60 61 62
    SimpleMarkerKernel<T><<<1, 32, 0, dev_ctx.stream()>>>(in_temp, out_temp,
                                                          32);
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
namespace plat = paddle::platform;

REGISTER_OP_CUDA_KERNEL(marker, ops::MarkerOpCUDAKernel<float>);