|
| 1 | +#include "../../utils.hpp" |
| 2 | +#include "infinicore/common/hash.hpp" |
| 3 | +#include "infinicore/ops/common/cache.hpp" |
| 4 | +#include "infinicore/ops/convert_to_f32.hpp" |
| 5 | + |
| 6 | +#include <infiniop.h> |
| 7 | + |
| 8 | +namespace infinicore::op::convert_to_f32_impl::infiniop { |
| 9 | + |
| 10 | +thread_local common::OpCache<size_t, infiniopConvertToF32Descriptor_t> caches( |
| 11 | + 100, |
| 12 | + [](infiniopConvertToF32Descriptor_t &desc) { |
| 13 | + if (desc != nullptr) { |
| 14 | + INFINICORE_CHECK_ERROR(infiniopDestroyConvertToF32Descriptor(desc)); |
| 15 | + desc = nullptr; |
| 16 | + } |
| 17 | + }); |
| 18 | + |
| 19 | +void calculate(Tensor output, Tensor input) { |
| 20 | + size_t seed = hash_combine(output, input); |
| 21 | + |
| 22 | + auto device = context::getDevice(); |
| 23 | + auto &cache = caches.getCache(device); |
| 24 | + |
| 25 | + auto desc_opt = cache.get(seed); |
| 26 | + infiniopConvertToF32Descriptor_t desc = nullptr; |
| 27 | + |
| 28 | + if (!desc_opt) { |
| 29 | + INFINICORE_CHECK_ERROR(infiniopCreateConvertToF32Descriptor( |
| 30 | + context::getInfiniopHandle(device), &desc, |
| 31 | + output->desc(), input->desc())); |
| 32 | + cache.put(seed, desc); |
| 33 | + } else { |
| 34 | + desc = *desc_opt; |
| 35 | + } |
| 36 | + |
| 37 | + size_t workspace_size = 0; |
| 38 | + INFINICORE_CHECK_ERROR(infiniopGetConvertToF32WorkspaceSize(desc, &workspace_size)); |
| 39 | + std::shared_ptr<Memory> workspace = context::allocateMemory(workspace_size); |
| 40 | + |
| 41 | + INFINICORE_CHECK_ERROR(infiniopConvertToF32( |
| 42 | + desc, workspace->data(), workspace_size, |
| 43 | + output->data(), input->data(), context::getStream())); |
| 44 | +} |
| 45 | + |
| 46 | +static bool registered = []() { |
| 47 | + ConvertToF32::dispatcher().registerAll(&calculate, false); |
| 48 | + return true; |
| 49 | +}(); |
| 50 | + |
| 51 | +} // namespace infinicore::op::convert_to_f32_impl::infiniop |
0 commit comments