transpose_kernel.cpp 2.7 KB
Newer Older
S
Shenghang Tsai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
Copyright 2020 The OneFlow 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.
*/
16
#include "oneflow/core/framework/framework.h"
J
Juncheng 已提交
17
#include "oneflow/core/kernel/cuda_graph_support.h"
Z
ZZK 已提交
18
#include "oneflow/core/primitive/include/permute.h"
19 20 21 22
namespace oneflow {

namespace user_op {

Z
ZZK 已提交
23 24 25 26 27 28
template<typename Context>
std::unique_ptr<primitive::Permute> NewPermutePrimitive(Context* ctx) {
  const int64_t num_dims = ctx->TensorDesc4ArgNameAndIndex("output", 0)->shape().NumAxes();
  return primitive::NewPrimitive<primitive::PermuteFactory>(ctx->device_type(), num_dims);
}

J
Juncheng 已提交
29
class TransposeKernel final : public OpKernel, public user_op::CudaGraphSupport {
30
 public:
Z
ZZK 已提交
31
  OF_DISALLOW_COPY_AND_MOVE(TransposeKernel);
32
  TransposeKernel() = default;
33
  ~TransposeKernel() override = default;
34 35 36

 private:
  void Compute(KernelComputeContext* ctx) const override {
Z
ZZK 已提交
37 38 39
    auto primitive = NewPermutePrimitive(ctx);
    CHECK(primitive);

40 41
    const Tensor* tensor_in = ctx->Tensor4ArgNameAndIndex("input", 0);
    Tensor* tensor_out = ctx->Tensor4ArgNameAndIndex("output", 0);
O
OuYang Yu 已提交
42
    const auto& perm = ctx->Attr<std::vector<int32_t>>("perm");
43
    const ShapeView& in_shape = tensor_in->shape();
Z
ZZK 已提交
44 45 46 47 48 49 50 51 52 53 54 55
    DataType dtype = tensor_out->data_type();
    size_t num_dims = tensor_in->shape().NumAxes();
    const int64_t* src_dims = in_shape.ptr();

    int64_t elem_cnt = tensor_out->shape().elem_cnt();
    if (elem_cnt != 0) {
      primitive->Launch(ctx->stream_ctx(), dtype, num_dims, src_dims, tensor_in->dptr(),
                        perm.data(), tensor_out->mut_dptr());
    } else {
      // For 0-d Tensor
      return;
    }
56 57 58 59
  }
  bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; }
};

Z
ZZK 已提交
60 61 62 63 64 65
hob::HobContextGetter<user_op::KernelRegContext, bool> PermutePrimitiveExists() {
  return user_op::HobCtxGetter<bool>("PermutePrimitiveExists",
                                     [](const user_op::KernelRegContext& ctx) {
                                       return NewPermutePrimitive(&ctx).operator bool();
                                     });
}
66

Z
ZZK 已提交
67 68 69
REGISTER_USER_KERNEL("transpose")
    .SetCreateFn<TransposeKernel>()
    .SetIsMatchedHob(PermutePrimitiveExists() == true);
S
Shenghang Tsai 已提交
70

71 72
}  // namespace user_op
}  // namespace oneflow