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