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