|
| 1 | +#include "infinicore/ops/ernie45_rope.hpp" |
| 2 | + |
| 3 | +#include "../infiniop_impl.hpp" |
| 4 | +#include "infiniop/ops/ernie45_rope.h" |
| 5 | + |
| 6 | +namespace infinicore::op::ernie45_rope_impl::infiniop { |
| 7 | + |
| 8 | +namespace mrope { |
| 9 | +INFINIOP_CACHABLE_DESCRIPTOR(MropeDescriptor, Ernie45Mrope, 100); |
| 10 | +} // namespace mrope |
| 11 | + |
| 12 | +namespace vision { |
| 13 | +INFINIOP_CACHABLE_DESCRIPTOR(VisionDescriptor, Ernie45VisionRope, 100); |
| 14 | +} // namespace vision |
| 15 | + |
| 16 | +namespace mrope_op { |
| 17 | + |
| 18 | +struct MropePlannedMeta { |
| 19 | + std::shared_ptr<mrope::MropeDescriptor> descriptor; |
| 20 | + graph::GraphTensor workspace; |
| 21 | + graph::GraphTensor q; |
| 22 | + graph::GraphTensor k; |
| 23 | + graph::GraphTensor positions; |
| 24 | +}; |
| 25 | + |
| 26 | +void *plan_mrope(Tensor q, |
| 27 | + Tensor k, |
| 28 | + const Tensor &positions, |
| 29 | + double rope_theta, |
| 30 | + size_t section_h, |
| 31 | + size_t section_w, |
| 32 | + size_t section_t) { |
| 33 | + size_t key = hash_combine(q, k, positions, rope_theta, section_h, section_w, section_t); |
| 34 | + std::shared_ptr<mrope::MropeDescriptor> descriptor; |
| 35 | + { |
| 36 | + auto device__ = context::getDevice(); |
| 37 | + auto &cache__ = mrope::caches.getCache(device__); |
| 38 | + descriptor = cache__.get(key).value_or(nullptr); |
| 39 | + if (!descriptor) { |
| 40 | + descriptor = std::make_shared<mrope::MropeDescriptor>(nullptr); |
| 41 | + INFINICORE_CHECK_ERROR(infiniopCreateErnie45MropeDescriptor( |
| 42 | + context::getInfiniopHandle(device__), |
| 43 | + &descriptor->desc, |
| 44 | + q->desc(), k->desc(), positions->desc(), rope_theta, section_h, section_w, section_t)); |
| 45 | + cache__.put(key, descriptor); |
| 46 | + } |
| 47 | + } |
| 48 | + INFINIOP_WORKSPACE_TENSOR(workspace, Ernie45Mrope, descriptor); |
| 49 | + return new MropePlannedMeta{ |
| 50 | + descriptor, |
| 51 | + graph::GraphTensor(workspace), |
| 52 | + graph::GraphTensor(q), |
| 53 | + graph::GraphTensor(k), |
| 54 | + graph::GraphTensor(positions)}; |
| 55 | +} |
| 56 | + |
| 57 | +void run_mrope(void *planned_meta) { |
| 58 | + auto *p = reinterpret_cast<MropePlannedMeta *>(planned_meta); |
| 59 | + INFINICORE_CHECK_ERROR( |
| 60 | + infiniopErnie45Mrope( |
| 61 | + p->descriptor->desc, |
| 62 | + p->workspace->data(), |
| 63 | + p->workspace->numel(), |
| 64 | + p->q->data(), |
| 65 | + p->k->data(), |
| 66 | + p->positions->data(), |
| 67 | + context::getStream())); |
| 68 | +} |
| 69 | + |
| 70 | +void cleanup_mrope(void **planned_meta_ptr) { |
| 71 | + delete *reinterpret_cast<MropePlannedMeta **>(planned_meta_ptr); |
| 72 | + *planned_meta_ptr = nullptr; |
| 73 | +} |
| 74 | + |
| 75 | +INFINICORE_GRAPH_OP_REGISTER_ALLDEVICE(Ernie45MRoPE, &plan_mrope, &run_mrope, &cleanup_mrope); |
| 76 | + |
| 77 | +} // namespace mrope_op |
| 78 | + |
| 79 | +namespace vision_op { |
| 80 | + |
| 81 | +struct VisionPlannedMeta { |
| 82 | + std::shared_ptr<vision::VisionDescriptor> descriptor; |
| 83 | + graph::GraphTensor workspace; |
| 84 | + graph::GraphTensor q; |
| 85 | + graph::GraphTensor k; |
| 86 | + graph::GraphTensor positions; |
| 87 | +}; |
| 88 | + |
| 89 | +void *plan_vision(Tensor q, |
| 90 | + Tensor k, |
| 91 | + const Tensor &positions, |
| 92 | + double rope_theta) { |
| 93 | + size_t key = hash_combine(q, k, positions, rope_theta); |
| 94 | + std::shared_ptr<vision::VisionDescriptor> descriptor; |
| 95 | + { |
| 96 | + auto device__ = context::getDevice(); |
| 97 | + auto &cache__ = vision::caches.getCache(device__); |
| 98 | + descriptor = cache__.get(key).value_or(nullptr); |
| 99 | + if (!descriptor) { |
| 100 | + descriptor = std::make_shared<vision::VisionDescriptor>(nullptr); |
| 101 | + INFINICORE_CHECK_ERROR(infiniopCreateErnie45VisionRopeDescriptor( |
| 102 | + context::getInfiniopHandle(device__), |
| 103 | + &descriptor->desc, |
| 104 | + q->desc(), k->desc(), positions->desc(), rope_theta)); |
| 105 | + cache__.put(key, descriptor); |
| 106 | + } |
| 107 | + } |
| 108 | + INFINIOP_WORKSPACE_TENSOR(workspace, Ernie45VisionRope, descriptor); |
| 109 | + return new VisionPlannedMeta{ |
| 110 | + descriptor, |
| 111 | + graph::GraphTensor(workspace), |
| 112 | + graph::GraphTensor(q), |
| 113 | + graph::GraphTensor(k), |
| 114 | + graph::GraphTensor(positions)}; |
| 115 | +} |
| 116 | + |
| 117 | +void run_vision(void *planned_meta) { |
| 118 | + auto *p = reinterpret_cast<VisionPlannedMeta *>(planned_meta); |
| 119 | + INFINICORE_CHECK_ERROR( |
| 120 | + infiniopErnie45VisionRope( |
| 121 | + p->descriptor->desc, |
| 122 | + p->workspace->data(), |
| 123 | + p->workspace->numel(), |
| 124 | + p->q->data(), |
| 125 | + p->k->data(), |
| 126 | + p->positions->data(), |
| 127 | + context::getStream())); |
| 128 | +} |
| 129 | + |
| 130 | +void cleanup_vision(void **planned_meta_ptr) { |
| 131 | + delete *reinterpret_cast<VisionPlannedMeta **>(planned_meta_ptr); |
| 132 | + *planned_meta_ptr = nullptr; |
| 133 | +} |
| 134 | + |
| 135 | +INFINICORE_GRAPH_OP_REGISTER_ALLDEVICE(Ernie45VisionRoPE, &plan_vision, &run_vision, &cleanup_vision); |
| 136 | + |
| 137 | +} // namespace vision_op |
| 138 | + |
| 139 | +} // namespace infinicore::op::ernie45_rope_impl::infiniop |
0 commit comments