"projects/Task001_Decathlon/README.md" did not exist on "4116e6adfb84c918033a51a0d0e9ee39eadb58d3"
Unverified Commit 784139b9 authored by thatPepe's avatar thatPepe Committed by GitHub
Browse files

Merge pull request #990 from InfiniTensor/demo131

Demo-131 Cuda graph with optimized paged attention
parents 3c8fb3c0 1d6527cb
...@@ -11,7 +11,7 @@ struct PlannedMeta { ...@@ -11,7 +11,7 @@ struct PlannedMeta {
float alpha, beta; float alpha, beta;
}; };
void *plan(Tensor c, Tensor a, Tensor b, float alpha, float beta) { void *plan(Tensor c, const Tensor &a, const Tensor &b, float alpha, float beta) {
size_t seed = hash_combine(c, a, b); size_t seed = hash_combine(c, a, b);
INFINIOP_CACHABLE_DESCRIPTOR_GET_OR_CREATE( INFINIOP_CACHABLE_DESCRIPTOR_GET_OR_CREATE(
......
...@@ -5,23 +5,46 @@ ...@@ -5,23 +5,46 @@
#include "infinicore/ops/common/cache.hpp" #include "infinicore/ops/common/cache.hpp"
#include <infiniop.h> #include <infiniop.h>
#define INFINIOP_CACHABLE_DESCRIPTOR(__DESC_TYPE__, __OP_NAME__, __SIZE__) \ #define INFINIOP_CACHABLE_DESCRIPTOR(__DESC_TYPE__, __OP_NAME__, __SIZE__) \
struct __DESC_TYPE__ { \ struct __DESC_TYPE__ { \
infiniop##__OP_NAME__##Descriptor_t desc; \ infiniop##__OP_NAME__##Descriptor_t desc = nullptr; \
Descriptor(infiniop##__OP_NAME__##Descriptor_t desc) : desc(desc) {} \ \
~Descriptor() { \ explicit __DESC_TYPE__(infiniop##__OP_NAME__##Descriptor_t d) \
if (desc != nullptr) { \ : desc(d) {} \
infiniopDestroy##__OP_NAME__##Descriptor(desc); \ \
desc = nullptr; \ /* non-copyable */ \
} \ __DESC_TYPE__(const __DESC_TYPE__ &) = delete; \
} \ __DESC_TYPE__ &operator=(const __DESC_TYPE__ &) = delete; \
}; \ \
\ /* movable */ \
thread_local common::OpCache<size_t, std::shared_ptr<__DESC_TYPE__>> \ __DESC_TYPE__(__DESC_TYPE__ &&other) noexcept \
caches( \ : desc(other.desc) { \
__SIZE__, \ other.desc = nullptr; \
[](std::shared_ptr<__DESC_TYPE__> &desc) { \ } \
desc = nullptr; \ \
__DESC_TYPE__ &operator=(__DESC_TYPE__ &&other) noexcept { \
if (this != &other) { \
if (desc != nullptr) { \
infiniopDestroy##__OP_NAME__##Descriptor(desc); \
} \
desc = other.desc; \
other.desc = nullptr; \
} \
return *this; \
} \
\
~__DESC_TYPE__() { \
if (desc != nullptr) { \
infiniopDestroy##__OP_NAME__##Descriptor(desc); \
} \
} \
}; \
\
thread_local common::OpCache<size_t, std::shared_ptr<__DESC_TYPE__>> \
caches( \
__SIZE__, \
[](std::shared_ptr<__DESC_TYPE__> &desc) { \
desc = nullptr; \
}); });
#define INFINIOP_CACHABLE_DESCRIPTOR_GET_OR_CREATE(__DESC_TYPE__, __DESC_NAME__, __INFINIOP_NAME__, __HASH_KEY__, ...) \ #define INFINIOP_CACHABLE_DESCRIPTOR_GET_OR_CREATE(__DESC_TYPE__, __DESC_NAME__, __INFINIOP_NAME__, __HASH_KEY__, ...) \
......
#include "infinicore/ops/kv_caching.hpp"
#include "../../utils.hpp"
namespace infinicore::op {
INFINICORE_GRAPH_OP_DISPATCHERS_IMPL(KVCaching);
KVCaching::KVCaching(Tensor k_cache,
Tensor v_cache,
const Tensor &k,
const Tensor &v,
const Tensor &past_kv_lengths) {
INFINICORE_ASSERT_TENSORS_SAME_DEVICE(k_cache, v_cache, k, v, past_kv_lengths);
INFINICORE_GRAPH_OP_DISPATCH(k_cache->device().getType(),
k_cache,
v_cache,
k,
v,
past_kv_lengths);
}
void KVCaching::execute(Tensor k_cache,
Tensor v_cache,
const Tensor &k,
const Tensor &v,
const Tensor &past_kv_lengths) {
INFINICORE_GRAPH_OP_RECORD_OR_RUN(KVCaching,
k_cache,
v_cache,
k,
v,
past_kv_lengths);
}
void kv_caching_(Tensor k_cache,
Tensor v_cache,
const Tensor &k,
const Tensor &v,
const Tensor &past_kv_lengths) {
KVCaching::execute(k_cache, v_cache, k, v, past_kv_lengths);
}
} // namespace infinicore::op
#include "../infiniop_impl.hpp"
#include "infinicore/ops/kv_caching.hpp"
namespace infinicore::op::kv_caching_impl::infiniop {
INFINIOP_CACHABLE_DESCRIPTOR(Descriptor, KVCaching, 100);
struct PlannedMeta {
std::shared_ptr<Descriptor> descriptor;
graph::GraphTensor workspace, k_cache, v_cache, k, v, past_kv_lengths;
};
void *plan(Tensor k_cache,
Tensor v_cache,
const Tensor &k,
const Tensor &v,
const Tensor &past_kv_lengths) {
size_t seed = hash_combine(k_cache, v_cache, k, v, past_kv_lengths);
INFINIOP_CACHABLE_DESCRIPTOR_GET_OR_CREATE(
Descriptor, descriptor, KVCaching,
seed, k_cache->desc(), v_cache->desc(),
k->desc(), v->desc(), past_kv_lengths->desc());
INFINIOP_WORKSPACE_TENSOR(workspace, KVCaching, descriptor);
auto planned = new PlannedMeta{
descriptor,
graph::GraphTensor(workspace),
graph::GraphTensor(k_cache),
graph::GraphTensor(v_cache),
graph::GraphTensor(k),
graph::GraphTensor(v),
graph::GraphTensor(past_kv_lengths)};
return planned;
}
void run(void *planned_meta) {
auto planned = reinterpret_cast<PlannedMeta *>(planned_meta);
INFINICORE_CHECK_ERROR(infiniopKVCaching(
planned->descriptor->desc,
nullptr, 0,
planned->k_cache->data(),
planned->v_cache->data(),
planned->k->data(),
planned->v->data(),
planned->past_kv_lengths->data(),
context::getStream()));
}
void cleanup(void **planned_meta_ptr) {
delete *reinterpret_cast<PlannedMeta **>(planned_meta_ptr);
*planned_meta_ptr = nullptr;
}
INFINICORE_GRAPH_OP_REGISTER_ALLDEVICE(KVCaching, &plan, &run, cleanup);
} // namespace infinicore::op::kv_caching_impl::infiniop
#include "infinicore/ops/linear_w4a16_awq.hpp"
#include "infinicore/ops/dequantize_awq.hpp"
#include "infinicore/ops/gemm.hpp"
namespace infinicore::op {
Tensor linear_w4a16_awq(Tensor input,
Tensor weight_packed,
Tensor weight_scale,
Tensor weight_zeros,
std::optional<Tensor> bias) {
// Input is of shape [M, K], Weight_packed is of shape [N, K],stirdes is [N, 1]
Size ndim = input->ndim();
Size out_features = weight_packed->shape()[0];
// Assign memory to out variables
auto output_shape = input->shape();
output_shape[ndim - 1] = out_features;
auto out = Tensor::empty(output_shape, input->dtype(), input->device());
// Inplace Calculate
linear_w4a16_awq_(out, input, weight_packed, weight_scale, weight_zeros, bias);
return out;
}
void linear_w4a16_awq_(Tensor out,
Tensor input,
Tensor weight_packed,
Tensor weight_scale,
Tensor weight_zeros,
std::optional<Tensor> bias) {
auto weight_packed_shape = weight_packed->shape();
Size out_features = weight_packed_shape[0];
Size in_features = weight_packed_shape[1];
Size ndim = input->ndim();
assert(out->ndim() == ndim);
Size N = 1;
auto input_shape = input->shape();
for (size_t i = 0; i < ndim - 1; ++i) {
N *= input_shape[i];
}
auto weight = Tensor::empty(
{out_features, in_features},
out->dtype(),
weight_packed->device());
float alpha = 1.0f;
float beta = 0.0f;
op::dequantize_awq_(weight, weight_packed, weight_scale, weight_zeros);
bias = std::make_optional(bias.value()->as_strided({N, out_features}, {0, 1}));
gemm_(out->view({N, out_features}),
input->view({N, in_features}),
weight->permute({1, 0}), alpha, beta);
}
} // namespace infinicore::op
#include "infinicore/ops/linear_w8a8i8.hpp"
#include "infinicore/ops/per_channel_quant_i8.hpp"
#include "infinicore/ops/scaled_mm_i8.hpp"
namespace infinicore::op {
Tensor linear_w8a8i8(Tensor input,
Tensor weight_packed,
Tensor weight_scale,
std::optional<Tensor> bias) {
// Input is of shape [M, K], Weight_packed is of shape [N, K],stirdes is [N, 1]
Size ndim = input->ndim();
Size out_features = weight_packed->shape()[0];
// Assign memory to out variables
auto output_shape = input->shape();
output_shape[ndim - 1] = out_features;
auto out = Tensor::empty(output_shape, input->dtype(), input->device());
// Inplace Calculate
linear_w8a8i8_(out, input, weight_packed, weight_scale, bias);
return out;
}
void linear_w8a8i8_(Tensor out,
Tensor input,
Tensor weight_packed,
Tensor weight_scale,
std::optional<Tensor> bias) {
auto weight_packed_shape = weight_packed->shape();
Size out_features = weight_packed_shape[0];
Size in_features = weight_packed_shape[1];
Size ndim = input->ndim();
assert(out->ndim() == ndim);
Size N = 1;
auto input_shape = input->shape();
for (size_t i = 0; i < ndim - 1; ++i) {
N *= input_shape[i];
}
auto input_packed = Tensor::empty(
{N, input_shape[ndim - 1]},
DataType::I8,
input->device());
auto input_scale = Tensor::empty(
{N, 1},
DataType::F32,
input->device());
op::per_channel_quant_i8_(input->view({N, in_features}), input_packed, input_scale);
if (bias.has_value()) {
bias = std::make_optional(bias.value()->as_strided({N, out_features}, {0, 1}));
}
op::scaled_mm_i8_(
out->view({N, out_features}),
input_packed,
input_scale,
weight_packed->permute({1, 0}),
weight_scale,
bias);
}
} // namespace infinicore::op
#include "infinicore/ops/mul.hpp" #include "infinicore/ops/mul.hpp"
#include "../../utils.hpp" #include "../../utils.hpp"
namespace infinicore::op { namespace infinicore::op {
common::OpDispatcher<Mul::schema> &Mul::dispatcher() { INFINICORE_GRAPH_OP_DISPATCHERS_IMPL(Mul);
static common::OpDispatcher<Mul::schema> dispatcher_;
return dispatcher_;
};
void Mul::execute(Tensor c, Tensor a, Tensor b) { Mul::Mul(Tensor c, const Tensor &a, const Tensor &b) {
INFINICORE_ASSERT_TENSORS_SAME_DEVICE(c, a, b); INFINICORE_ASSERT_TENSORS_SAME_DEVICE(c, a, b);
infinicore::context::setDevice(c->device()); INFINICORE_GRAPH_OP_DISPATCH(c->device().getType(), c, a, b);
dispatcher().lookup(c->device().getType())(c, a, b); }
void Mul::execute(Tensor c, const Tensor &a, const Tensor &b) {
INFINICORE_GRAPH_OP_RECORD_OR_RUN(Mul, c, a, b);
} }
Tensor mul(Tensor a, Tensor b) { Tensor mul(const Tensor &a, const Tensor &b) {
auto c = Tensor::empty(a->shape(), a->dtype(), a->device()); auto c = Tensor::empty(a->shape(), a->dtype(), a->device());
mul_(c, a, b); mul_(c, a, b);
return c; return c;
} }
void mul_(Tensor c, Tensor a, Tensor b) { void mul_(Tensor c, const Tensor &a, const Tensor &b) {
Mul::execute(c, a, b); Mul::execute(c, a, b);
} }
......
#include "../../utils.hpp"
#include "infinicore/common/hash.hpp"
#include "infinicore/ops/common/cache.hpp"
#include "infinicore/ops/mul.hpp" #include "infinicore/ops/mul.hpp"
#include <infiniop.h>
#include "../infiniop_impl.hpp"
namespace infinicore::op::mul_impl::infiniop { namespace infinicore::op::mul_impl::infiniop {
thread_local common::OpCache<size_t, infiniopMulDescriptor_t> caches( INFINIOP_CACHABLE_DESCRIPTOR(Descriptor, Mul, 100);
100, // capacity
[](infiniopMulDescriptor_t &desc) { struct PlannedMeta {
if (desc != nullptr) { std::shared_ptr<Descriptor> descriptor;
INFINICORE_CHECK_ERROR(infiniopDestroyMulDescriptor(desc)); graph::GraphTensor workspace, c, a, b;
desc = nullptr; };
}
});
void calculate(Tensor c, Tensor a, Tensor b) { void *plan(Tensor c, const Tensor &a, const Tensor &b) {
size_t seed = hash_combine(c, b, a); size_t seed = hash_combine(c, b, a);
auto device = context::getDevice(); INFINIOP_CACHABLE_DESCRIPTOR_GET_OR_CREATE(
auto &cache = caches.getCache(device); Descriptor, descriptor, Mul,
seed, c->desc(), a->desc(), b->desc());
auto desc_opt = cache.get(seed); INFINIOP_WORKSPACE_TENSOR(workspace, Mul, descriptor);
infiniopMulDescriptor_t desc = nullptr;
if (!desc_opt) { return new PlannedMeta{
INFINICORE_CHECK_ERROR(infiniopCreateMulDescriptor( descriptor,
context::getInfiniopHandle(device), &desc, graph::GraphTensor(workspace),
c->desc(), a->desc(), b->desc())); graph::GraphTensor(c),
cache.put(seed, desc); graph::GraphTensor(a),
} else { graph::GraphTensor(b)};
desc = *desc_opt; }
}
size_t workspace_size = 0; void run(void *planned_meta) {
INFINICORE_CHECK_ERROR(infiniopGetMulWorkspaceSize(desc, &workspace_size)); auto planned = reinterpret_cast<PlannedMeta *>(planned_meta);
std::shared_ptr<Memory> workspace = context::allocateMemory(workspace_size);
INFINICORE_CHECK_ERROR(infiniopMul( INFINICORE_CHECK_ERROR(infiniopMul(
desc, workspace->data(), workspace_size, planned->descriptor->desc,
c->data(), a->data(), b->data(), context::getStream())); planned->workspace->data(),
planned->workspace->numel(),
planned->c->data(),
planned->a->data(),
planned->b->data(),
context::getStream()));
}
void cleanup(void **planned_meta_ptr) {
delete *reinterpret_cast<PlannedMeta **>(planned_meta_ptr);
*planned_meta_ptr = nullptr;
} }
static bool registered = []() { INFINICORE_GRAPH_OP_REGISTER_ALLDEVICE(Mul, &plan, &run, &cleanup);
Mul::dispatcher().registerAll(&calculate, false);
return true;
}();
} // namespace infinicore::op::mul_impl::infiniop } // namespace infinicore::op::mul_impl::infiniop
#include "infinicore/ops/paged_attention.hpp" #include "infinicore/ops/paged_attention.hpp"
#include "../../utils.hpp" #include "../../utils.hpp"
namespace infinicore::op { namespace infinicore::op {
common::OpDispatcher<PagedAttention::schema> &PagedAttention::dispatcher() { INFINICORE_GRAPH_OP_DISPATCHERS_IMPL(PagedAttention);
static common::OpDispatcher<PagedAttention::schema> dispatcher_;
return dispatcher_;
};
void PagedAttention::execute(Tensor out, Tensor q, Tensor k_cache, Tensor v_cache, Tensor block_tables, Tensor kv_lens, std::optional<Tensor> alibi_slopes, float scale) { PagedAttention::PagedAttention(Tensor out, const Tensor &q, const Tensor &k_cache, const Tensor &v_cache,
const Tensor &block_tables, const Tensor &kv_lens,
std::optional<Tensor> alibi_slopes, float scale) {
INFINICORE_ASSERT_TENSORS_SAME_DEVICE(out, q, k_cache, v_cache, block_tables, kv_lens); INFINICORE_ASSERT_TENSORS_SAME_DEVICE(out, q, k_cache, v_cache, block_tables, kv_lens);
infinicore::context::setDevice(out->device()); INFINICORE_GRAPH_OP_DISPATCH(out->device().getType(),
dispatcher().lookup(out->device().getType())(out, q, k_cache, v_cache, block_tables, kv_lens, alibi_slopes, scale); out, q, k_cache, v_cache, block_tables, kv_lens, alibi_slopes, scale);
}
void PagedAttention::execute(Tensor out, const Tensor &q, const Tensor &k_cache, const Tensor &v_cache,
const Tensor &block_tables, const Tensor &kv_lens,
std::optional<Tensor> alibi_slopes, float scale) {
INFINICORE_GRAPH_OP_RECORD_OR_RUN(
PagedAttention,
out, q, k_cache, v_cache, block_tables, kv_lens, alibi_slopes, scale);
} }
Tensor paged_attention(Tensor q, Tensor k_cache, Tensor v_cache, Tensor block_tables, Tensor kv_lens, std::optional<Tensor> alibi_slopes, float scale) { Tensor paged_attention(const Tensor &q, const Tensor &k_cache, const Tensor &v_cache,
const Tensor &block_tables, const Tensor &kv_lens,
std::optional<Tensor> alibi_slopes, float scale) {
auto out = Tensor::empty(q->shape(), q->dtype(), q->device()); auto out = Tensor::empty(q->shape(), q->dtype(), q->device());
paged_attention_(out, q, k_cache, v_cache, block_tables, kv_lens, alibi_slopes, scale); paged_attention_(out, q, k_cache, v_cache, block_tables, kv_lens, alibi_slopes, scale);
return out; return out;
} }
void paged_attention_(Tensor out, Tensor q, Tensor k_cache, Tensor v_cache, Tensor block_tables, Tensor kv_lens, std::optional<Tensor> alibi_slopes, float scale) { void paged_attention_(Tensor out, const Tensor &q, const Tensor &k_cache, const Tensor &v_cache,
const Tensor &block_tables, const Tensor &kv_lens,
std::optional<Tensor> alibi_slopes, float scale) {
PagedAttention::execute(out, q, k_cache, v_cache, block_tables, kv_lens, alibi_slopes, scale); PagedAttention::execute(out, q, k_cache, v_cache, block_tables, kv_lens, alibi_slopes, scale);
} }
......
#include "../../utils.hpp"
#include "infinicore/common/hash.hpp"
#include "infinicore/ops/common/cache.hpp"
#include "infinicore/ops/paged_attention.hpp" #include "infinicore/ops/paged_attention.hpp"
#include <infiniop.h>
#include "../infiniop_impl.hpp"
namespace infinicore::op::paged_attention_impl::infiniop { namespace infinicore::op::paged_attention_impl::infiniop {
thread_local common::OpCache<size_t, infiniopPagedAttentionDescriptor_t> caches( INFINIOP_CACHABLE_DESCRIPTOR(Descriptor, PagedAttention, 100);
100, // capacity
[](infiniopPagedAttentionDescriptor_t &desc) { struct PlannedMeta {
if (desc != nullptr) { std::shared_ptr<Descriptor> descriptor;
INFINICORE_CHECK_ERROR(infiniopDestroyPagedAttentionDescriptor(desc)); graph::GraphTensor workspace, out, q, k_cache, v_cache, block_tables, cache_lens;
desc = nullptr; std::optional<graph::GraphTensor> alibi_slopes;
} float scale;
}); };
void calculate(Tensor out, Tensor q, Tensor k_cache, Tensor v_cache, Tensor block_tables, Tensor kv_lens, std::optional<Tensor> alibi_slopes, float scale) { void *plan(Tensor out, const Tensor &q, const Tensor &k_cache, const Tensor &v_cache,
size_t seed = hash_combine(out, q, k_cache, v_cache, block_tables, kv_lens, alibi_slopes, scale); const Tensor &block_tables, const Tensor &cache_lens,
std::optional<Tensor> alibi_slopes, float scale) {
auto device = context::getDevice(); size_t seed = hash_combine(out, q, k_cache, v_cache, block_tables, cache_lens, alibi_slopes);
auto &cache = caches.getCache(device); INFINIOP_CACHABLE_DESCRIPTOR_GET_OR_CREATE(
Descriptor, descriptor, PagedAttention,
auto desc_opt = cache.get(seed); seed,
infiniopPagedAttentionDescriptor_t desc = nullptr; out->desc(), q->desc(), k_cache->desc(), v_cache->desc(),
block_tables->desc(), cache_lens->desc(),
if (!desc_opt) { alibi_slopes ? alibi_slopes.value()->desc() : nullptr,
INFINICORE_CHECK_ERROR(infiniopCreatePagedAttentionDescriptor( scale);
context::getInfiniopHandle(device), &desc,
out->desc(), q->desc(), k_cache->desc(), v_cache->desc(), block_tables->desc(), kv_lens->desc(), INFINIOP_WORKSPACE_TENSOR(workspace, PagedAttention, descriptor);
alibi_slopes.has_value() ? alibi_slopes.value()->desc() : nullptr,
scale)); return new PlannedMeta{
cache.put(seed, desc); descriptor,
} else { graph::GraphTensor(workspace),
desc = *desc_opt; graph::GraphTensor(out),
} graph::GraphTensor(q),
graph::GraphTensor(k_cache),
size_t workspace_size = 0; graph::GraphTensor(v_cache),
INFINICORE_CHECK_ERROR(infiniopGetPagedAttentionWorkspaceSize(desc, &workspace_size)); graph::GraphTensor(block_tables),
std::shared_ptr<Memory> workspace = context::allocateMemory(workspace_size); graph::GraphTensor(cache_lens),
alibi_slopes ? std::optional<graph::GraphTensor>(graph::GraphTensor(*alibi_slopes)) : std::nullopt,
INFINICORE_CHECK_ERROR(infiniopPagedAttention( scale};
desc, workspace->data(), workspace_size, }
out->data(), q->data(), k_cache->data(), v_cache->data(), block_tables->data(), kv_lens->data(),
alibi_slopes.has_value() ? alibi_slopes.value()->data() : nullptr, void run(void *planned_meta) {
context::getStream())); auto *p = reinterpret_cast<PlannedMeta *>(planned_meta);
INFINICORE_CHECK_ERROR(
infiniopPagedAttention(
p->descriptor->desc,
p->workspace->data(),
p->workspace->numel(),
p->out->data(),
p->q->data(),
p->k_cache->data(),
p->v_cache->data(),
p->block_tables->data(),
p->cache_lens->data(),
p->alibi_slopes.has_value() ? p->alibi_slopes.value()->data() : nullptr,
context::getStream()));
}
void cleanup(void **planned_meta_ptr) {
delete *reinterpret_cast<PlannedMeta **>(planned_meta_ptr);
*planned_meta_ptr = nullptr;
} }
static bool registered = []() { INFINICORE_GRAPH_OP_REGISTER_ALLDEVICE(PagedAttention, &plan, &run, &cleanup);
PagedAttention::dispatcher().registerAll(&calculate, false);
return true;
}();
} // namespace infinicore::op::paged_attention_impl::infiniop } // namespace infinicore::op::paged_attention_impl::infiniop
#include "infinicore/ops/paged_caching.hpp" #include "infinicore/ops/paged_caching.hpp"
#include "../../utils.hpp" #include "../../utils.hpp"
namespace infinicore::op { namespace infinicore::op {
common::OpDispatcher<PagedCaching::schema> &PagedCaching::dispatcher() { INFINICORE_GRAPH_OP_DISPATCHERS_IMPL(PagedCaching);
static common::OpDispatcher<PagedCaching::schema> dispatcher_;
return dispatcher_;
};
void PagedCaching::execute(Tensor k_cache, Tensor v_cache, Tensor k, Tensor v, Tensor slot_mapping) { PagedCaching::PagedCaching(Tensor k_cache, Tensor v_cache, const Tensor &k, const Tensor &v, const Tensor &slot_mapping) {
INFINICORE_ASSERT_TENSORS_SAME_DEVICE(k_cache, v_cache, k, v, slot_mapping); INFINICORE_ASSERT_TENSORS_SAME_DEVICE(k_cache, v_cache, k, v, slot_mapping);
infinicore::context::setDevice(k_cache->device()); INFINICORE_GRAPH_OP_DISPATCH(k->device().getType(), k_cache, v_cache, k, v, slot_mapping);
dispatcher().lookup(k_cache->device().getType())(k_cache, v_cache, k, v, slot_mapping); }
void PagedCaching::execute(Tensor k_cache, Tensor v_cache, const Tensor &k, const Tensor &v, const Tensor &slot_mapping) {
INFINICORE_GRAPH_OP_RECORD_OR_RUN(PagedCaching, k_cache, v_cache, k, v, slot_mapping);
} }
void paged_caching_(Tensor k_cache, Tensor v_cache, Tensor k, Tensor v, Tensor slot_mapping) { void paged_caching_(Tensor k_cache, Tensor v_cache, const Tensor &k, const Tensor &v, const Tensor &slot_mapping) {
PagedCaching::execute(k_cache, v_cache, k, v, slot_mapping); PagedCaching::execute(k_cache, v_cache, k, v, slot_mapping);
} }
......
#include "../../utils.hpp"
#include "infinicore/common/hash.hpp"
#include "infinicore/ops/common/cache.hpp"
#include "infinicore/ops/paged_caching.hpp" #include "infinicore/ops/paged_caching.hpp"
#include <infiniop.h>
#include "../infiniop_impl.hpp"
namespace infinicore::op::paged_caching_impl::infiniop { namespace infinicore::op::paged_caching_impl::infiniop {
thread_local common::OpCache<size_t, infiniopPagedCachingDescriptor_t> caches( INFINIOP_CACHABLE_DESCRIPTOR(Descriptor, PagedCaching, 100);
100, // capacity
[](infiniopPagedCachingDescriptor_t &desc) { struct PlannedMeta {
if (desc != nullptr) { std::shared_ptr<Descriptor> descriptor;
INFINICORE_CHECK_ERROR(infiniopDestroyPagedCachingDescriptor(desc));
desc = nullptr; graph::GraphTensor workspace, k_cache, v_cache, k, v, slot_mapping;
} };
});
void *plan(Tensor k_cache, Tensor v_cache, const Tensor &k, const Tensor &v, const Tensor &slot_mapping) {
void calculate(Tensor k_cache, Tensor v_cache, Tensor k, Tensor v, Tensor slot_mapping) { size_t key = hash_combine(k_cache, v_cache, k, v, slot_mapping);
size_t seed = hash_combine(k_cache, v_cache, k, v, slot_mapping);
INFINIOP_CACHABLE_DESCRIPTOR_GET_OR_CREATE(
auto device = context::getDevice(); Descriptor, descriptor, PagedCaching,
auto &cache = caches.getCache(device); key, k_cache->desc(), v_cache->desc(), k->desc(), v->desc(), slot_mapping->desc());
auto desc_opt = cache.get(seed); INFINIOP_WORKSPACE_TENSOR(workspace, PagedCaching, descriptor);
infiniopPagedCachingDescriptor_t desc = nullptr;
return new PlannedMeta{
if (!desc_opt) { descriptor,
INFINICORE_CHECK_ERROR(infiniopCreatePagedCachingDescriptor( graph::GraphTensor(workspace),
context::getInfiniopHandle(device), &desc, graph::GraphTensor(k_cache),
k_cache->desc(), v_cache->desc(), k->desc(), v->desc(), slot_mapping->desc())); graph::GraphTensor(v_cache),
cache.put(seed, desc); graph::GraphTensor(k),
} else { graph::GraphTensor(v),
desc = *desc_opt; graph::GraphTensor(slot_mapping)};
} }
size_t workspace_size = 0; void run(void *planned_meta) {
INFINICORE_CHECK_ERROR(infiniopGetPagedCachingWorkspaceSize(desc, &workspace_size)); auto *p = reinterpret_cast<PlannedMeta *>(planned_meta);
std::shared_ptr<Memory> workspace = context::allocateMemory(workspace_size);
INFINICORE_CHECK_ERROR(
INFINICORE_CHECK_ERROR(infiniopPagedCaching( infiniopPagedCaching(
desc, workspace->data(), workspace_size, p->descriptor->desc,
k_cache->data(), v_cache->data(), k->data(), v->data(), slot_mapping->data(), context::getStream())); p->workspace->data(),
p->workspace->numel(),
p->k_cache->data(),
p->v_cache->data(),
p->k->data(),
p->v->data(),
p->slot_mapping->data(),
context::getStream()));
}
void cleanup(void **planned_meta_ptr) {
delete *reinterpret_cast<PlannedMeta **>(planned_meta_ptr);
*planned_meta_ptr = nullptr;
} }
static bool registered = []() { INFINICORE_GRAPH_OP_REGISTER_ALLDEVICE(PagedCaching, &plan, &run, &cleanup);
PagedCaching::dispatcher().registerAll(&calculate, false);
return true;
}();
} // namespace infinicore::op::paged_caching_impl::infiniop } // namespace infinicore::op::paged_caching_impl::infiniop
#include "infinicore/ops/per_channel_quant_i8.hpp"
#include "../../utils.hpp"
namespace infinicore::op {
INFINICORE_GRAPH_OP_DISPATCHERS_IMPL(PerChannelQuantI8);
PerChannelQuantI8::PerChannelQuantI8(const Tensor &x, Tensor x_packed, Tensor x_scale) {
INFINICORE_ASSERT_TENSORS_SAME_DEVICE(x, x_packed, x_scale);
INFINICORE_GRAPH_OP_DISPATCH(x->device().getType(), x, x_packed, x_scale);
}
void PerChannelQuantI8::execute(const Tensor &x, Tensor x_packed, Tensor x_scale) {
INFINICORE_GRAPH_OP_RECORD_OR_RUN(PerChannelQuantI8, x, x_packed, x_scale);
}
void per_channel_quant_i8_(const Tensor &x, Tensor x_packed, Tensor x_scale) {
PerChannelQuantI8::execute(x, x_packed, x_scale);
}
} // namespace infinicore::op
#include "../../utils.hpp"
#include "../infiniop_impl.hpp"
#include "infinicore/common/hash.hpp"
#include "infinicore/ops/common/cache.hpp"
#include "infinicore/ops/per_channel_quant_i8.hpp"
#include <infiniop.h>
namespace infinicore::op::per_channel_quant_i8_impl::infiniop {
INFINIOP_CACHABLE_DESCRIPTOR(Descriptor, PerChannelQuantI8, 100);
struct PlannedMeta {
std::shared_ptr<Descriptor> descriptor;
graph::GraphTensor workspace, x, x_packed, x_scale;
};
void *plan(const Tensor &x, Tensor x_packed, Tensor x_scale) {
size_t seed = hash_combine(x, x_packed, x_scale);
INFINIOP_CACHABLE_DESCRIPTOR_GET_OR_CREATE(
Descriptor, descriptor, PerChannelQuantI8,
seed,
x_packed->desc(), x_scale->desc(), nullptr, x->desc());
INFINIOP_WORKSPACE_TENSOR(workspace, PerChannelQuantI8, descriptor);
return new PlannedMeta{
descriptor,
graph::GraphTensor(workspace),
graph::GraphTensor(x),
graph::GraphTensor(x_packed),
graph::GraphTensor(x_scale)};
}
void run(void *planned_meta) {
auto planned = reinterpret_cast<PlannedMeta *>(planned_meta);
INFINICORE_CHECK_ERROR(infiniopPerChannelQuantI8(
planned->descriptor->desc,
planned->workspace->data(),
planned->workspace->numel(),
planned->x_packed->data(),
planned->x_scale->data(),
nullptr,
planned->x->data(),
context::getStream()));
}
void cleanup(void **planned_meta_ptr) {
delete *reinterpret_cast<PlannedMeta **>(planned_meta_ptr);
*planned_meta_ptr = nullptr;
}
INFINICORE_GRAPH_OP_REGISTER_ALLDEVICE(PerChannelQuantI8, &plan, &run, &cleanup);
} // namespace infinicore::op::per_channel_quant_i8_impl::infiniop
...@@ -3,24 +3,30 @@ ...@@ -3,24 +3,30 @@
namespace infinicore::op { namespace infinicore::op {
common::OpDispatcher<Rearrange::schema> &Rearrange::dispatcher() { INFINICORE_GRAPH_OP_DISPATCHERS_IMPL(Rearrange);
static common::OpDispatcher<Rearrange::schema> dispatcher_;
return dispatcher_;
};
void Rearrange::execute(Tensor y, Tensor x) { Rearrange::Rearrange(Tensor y, const Tensor &x) {
INFINICORE_ASSERT_TENSORS_SAME_DEVICE(y, x); INFINICORE_ASSERT_TENSORS_SAME_DEVICE(y, x);
infinicore::context::setDevice(y->device()); INFINICORE_GRAPH_OP_DISPATCH(y->device().getType(), y, x);
dispatcher().lookup(y->device().getType())(y, x);
} }
Tensor rearrange(Tensor x) { void Rearrange::execute(Tensor y, const Tensor &x) {
auto op = std::make_shared<Rearrange>(y, x);
if (context::isGraphRecording()) {
context::addGraphOperator(op);
} else {
op->run();
}
}
Tensor rearrange(const Tensor &x) {
auto y = Tensor::empty(x->shape(), x->dtype(), x->device()); auto y = Tensor::empty(x->shape(), x->dtype(), x->device());
rearrange_(y, x); rearrange_(y, x);
return y; return y;
} }
void rearrange_(Tensor y, Tensor x) { void rearrange_(Tensor y, const Tensor &x) {
Rearrange::execute(y, x); Rearrange::execute(y, x);
} }
} // namespace infinicore::op } // namespace infinicore::op
#include "../../utils.hpp"
#include "infinicore/common/hash.hpp"
#include "infinicore/ops/common/cache.hpp"
#include "infinicore/ops/rearrange.hpp" #include "infinicore/ops/rearrange.hpp"
#include <infiniop.h>
#include "../infiniop_impl.hpp"
namespace infinicore::op::rearrange_impl::infiniop { namespace infinicore::op::rearrange_impl::infiniop {
thread_local common::OpCache<size_t, infiniopRearrangeDescriptor_t> caches( INFINIOP_CACHABLE_DESCRIPTOR(Descriptor, Rearrange, 100);
100, // capacity
[](infiniopRearrangeDescriptor_t &desc) { struct PlannedMeta {
if (desc != nullptr) { std::shared_ptr<Descriptor> descriptor;
INFINICORE_CHECK_ERROR(infiniopDestroyRearrangeDescriptor(desc)); graph::GraphTensor y, x;
desc = nullptr; };
}
});
void calculate(Tensor y, Tensor x) { void *plan(Tensor y, const Tensor &x) {
size_t seed = hash_combine(y, x); size_t seed = hash_combine(y, x);
auto device = context::getDevice(); INFINIOP_CACHABLE_DESCRIPTOR_GET_OR_CREATE(
auto &cache = caches.getCache(device); Descriptor, descriptor, Rearrange,
seed, y->desc(),
x->desc());
auto desc_opt = cache.get(seed); return new PlannedMeta{
infiniopRearrangeDescriptor_t desc = nullptr; descriptor,
graph::GraphTensor(y),
graph::GraphTensor(x)};
}
if (!desc_opt) { void run(void *planned_meta) {
INFINICORE_CHECK_ERROR(infiniopCreateRearrangeDescriptor(context::getInfiniopHandle(device), &desc, y->desc(), x->desc())); auto planned = reinterpret_cast<PlannedMeta *>(planned_meta);
cache.put(seed, desc);
} else {
desc = *desc_opt;
}
INFINICORE_CHECK_ERROR( INFINICORE_CHECK_ERROR(
infiniopRearrange( infiniopRearrange(
desc, planned->descriptor->desc,
y->data(), planned->y->data(),
x->data(), planned->x->data(),
context::getStream())); context::getStream()));
} }
static bool registered = []() { void cleanup(void **planned_meta_ptr) {
Rearrange::dispatcher().registerAll(&calculate, false); delete *reinterpret_cast<PlannedMeta **>(planned_meta_ptr);
return true; *planned_meta_ptr = nullptr;
}(); }
INFINICORE_GRAPH_OP_REGISTER_ALLDEVICE(Rearrange, &plan, &run, &cleanup);
} // namespace infinicore::op::rearrange_impl::infiniop } // namespace infinicore::op::rearrange_impl::infiniop
#include "infinicore/ops/rms_norm.hpp" #include "infinicore/ops/rms_norm.hpp"
#include "../../utils.hpp" #include "../../utils.hpp"
namespace infinicore::op { namespace infinicore::op {
INFINICORE_GRAPH_OP_DISPATCHERS_IMPL(RMSNorm);
common::OpDispatcher<RMSNorm::schema> &RMSNorm::dispatcher() { RMSNorm::RMSNorm(Tensor y, const Tensor &x, const Tensor &weight, float epsilon) {
static common::OpDispatcher<RMSNorm::schema> dispatcher_;
return dispatcher_;
};
void RMSNorm::execute(Tensor y, Tensor x, Tensor weight, float epsilon) {
INFINICORE_ASSERT_TENSORS_SAME_DEVICE(y, x, weight); INFINICORE_ASSERT_TENSORS_SAME_DEVICE(y, x, weight);
infinicore::context::setDevice(y->device()); INFINICORE_GRAPH_OP_DISPATCH(y->device().getType(), y, x, weight, epsilon);
dispatcher().lookup(y->device().getType())(y, x, weight, epsilon); }
void RMSNorm::execute(Tensor y, const Tensor &x, const Tensor &weight, float epsilon) {
INFINICORE_GRAPH_OP_RECORD_OR_RUN(RMSNorm, y, x, weight, epsilon);
} }
Tensor rms_norm(Tensor x, Tensor weight, float epsilon) { Tensor rms_norm(const Tensor &x, const Tensor &weight, float epsilon) {
auto y = Tensor::empty(x->shape(), x->dtype(), x->device()); auto y = Tensor::empty(x->shape(), x->dtype(), x->device());
rms_norm_(y, x, weight, epsilon); rms_norm_(y, x, weight, epsilon);
return y; return y;
} }
void rms_norm_(Tensor y, Tensor x, Tensor weight, float epsilon) { void rms_norm_(Tensor y, const Tensor &x, const Tensor &weight, float epsilon) {
RMSNorm::execute(y, x, weight, epsilon); RMSNorm::execute(y, x, weight, epsilon);
} }
......
#include "../../utils.hpp"
#include "infinicore/common/hash.hpp"
#include "infinicore/ops/common/cache.hpp"
#include "infinicore/ops/rms_norm.hpp" #include "infinicore/ops/rms_norm.hpp"
#include <infiniop.h>
namespace infinicore::op::rms_norm_impl::infiniop { #include "../infiniop_impl.hpp"
thread_local common::OpCache<size_t, infiniopRMSNormDescriptor_t> caches( namespace infinicore::op::rms_norm_impl::infiniop {
100, // capacity
[](infiniopRMSNormDescriptor_t &desc) {
if (desc != nullptr) {
INFINICORE_CHECK_ERROR(infiniopDestroyRMSNormDescriptor(desc));
desc = nullptr;
}
});
void calculate(Tensor y, Tensor x, Tensor weight, float epsilon) { INFINIOP_CACHABLE_DESCRIPTOR(Descriptor, RMSNorm, 100);
size_t seed = hash_combine(y, x, weight, epsilon);
auto device = context::getDevice(); struct PlannedMeta {
auto &cache = caches.getCache(device); std::shared_ptr<Descriptor> descriptor;
graph::GraphTensor workspace, y, x, weight;
};
auto desc_opt = cache.get(seed); void *plan(Tensor y, const Tensor &x, const Tensor &weight, float epsilon) {
infiniopRMSNormDescriptor_t desc = nullptr; size_t seed = hash_combine(y, x, weight, epsilon);
if (!desc_opt) { INFINIOP_CACHABLE_DESCRIPTOR_GET_OR_CREATE(
INFINICORE_CHECK_ERROR(infiniopCreateRMSNormDescriptor( Descriptor, descriptor, RMSNorm,
context::getInfiniopHandle(device), &desc, seed, y->desc(),
y->desc(), x->desc(), weight->desc(), epsilon)); x->desc(),
cache.put(seed, desc); weight->desc(),
} else { epsilon);
desc = *desc_opt;
} INFINIOP_WORKSPACE_TENSOR(workspace, RMSNorm, descriptor);
return new PlannedMeta{
descriptor,
graph::GraphTensor(workspace),
graph::GraphTensor(y),
graph::GraphTensor(x),
graph::GraphTensor(weight)};
}
size_t workspace_size = 0; void run(void *planned_meta) {
INFINICORE_CHECK_ERROR(infiniopGetRMSNormWorkspaceSize(desc, &workspace_size)); auto planned = reinterpret_cast<PlannedMeta *>(planned_meta);
std::shared_ptr<Memory> workspace = context::allocateMemory(workspace_size);
INFINICORE_CHECK_ERROR(
infiniopRMSNorm(
planned->descriptor->desc,
planned->workspace->data(),
planned->workspace->numel(),
planned->y->data(),
planned->x->data(),
planned->weight->data(),
context::getStream()));
}
INFINICORE_CHECK_ERROR(infiniopRMSNorm( void cleanup(void **planned_meta_ptr) {
desc, workspace->data(), workspace_size, delete *reinterpret_cast<PlannedMeta **>(planned_meta_ptr);
y->data(), x->data(), weight->data(), context::getStream())); *planned_meta_ptr = nullptr;
} }
static bool registered = []() { INFINICORE_GRAPH_OP_REGISTER_ALLDEVICE(RMSNorm, &plan, &run, &cleanup);
RMSNorm::dispatcher().registerAll(&calculate, false);
return true;
}();
} // namespace infinicore::op::rms_norm_impl::infiniop } // namespace infinicore::op::rms_norm_impl::infiniop
#include "infinicore/ops/rope.hpp" #include "infinicore/ops/rope.hpp"
#include "../../utils.hpp" #include "../../utils.hpp"
#include "infinicore/context/context.hpp"
#include <stdexcept>
namespace infinicore::op { namespace infinicore::op {
common::OpDispatcher<RoPE::schema> &RoPE::dispatcher() { INFINICORE_GRAPH_OP_DISPATCHERS_IMPL(RoPE);
static common::OpDispatcher<RoPE::schema> dispatcher_;
return dispatcher_;
};
void RoPE::execute(Tensor x_out, const Tensor &x, const Tensor &pos, const Tensor &sin_table, const Tensor &cos_table, infinicore::nn::RoPE::Algo algo) { RoPE::RoPE(Tensor x_out,
const Tensor &x,
const Tensor &pos,
const Tensor &sin_table,
const Tensor &cos_table,
infinicore::nn::RoPE::Algo algo) {
INFINICORE_ASSERT_TENSORS_SAME_DEVICE(x_out, x, pos, sin_table, cos_table); INFINICORE_ASSERT_TENSORS_SAME_DEVICE(x_out, x, pos, sin_table, cos_table);
infinicore::context::setDevice(x_out->device()); INFINICORE_GRAPH_OP_DISPATCH(x_out->device().getType(), x_out, x, pos, sin_table, cos_table, algo);
auto device_type = x_out->device().getType(); }
auto func = dispatcher().lookup(device_type);
if (func == nullptr) {
throw std::runtime_error("No RoPE implementation found for device type: " + std::to_string(static_cast<int>(device_type)));
}
func(x_out, x, pos, sin_table, cos_table, algo); void RoPE::execute(Tensor x_out,
const Tensor &x,
const Tensor &pos,
const Tensor &sin_table,
const Tensor &cos_table,
infinicore::nn::RoPE::Algo algo) {
INFINICORE_GRAPH_OP_RECORD_OR_RUN(RoPE, x_out, x, pos, sin_table, cos_table, algo);
} }
void rope_(Tensor x_out, const Tensor &x, const Tensor &pos, const Tensor &sin_table, const Tensor &cos_table, infinicore::nn::RoPE::Algo algo) { void rope_(Tensor x_out,
const Tensor &x,
const Tensor &pos,
const Tensor &sin_table,
const Tensor &cos_table,
infinicore::nn::RoPE::Algo algo) {
RoPE::execute(x_out, x, pos, sin_table, cos_table, algo); RoPE::execute(x_out, x, pos, sin_table, cos_table, algo);
} }
Tensor rope(const Tensor &x, const Tensor &pos, const Tensor &sin_table, const Tensor &cos_table, infinicore::nn::RoPE::Algo algo) { Tensor rope(const Tensor &x,
Shape shape = x->shape(); const Tensor &pos,
auto x_out = Tensor::empty(shape, x->dtype(), x->device()); const Tensor &sin_table,
const Tensor &cos_table,
infinicore::nn::RoPE::Algo algo) {
auto x_out = Tensor::empty(x->shape(), x->dtype(), x->device());
rope_(x_out, x, pos, sin_table, cos_table, algo); rope_(x_out, x, pos, sin_table, cos_table, algo);
return x_out; return x_out;
} }
......
#include "../../utils.hpp"
#include "infinicore/common/hash.hpp"
#include "infinicore/ops/common/cache.hpp"
#include "infinicore/ops/rope.hpp" #include "infinicore/ops/rope.hpp"
#include <infiniop.h>
#include "../infiniop_impl.hpp"
namespace infinicore::op::rope_impl::infiniop { namespace infinicore::op::rope_impl::infiniop {
thread_local common::OpCache<size_t, infiniopRoPEDescriptor_t> caches( INFINIOP_CACHABLE_DESCRIPTOR(Descriptor, RoPE, 100);
100, // capacity
[](infiniopRoPEDescriptor_t &desc) { struct PlannedMeta {
if (desc != nullptr) { std::shared_ptr<Descriptor> descriptor;
INFINICORE_CHECK_ERROR(infiniopDestroyRoPEDescriptor(desc)); graph::GraphTensor workspace;
desc = nullptr; graph::GraphTensor x_out;
} graph::GraphTensor x;
}); graph::GraphTensor pos;
graph::GraphTensor sin;
graph::GraphTensor cos;
};
void calculate(Tensor x_out, const Tensor &x, const Tensor &pos, const Tensor &sin_cache, const Tensor &cos_cache, infinicore::nn::RoPE::Algo algo) { static infiniopRoPEAlgo_t to_infiniop_algo(infinicore::nn::RoPE::Algo algo) {
// Convert infinicore::nn::RoPE::Algo to infiniopRoPEAlgo_t
infiniopRoPEAlgo_t infiniop_algo;
switch (algo) { switch (algo) {
case infinicore::nn::RoPE::Algo::GPT_J: case infinicore::nn::RoPE::Algo::GPT_J:
infiniop_algo = INFINIOP_ROPE_ALGO_GPT_J; return INFINIOP_ROPE_ALGO_GPT_J;
break;
case infinicore::nn::RoPE::Algo::GPT_NEOX: case infinicore::nn::RoPE::Algo::GPT_NEOX:
infiniop_algo = INFINIOP_ROPE_ALGO_GPT_NEOX; return INFINIOP_ROPE_ALGO_GPT_NEOX;
break;
default: default:
throw std::runtime_error("Unsupported RoPE algorithm: " + std::to_string(static_cast<int>(algo))); throw std::runtime_error("Unsupported RoPE algorithm");
} }
}
// Create hash key for descriptor caching void *plan(Tensor x_out,
size_t key = hash_combine(x_out, x, pos, sin_cache, cos_cache); const Tensor &x,
hash_combine(key, std::hash<int>()(static_cast<int>(infiniop_algo))); const Tensor &pos,
const Tensor &sin,
const Tensor &cos,
infinicore::nn::RoPE::Algo algo) {
auto infiniop_algo = to_infiniop_algo(algo);
size_t key = hash_combine(x_out, x, pos, sin, cos, static_cast<int>(infiniop_algo));
auto device = context::getDevice(); INFINIOP_CACHABLE_DESCRIPTOR_GET_OR_CREATE(
auto &cache = caches.getCache(device); Descriptor, descriptor, RoPE, key, x_out->desc(),
x->desc(),
pos->desc(),
sin->desc(),
cos->desc(),
infiniop_algo);
auto desc_opt = cache.get(key); INFINIOP_WORKSPACE_TENSOR(workspace, RoPE, descriptor);
infiniopRoPEDescriptor_t desc = nullptr; return new PlannedMeta{
descriptor,
graph::GraphTensor(workspace),
graph::GraphTensor(x_out),
graph::GraphTensor(x),
graph::GraphTensor(pos),
graph::GraphTensor(sin),
graph::GraphTensor(cos)};
}
if (!desc_opt) { void run(void *planned_meta) {
INFINICORE_CHECK_ERROR(infiniopCreateRoPEDescriptor( auto *p = reinterpret_cast<PlannedMeta *>(planned_meta);
context::getInfiniopHandle(device), &desc,
x_out->desc(), x->desc(),
pos->desc(), sin_cache->desc(), cos_cache->desc(),
infiniop_algo));
cache.put(key, desc);
} else {
desc = *desc_opt;
}
size_t workspace_size = 0; INFINICORE_CHECK_ERROR(
INFINICORE_CHECK_ERROR(infiniopGetRoPEWorkspaceSize(desc, &workspace_size)); infiniopRoPE(
std::shared_ptr<Memory> workspace = context::allocateMemory(workspace_size); p->descriptor->desc,
p->workspace->data(),
p->workspace->numel(),
p->x_out->data(),
p->x->data(),
p->pos->data(),
p->sin->data(),
p->cos->data(),
context::getStream()));
}
// InfiniOP reads from x and writes to x_out (handles copying internally) void cleanup(void **planned_meta_ptr) {
INFINICORE_CHECK_ERROR(infiniopRoPE( delete *reinterpret_cast<PlannedMeta **>(planned_meta_ptr);
desc, workspace->data(), workspace_size, *planned_meta_ptr = nullptr;
x_out->data(), x->data(), pos->data(),
sin_cache->data(), cos_cache->data(), context::getStream()));
} }
static bool registered = []() { INFINICORE_GRAPH_OP_REGISTER_ALLDEVICE(RoPE, &plan, &run, &cleanup);
RoPE::dispatcher().registerAll(&calculate, false);
return true;
}();
} // namespace infinicore::op::rope_impl::infiniop } // namespace infinicore::op::rope_impl::infiniop
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment