MCPcopy Create free account
hub / github.com/Oneflow-Inc/oneflow / Compute

Method Compute

oneflow/user/kernels/dim_scatter_kernels.cpp:30–73  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28
29 private:
30 void Compute(KernelComputeContext* ctx) const override {
31 const Tensor* input_tensor = ctx->Tensor4ArgNameAndIndex("input", 0);
32 const Tensor* index_tensor = ctx->Tensor4ArgNameAndIndex("index", 0);
33 Tensor* out_tensor = ctx->Tensor4ArgNameAndIndex("output", 0);
34 const Tensor* src_tensor = ctx->Tensor4ArgNameAndIndex("src", 0);
35 const int32_t dim = ctx->Attr<int32_t>("dim");
36
37 const IDX_T* index = index_tensor->dptr<IDX_T>();
38 IN_T* output = out_tensor->mut_dptr<IN_T>();
39 size_t out_bytes_size =
40 out_tensor->shape_view().elem_cnt() * GetSizeOfDataType(out_tensor->data_type());
41
42 Tensor* like_tensor = ctx->Tensor4ArgNameAndIndex("like", 0);
43 const IN_T* src = src_tensor->dptr<IN_T>();
44
45 if (input_tensor) {
46 Memcpy<device_type>(ctx->stream(), output, input_tensor->dptr<IN_T>(), out_bytes_size);
47 } else if (like_tensor) {
48 Memset<device_type>(ctx->stream(), output, 0, out_bytes_size);
49 } else {
50 UNIMPLEMENTED() << "Input tensor and like tensor cannot be empty simultaneously.";
51 }
52
53 const Shape src_shape = ExpandDimIf0D(src_tensor->shape_view());
54 const Shape index_shape = ExpandDimIf0D(index_tensor->shape_view());
55 const int ndim = src_shape.NumAxes();
56 DimOpIndexNdHelper<IDX_T> src_nd_helper(src_shape.data(), ndim);
57 DimOpIndexNdHelper<IDX_T> idx_nd_helper(index_shape.data(), ndim);
58 DimOpIndexNdHelper<IDX_T> output_nd_helper(out_tensor->shape_view().data(), ndim);
59
60 const int64_t upper_bound = [&]() {
61 if (input_tensor) {
62 const Shape input_shape = ExpandDimIf0D(input_tensor->shape_view());
63 return input_shape.At(dim);
64 } else {
65 const Shape like_shape = ExpandDimIf0D(like_tensor->shape_view());
66 return like_shape.At(dim);
67 }
68 }();
69
70 DimScatterFunctor<device_type, IN_T, IDX_T, Opt>()(
71 ctx->stream(), src_nd_helper, idx_nd_helper, output_nd_helper, ndim, index_shape.elem_cnt(),
72 dim, upper_bound, index, src, output);
73 }
74 bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; }
75};
76

Callers

nothing calls this directly

Calls 10

GetSizeOfDataTypeFunction · 0.85
ExpandDimIf0DFunction · 0.85
elem_cntMethod · 0.45
shape_viewMethod · 0.45
data_typeMethod · 0.45
streamMethod · 0.45
NumAxesMethod · 0.45
dataMethod · 0.45
AtMethod · 0.45

Tested by

no test coverage detected