|
| 1 | +#include "../../utils.hpp" |
| 2 | +#include "infinicore/common/hash.hpp" |
| 3 | +#include "infinicore/ops/common/cache.hpp" |
| 4 | +#include "infinicore/ops/conv2d.hpp" |
| 5 | +#include <infiniop.h> |
| 6 | + |
| 7 | +namespace infinicore::op::conv2d_impl::infiniop { |
| 8 | + |
| 9 | +thread_local common::OpCache<size_t, infiniopConvDescriptor_t> caches( |
| 10 | + 100, // capacity |
| 11 | + [](infiniopConvDescriptor_t &desc) { |
| 12 | + if (desc != nullptr) { |
| 13 | + INFINICORE_CHECK_ERROR(infiniopDestroyConvDescriptor(desc)); |
| 14 | + desc = nullptr; |
| 15 | + } |
| 16 | + }); |
| 17 | + |
| 18 | +void calculate(Tensor output, |
| 19 | + Tensor input, |
| 20 | + Tensor weight, |
| 21 | + Tensor bias, |
| 22 | + const size_t *pads, |
| 23 | + const size_t *strides, |
| 24 | + const size_t *dilations, |
| 25 | + size_t n) { |
| 26 | + size_t seed = hash_combine(output, input, weight, bias, n); |
| 27 | + for (size_t i = 0; i < n; ++i) { |
| 28 | + hash_combine(seed, pads[i], strides[i], dilations[i]); |
| 29 | + } |
| 30 | + |
| 31 | + auto device = context::getDevice(); |
| 32 | + auto &cache = caches.getCache(device); |
| 33 | + |
| 34 | + auto desc_opt = cache.get(seed); |
| 35 | + infiniopConvDescriptor_t desc = nullptr; |
| 36 | + |
| 37 | + if (!desc_opt) { |
| 38 | + INFINICORE_CHECK_ERROR(infiniopCreateConvDescriptor( |
| 39 | + context::getInfiniopHandle(device), &desc, |
| 40 | + output->desc(), input->desc(), weight->desc(), |
| 41 | + bias ? bias->desc() : nullptr, |
| 42 | + const_cast<size_t *>(pads), |
| 43 | + const_cast<size_t *>(strides), |
| 44 | + const_cast<size_t *>(dilations), |
| 45 | + n)); |
| 46 | + cache.put(seed, desc); |
| 47 | + } else { |
| 48 | + desc = *desc_opt; |
| 49 | + } |
| 50 | + |
| 51 | + size_t workspace_size = 0; |
| 52 | + INFINICORE_CHECK_ERROR(infiniopGetConvWorkspaceSize(desc, &workspace_size)); |
| 53 | + std::shared_ptr<Memory> workspace = context::allocateMemory(workspace_size); |
| 54 | + |
| 55 | + INFINICORE_CHECK_ERROR(infiniopConv( |
| 56 | + desc, workspace->data(), workspace_size, |
| 57 | + output->data(), |
| 58 | + input->data(), |
| 59 | + weight->data(), |
| 60 | + bias ? bias->data() : nullptr, |
| 61 | + context::getStream())); |
| 62 | +} |
| 63 | + |
| 64 | +static bool registered = []() { |
| 65 | + Conv2d::dispatcher().registerAll(&calculate, false); |
| 66 | + return true; |
| 67 | +}(); |
| 68 | + |
| 69 | +} // namespace infinicore::op::conv2d_impl::infiniop |
0 commit comments