|
| 1 | +#include "cast_cpu.h" |
| 2 | +#include "../../../devices/cpu/common_cpu.h" |
| 3 | +#include "../info.h" |
| 4 | +#include "infinicore.h" |
| 5 | +#include <algorithm> |
| 6 | + |
| 7 | +namespace op::cast::cpu { |
| 8 | + |
| 9 | +Descriptor::~Descriptor() = default; |
| 10 | + |
| 11 | +infiniStatus_t Descriptor::create( |
| 12 | + infiniopHandle_t handle_, |
| 13 | + Descriptor **desc_ptr, |
| 14 | + infiniopTensorDescriptor_t out_desc, |
| 15 | + infiniopTensorDescriptor_t in_desc) { |
| 16 | + |
| 17 | + auto handle = reinterpret_cast<device::cpu::Handle *>(handle_); |
| 18 | + |
| 19 | + auto info_r = CastInfo::create(out_desc, in_desc); |
| 20 | + CHECK_RESULT(info_r); |
| 21 | + |
| 22 | + *desc_ptr = new Descriptor( |
| 23 | + info_r.take(), |
| 24 | + 0, |
| 25 | + nullptr, |
| 26 | + handle->device, handle->device_id); |
| 27 | + |
| 28 | + return INFINI_STATUS_SUCCESS; |
| 29 | +} |
| 30 | + |
| 31 | +size_t Descriptor::workspaceSize() const { |
| 32 | + return _min_workspace_size; |
| 33 | +} |
| 34 | + |
| 35 | +template <typename Tout, typename Tin> |
| 36 | +static inline void cpu_cast_impl_incremental( |
| 37 | + void *output, const void *input, const op::cast::CastInfo &info) { |
| 38 | + |
| 39 | + const size_t ndim = info.shape.size(); |
| 40 | + const size_t n = info.n; |
| 41 | + |
| 42 | + auto out_base = reinterpret_cast<Tout *>(output); |
| 43 | + auto in_base = reinterpret_cast<const Tin *>(input); |
| 44 | + |
| 45 | + const std::vector<size_t> &shape = info.shape; |
| 46 | + const std::vector<ptrdiff_t> &in_stride = info.in_stride; |
| 47 | + const std::vector<ptrdiff_t> &out_stride = info.out_stride; |
| 48 | + |
| 49 | + if (n == 0) return; |
| 50 | + |
| 51 | + std::vector<size_t> idx(ndim, 0); |
| 52 | + ptrdiff_t in_off = 0; |
| 53 | + ptrdiff_t out_off = 0; |
| 54 | + |
| 55 | + for (size_t it = 0; it < n; ++it) { |
| 56 | + const Tin *in_elem = in_base + in_off; |
| 57 | + Tout *out_elem = out_base + out_off; |
| 58 | + *out_elem = utils::cast<Tout, Tin>(*in_elem); |
| 59 | + |
| 60 | + for (int d = static_cast<int>(ndim) - 1; d >= 0; --d) { |
| 61 | + idx[d] += 1; |
| 62 | + if (in_stride[d] != 0) in_off += in_stride[d]; |
| 63 | + if (out_stride[d] != 0) out_off += out_stride[d]; |
| 64 | + |
| 65 | + if (idx[d] < shape[d]) { |
| 66 | + break; |
| 67 | + } else { |
| 68 | + idx[d] = 0; |
| 69 | + if (in_stride[d] != 0) in_off -= static_cast<ptrdiff_t>(shape[d]) * in_stride[d]; |
| 70 | + if (out_stride[d] != 0) out_off -= static_cast<ptrdiff_t>(shape[d]) * out_stride[d]; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +infiniStatus_t Descriptor::calculate( |
| 77 | + void *workspace, |
| 78 | + size_t workspace_size, |
| 79 | + void *output, |
| 80 | + const void *input, |
| 81 | + void *stream) const { |
| 82 | + |
| 83 | + if (output == const_cast<void*>(input)) { |
| 84 | + return INFINI_STATUS_BAD_PARAM; // or INFINI_STATUS_INPLACE_NOT_SUPPORTED |
| 85 | + } |
| 86 | + |
| 87 | + #define CASE_OUT(DT_OUT, TOUT) \ |
| 88 | + case DT_OUT: { \ |
| 89 | + switch (_info.dt_in) { \ |
| 90 | + case INFINI_DTYPE_I32: \ |
| 91 | + cpu_cast_impl_incremental<TOUT, int32_t>(output, input, _info); \ |
| 92 | + break; \ |
| 93 | + case INFINI_DTYPE_I64: \ |
| 94 | + cpu_cast_impl_incremental<TOUT, int64_t>(output, input, _info); \ |
| 95 | + break; \ |
| 96 | + case INFINI_DTYPE_U32: \ |
| 97 | + cpu_cast_impl_incremental<TOUT, uint32_t>(output, input, _info); \ |
| 98 | + break; \ |
| 99 | + case INFINI_DTYPE_U64: \ |
| 100 | + cpu_cast_impl_incremental<TOUT, uint64_t>(output, input, _info); \ |
| 101 | + break; \ |
| 102 | + case INFINI_DTYPE_F16: \ |
| 103 | + cpu_cast_impl_incremental<TOUT, fp16_t>(output, input, _info); \ |
| 104 | + break; \ |
| 105 | + case INFINI_DTYPE_F32: \ |
| 106 | + cpu_cast_impl_incremental<TOUT, float>(output, input, _info); \ |
| 107 | + break; \ |
| 108 | + case INFINI_DTYPE_F64: \ |
| 109 | + cpu_cast_impl_incremental<TOUT, double>(output, input, _info); \ |
| 110 | + break; \ |
| 111 | + default: \ |
| 112 | + return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED; \ |
| 113 | + } \ |
| 114 | + break; \ |
| 115 | + } |
| 116 | + |
| 117 | + switch (_info.dt_out) { |
| 118 | + CASE_OUT(INFINI_DTYPE_I32, int32_t); |
| 119 | + CASE_OUT(INFINI_DTYPE_I64, int64_t); |
| 120 | + CASE_OUT(INFINI_DTYPE_U32, uint32_t); |
| 121 | + CASE_OUT(INFINI_DTYPE_U64, uint64_t); |
| 122 | + CASE_OUT(INFINI_DTYPE_F16, fp16_t); |
| 123 | + CASE_OUT(INFINI_DTYPE_F32, float); |
| 124 | + CASE_OUT(INFINI_DTYPE_F64, double); |
| 125 | + default: |
| 126 | + return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED; |
| 127 | + } |
| 128 | + |
| 129 | + #undef CASE_OUT |
| 130 | + |
| 131 | + return INFINI_STATUS_SUCCESS; |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +} // namespace op::cast::cpu |
0 commit comments