#include "SelectExecution.hpp" #include "core/MusaBackend.hpp" namespace MNN { namespace MUSA { template __global__ void SelectKernel(const bool* condition, const T* x, const T* y, T* output, int totalSize) { int index = blockIdx.x * blockDim.x + threadIdx.x; if (index < totalSize) { output[index] = condition[index] ? x[index] : y[index]; } } SelectExecution::SelectExecution(const std::vector& inputs, const MNN::Op* op, Backend* backend) : Execution(inputs, {}, backend) { mBackend = static_cast(backend); } ErrorCode SelectExecution::onResize(const std::vector& inputs, const std::vector& outputs) { auto output = outputs[0]; mTotalSize = 1; for (int i = 0; i < output->dimensions(); i++) { mTotalSize *= output->length(i); } int threads = 256; int blocks = (mTotalSize + threads - 1) / threads; mDim3Grid = {blocks, 1, 1}; mDim3Block = {threads, 1, 1}; return NO_ERROR; } ErrorCode SelectExecution::onExecute(const std::vector& inputs, const std::vector& outputs) { auto condition = inputs[0]; auto x = inputs[1]; auto y = inputs[2]; auto output = outputs[0]; auto conditionPtr = condition->host(); auto xPtr = x->host(); auto yPtr = y->host(); auto outputPtr = output->host(); SelectKernel<<>>( conditionPtr, xPtr, yPtr, outputPtr, mTotalSize ); musaError_t err = musaGetLastError(); if (err != musaSuccess) { return COMPUTE_NO_SUPPORT; } return NO_ERROR; } class SelectCreator : public Creator { public: virtual Execution* onCreate(const std::vector& inputs, const MNN::Op* op, Backend* backend) const override { return new SelectExecution(inputs, op, backend); } }; MNNCreatorRegister gSelectRegistration(OpType_Select); } // namespace MUSA } // namespace MNN