Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/infinicore/ops/topksoftmax/topksoftmax_infiniops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,51 @@
#ifdef ENABLE_INFINIOPS_API
#include "../infiniops_impl.hpp"

#include "base/topksoftmax_infinilm.h"
#include "base/topk_softmax.h"

#include <optional>

namespace infinicore::op::topksoftmax_impl::infiniops {
namespace {
using TensorMeta = ::infinicore::op::infiniops::TensorMeta;
struct PlannedMeta {
TensorMeta values, indices, x;
graph::GraphTensor values_tensor, indices_tensor, x_tensor;
size_t topk;
TensorMeta values, indices, token_expert_indices, x;
graph::GraphTensor values_tensor, indices_tensor, token_expert_indices_tensor, x_tensor;
int norm;
};
} // namespace

void *plan(Tensor values, Tensor indices, const Tensor &x, const size_t topk, const int norm) {
INFINICORE_ASSERT(::infinicore::op::infiniops::isSupportedDevice(values->device().getType()));
INFINICORE_ASSERT_TENSORS_SAME_DEVICE(values, indices, x);
return new PlannedMeta{TensorMeta(values), TensorMeta(indices), TensorMeta(x), graph::GraphTensor(values), graph::GraphTensor(indices), graph::GraphTensor(x), topk, norm};
auto token_expert_indices = Tensor::empty({x->size(0), topk}, DataType::I32, indices->device());
return new PlannedMeta{
TensorMeta(values),
TensorMeta(indices),
TensorMeta(token_expert_indices),
TensorMeta(x),
graph::GraphTensor(values),
graph::GraphTensor(indices),
graph::GraphTensor(token_expert_indices),
graph::GraphTensor(x),
norm};
}

void run(void *planned_meta) {
auto planned = reinterpret_cast<PlannedMeta *>(planned_meta);
infini::ops::Handle handle;
handle.set_stream(context::getStream());
infini::ops::Config config;
infini::ops::TopksoftmaxInfinilm::Call(
infini::ops::TopkSoftmax::Call(
handle,
config,
planned->x.tensor(planned->x_tensor),
static_cast<int64_t>(planned->topk),
std::optional<infini::ops::Tensor>{},
std::optional<infini::ops::Tensor>{},
planned->norm != 0,
planned->values.tensor(planned->values_tensor),
planned->indices.tensor(planned->indices_tensor));
planned->indices.tensor(planned->indices_tensor),
planned->token_expert_indices.tensor(planned->token_expert_indices_tensor));
}

void cleanup(void **planned_meta_ptr) {
Expand Down
Loading