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

Method Compute

oneflow/user/kernels/top_k_kernel.cpp:85–97  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

83
84 private:
85 void Compute(user_op::KernelComputeContext* ctx) const override {
86 const user_op::Tensor* in = ctx->Tensor4ArgNameAndIndex("in", 0);
87 if (in->shape_view().elem_cnt() == 0) { return; }
88 user_op::Tensor* out = ctx->Tensor4ArgNameAndIndex("out", 0);
89 user_op::Tensor* tmp_buffer = ctx->Tensor4ArgNameAndIndex("tmp_buffer", 0);
90
91 const int64_t instance_size = in->shape_view().At(in->shape_view().NumAxes() - 1);
92 const int64_t instance_num = in->shape_view().elem_cnt() / instance_size;
93 const int64_t k = std::min(static_cast<int64_t>(ctx->Attr<int32_t>("k")), instance_size);
94 int64_t* indices_ptr = tmp_buffer ? tmp_buffer->mut_dptr<int64_t>() : nullptr;
95 CpuTopK(ctx->stream(), in->dptr<T>(), indices_ptr, instance_num, instance_size, k,
96 ctx->Attr<bool>("sorted"), out->mut_dptr<int64_t>());
97 }
98 bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; }
99};
100

Callers

nothing calls this directly

Calls 7

CpuTopKFunction · 0.85
elem_cntMethod · 0.45
shape_viewMethod · 0.45
AtMethod · 0.45
NumAxesMethod · 0.45
streamMethod · 0.45

Tested by

no test coverage detected