|
| 1 | +#include "logsoftmax_cpu.h" |
| 2 | +#include "../../../devices/cpu/common_cpu.h" |
| 3 | +#include "../../../reduce/cpu/reduce.h" |
| 4 | +#include <algorithm> |
| 5 | +#include <cmath> |
| 6 | + |
| 7 | +namespace op::logsoftmax::cpu { |
| 8 | + |
| 9 | +Descriptor::~Descriptor() {} |
| 10 | + |
| 11 | +infiniStatus_t Descriptor::create( |
| 12 | + infiniopHandle_t handle, |
| 13 | + Descriptor **desc_ptr, |
| 14 | + infiniopTensorDescriptor_t y_desc, |
| 15 | + infiniopTensorDescriptor_t x_desc) { |
| 16 | + auto result = LogSoftmaxInfo::create(y_desc, x_desc); |
| 17 | + CHECK_RESULT(result); |
| 18 | + *desc_ptr = new Descriptor(nullptr, result.take(), 0, handle->device, handle->device_id); |
| 19 | + return INFINI_STATUS_SUCCESS; |
| 20 | +} |
| 21 | + |
| 22 | +template <typename Tx, typename Ty> |
| 23 | +infiniStatus_t logsoftmax(const LogSoftmaxInfo *info, Ty *y, const Tx *x) { |
| 24 | +#pragma omp parallel for |
| 25 | + for (ptrdiff_t batch = 0; batch < ptrdiff_t(info->batch_size); batch++) { |
| 26 | + ptrdiff_t y_offset, x_offset; |
| 27 | + |
| 28 | + if (info->ndim == 3) { |
| 29 | + // For 3D tensors, convert linear batch index back to 2D indices |
| 30 | + ptrdiff_t batch_idx = batch / info->seq_len; |
| 31 | + ptrdiff_t seq_idx = batch % info->seq_len; |
| 32 | + y_offset = batch_idx * info->y_stride_0 + seq_idx * info->y_stride_1; |
| 33 | + x_offset = batch_idx * info->x_stride_0 + seq_idx * info->x_stride_1; |
| 34 | + } else { |
| 35 | + // For 2D tensors, use the flattened strides |
| 36 | + y_offset = batch * info->y_stride_b; |
| 37 | + x_offset = batch * info->x_stride_b; |
| 38 | + } |
| 39 | + |
| 40 | + Ty *y_ = y + y_offset; |
| 41 | + const Tx *x_ = x + x_offset; |
| 42 | + |
| 43 | + // Find max value for numerical stability |
| 44 | + float max_val; |
| 45 | + if constexpr (std::is_same<Tx, fp16_t>::value || std::is_same<Tx, bf16_t>::value) { |
| 46 | + max_val = op::common_cpu::reduce_op::max(x_, info->probs_size, info->x_stride_p); |
| 47 | + } else { |
| 48 | + max_val = op::common_cpu::reduce_op::max(x_, info->probs_size, info->x_stride_p); |
| 49 | + } |
| 50 | + |
| 51 | + // Compute exp(x - max) and sum |
| 52 | + float sum = 0.0f; |
| 53 | + for (size_t i = 0; i < info->probs_size; i++) { |
| 54 | + float x_val; |
| 55 | + if constexpr (std::is_same<Tx, fp16_t>::value || std::is_same<Tx, bf16_t>::value) { |
| 56 | + x_val = utils::cast<float>(x_[i * info->x_stride_p]); |
| 57 | + } else { |
| 58 | + x_val = x_[i * info->x_stride_p]; |
| 59 | + } |
| 60 | + sum += std::exp(x_val - max_val); |
| 61 | + } |
| 62 | + |
| 63 | + // Compute log(sum) |
| 64 | + float log_sum = std::log(sum); |
| 65 | + |
| 66 | + // Compute log_softmax = x - max - log(sum) |
| 67 | + for (size_t i = 0; i < info->probs_size; i++) { |
| 68 | + float x_val; |
| 69 | + if constexpr (std::is_same<Tx, fp16_t>::value || std::is_same<Tx, bf16_t>::value) { |
| 70 | + x_val = utils::cast<float>(x_[i * info->x_stride_p]); |
| 71 | + } else { |
| 72 | + x_val = x_[i * info->x_stride_p]; |
| 73 | + } |
| 74 | + |
| 75 | + float result = x_val - max_val - log_sum; |
| 76 | + |
| 77 | + if constexpr (std::is_same<Ty, fp16_t>::value || std::is_same<Ty, bf16_t>::value) { |
| 78 | + y_[i * info->y_stride_p] = utils::cast<Ty>(result); |
| 79 | + } else { |
| 80 | + y_[i * info->y_stride_p] = result; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return INFINI_STATUS_SUCCESS; |
| 86 | +} |
| 87 | + |
| 88 | +infiniStatus_t Descriptor::calculate( |
| 89 | + void *workspace, size_t workspace_size, |
| 90 | + void *y, |
| 91 | + const void *x, |
| 92 | + void *stream) const { |
| 93 | + |
| 94 | + // Handle different input/output dtype combinations |
| 95 | + if (_info.x_dtype == INFINI_DTYPE_F16) { |
| 96 | + if (_info.y_dtype == INFINI_DTYPE_F16) { |
| 97 | + return logsoftmax<fp16_t, fp16_t>(&_info, (fp16_t *)y, (const fp16_t *)x); |
| 98 | + } else if (_info.y_dtype == INFINI_DTYPE_BF16) { |
| 99 | + return logsoftmax<fp16_t, bf16_t>(&_info, (bf16_t *)y, (const fp16_t *)x); |
| 100 | + } else if (_info.y_dtype == INFINI_DTYPE_F32) { |
| 101 | + return logsoftmax<fp16_t, float>(&_info, (float *)y, (const fp16_t *)x); |
| 102 | + } else { |
| 103 | + return INFINI_STATUS_BAD_TENSOR_DTYPE; |
| 104 | + } |
| 105 | + } else if (_info.x_dtype == INFINI_DTYPE_BF16) { |
| 106 | + if (_info.y_dtype == INFINI_DTYPE_F16) { |
| 107 | + return logsoftmax<bf16_t, fp16_t>(&_info, (fp16_t *)y, (const bf16_t *)x); |
| 108 | + } else if (_info.y_dtype == INFINI_DTYPE_BF16) { |
| 109 | + return logsoftmax<bf16_t, bf16_t>(&_info, (bf16_t *)y, (const bf16_t *)x); |
| 110 | + } else if (_info.y_dtype == INFINI_DTYPE_F32) { |
| 111 | + return logsoftmax<bf16_t, float>(&_info, (float *)y, (const bf16_t *)x); |
| 112 | + } else { |
| 113 | + return INFINI_STATUS_BAD_TENSOR_DTYPE; |
| 114 | + } |
| 115 | + } else if (_info.x_dtype == INFINI_DTYPE_F32) { |
| 116 | + if (_info.y_dtype == INFINI_DTYPE_F16) { |
| 117 | + return logsoftmax<float, fp16_t>(&_info, (fp16_t *)y, (const float *)x); |
| 118 | + } else if (_info.y_dtype == INFINI_DTYPE_BF16) { |
| 119 | + return logsoftmax<float, bf16_t>(&_info, (bf16_t *)y, (const float *)x); |
| 120 | + } else if (_info.y_dtype == INFINI_DTYPE_F32) { |
| 121 | + return logsoftmax<float, float>(&_info, (float *)y, (const float *)x); |
| 122 | + } else { |
| 123 | + return INFINI_STATUS_BAD_TENSOR_DTYPE; |
| 124 | + } |
| 125 | + } else { |
| 126 | + return INFINI_STATUS_BAD_TENSOR_DTYPE; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace op::logsoftmax::cpu |
0 commit comments