-
Notifications
You must be signed in to change notification settings - Fork 741
Expand file tree
/
Copy pathremote_cache_kv_ipc.h
More file actions
133 lines (122 loc) · 4.33 KB
/
remote_cache_kv_ipc.h
File metadata and controls
133 lines (122 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
// POSIX-only: mmap, shm_open
#if !defined(_WIN32)
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/msg.h>
#include <unistd.h>
#endif
#include "custom_ftok.h"
#include "driver_types.h"
#include "msg_utils.h"
#include "paddle/extension.h"
#include "paddle/phi/core/allocator.h"
#include "paddle/phi/core/dense_tensor.h"
struct RemoteCacheKvIpc {
struct save_cache_kv_complete_signal_layerwise_meta_data {
int32_t layer_id = -1;
void* shm_ptr = nullptr;
int shm_fd = -1;
save_cache_kv_complete_signal_layerwise_meta_data() {}
save_cache_kv_complete_signal_layerwise_meta_data(int32_t layer_id_,
void* shm_ptr_,
int shm_fd_)
: layer_id(layer_id_), shm_ptr(shm_ptr_), shm_fd(shm_fd_) {}
};
struct save_cache_kv_complete_signal_layerwise_meta_data_per_query {
int layer_id_;
int num_layers_;
bool inited = false;
struct msgdatakv msg_sed;
int msgid;
save_cache_kv_complete_signal_layerwise_meta_data_per_query() {}
void init(const int* seq_lens_encoder,
const int* seq_lens_decoder,
const int rank,
const int num_layers,
const int real_bsz) {
#if defined(_WIN32)
PD_THROW(
"RemoteCacheKvIpc::init is not supported on Windows "
"(POSIX IPC required).");
#else
layer_id_ = 0;
num_layers_ = num_layers;
msg_sed.mtype = 1;
int encoder_count = 0;
for (int i = 0; i < real_bsz; i++) {
if (seq_lens_encoder[i] > 0) {
msg_sed.mtext[3 * encoder_count + 2] = i;
msg_sed.mtext[3 * encoder_count + 3] = seq_lens_decoder[i];
msg_sed.mtext[3 * encoder_count + 4] = seq_lens_encoder[i];
encoder_count++;
}
}
msg_sed.mtext[0] = encoder_count;
if (!inited) {
// just init once
int msg_queue_id = 1024;
if (const char* msg_que_str_tmp =
std::getenv("INFERENCE_MSG_QUEUE_ID")) {
std::string msg_que_str(msg_que_str_tmp);
msg_queue_id = std::stoi(msg_que_str);
}
msg_queue_id = msg_queue_id << 4 + rank;
key_t key = custom_ftok("/opt/", msg_queue_id);
msgid = msgget(key, IPC_CREAT | 0666);
inited = true;
}
#endif
}
void CUDART_CB send_signal() {
#if defined(_WIN32)
PD_THROW(
"RemoteCacheKvIpc::send_signal is not supported on Windows "
"(POSIX IPC required).");
#else
if (inited) {
msg_sed.mtext[1] = layer_id_;
if ((msgsnd(msgid, &msg_sed, (MAX_BSZ * 3 + 2) * 4, 0)) == -1) {
printf("kv signal full msg buffer\n");
}
layer_id_ = (layer_id_ + 1);
assert(layer_id_ <= num_layers_);
}
#endif
}
};
static RemoteCacheKvIpc::save_cache_kv_complete_signal_layerwise_meta_data
kv_complete_signal_meta_data;
static RemoteCacheKvIpc::
save_cache_kv_complete_signal_layerwise_meta_data_per_query
kv_complete_signal_meta_data_per_query;
static void* kv_complete_signal_identity_ptr;
static bool kv_complete_signal_shmem_opened;
static RemoteCacheKvIpc::save_cache_kv_complete_signal_layerwise_meta_data
open_shm_and_get_complete_signal_meta_data(const int rank_id,
const int device_id,
const bool keep_pd_step_flag);
static void CUDART_CB
save_cache_kv_complete_signal_layerwise(void* meta_data);
static void CUDART_CB
save_cache_kv_complete_signal_layerwise_per_query(void* meta_data);
};