Skip to content

Commit 21be9ca

Browse files
authored
rpc : fix load/store misaligned addresses (ggml-org#7948)
1 parent 006167a commit 21be9ca

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

ggml-rpc.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ struct rpc_tensor {
7373
uint64_t view_offs;
7474
uint64_t data;
7575
char name[GGML_MAX_NAME];
76+
77+
char padding[4];
7678
};
7779
#pragma pack(pop)
7880

81+
static_assert(sizeof(rpc_tensor) % 8 == 0, "rpc_tensor size must be multiple of 8");
82+
7983
// RPC commands
8084
enum rpc_cmd {
8185
ALLOC_BUFFER = 0,
@@ -599,9 +603,8 @@ static void serialize_graph(const ggml_cgraph * cgraph, std::vector<uint8_t> & o
599603
int output_size = sizeof(uint32_t) + n_nodes * sizeof(uint64_t) + sizeof(uint32_t) + n_tensors * sizeof(rpc_tensor);
600604
output.resize(output_size, 0);
601605
memcpy(output.data(), &n_nodes, sizeof(n_nodes));
602-
uint64_t * out_nodes = (uint64_t *)(output.data() + sizeof(n_nodes));
603606
for (uint32_t i = 0; i < n_nodes; i++) {
604-
out_nodes[i] = reinterpret_cast<uint64_t>(cgraph->nodes[i]);
607+
memcpy(output.data() + sizeof(n_nodes) + i * sizeof(uint64_t), &cgraph->nodes[i], sizeof(uint64_t));
605608
}
606609
uint32_t * out_ntensors = (uint32_t *)(output.data() + sizeof(n_nodes) + n_nodes * sizeof(uint64_t));
607610
*out_ntensors = n_tensors;
@@ -1036,7 +1039,9 @@ bool rpc_server::graph_compute(const std::vector<uint8_t> & input, std::vector<u
10361039
}
10371040
std::unordered_map<uint64_t, ggml_tensor*> tensor_map;
10381041
for (uint32_t i = 0; i < n_nodes; i++) {
1039-
graph->nodes[i] = create_node(nodes[i], ctx, tensor_ptrs, tensor_map);
1042+
int64_t id;
1043+
memcpy(&id, &nodes[i], sizeof(id));
1044+
graph->nodes[i] = create_node(id, ctx, tensor_ptrs, tensor_map);
10401045
}
10411046
ggml_status status = ggml_backend_graph_compute(backend, graph);
10421047
// output serialization format: | status (1 byte) |

0 commit comments

Comments
 (0)