-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermute.cc
More file actions
173 lines (145 loc) · 5.22 KB
/
permute.cc
File metadata and controls
173 lines (145 loc) · 5.22 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2023 Ant Group Co., Ltd.
//
// 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.
#include "libspu/mpc/cheetah/permute.h"
#include <cstdio>
#include <limits>
#include "yacl/crypto/rand/rand.h"
#include "libspu/mpc/cheetah/nonlinear/osn_prot.h"
#include "libspu/mpc/cheetah/tiled_dispatch.h"
#include "libspu/mpc/cheetah/type.h"
#include "libspu/mpc/utils/permute.h"
#include "libspu/mpc/utils/ring_ops.h"
namespace spu::mpc::cheetah {
NdArrayRef RandPermM::proc(KernelEvalContext* ctx, const Shape& shape) const {
SPU_ENFORCE_LE(shape.numel(),
static_cast<int64_t>(std::numeric_limits<uint32_t>::max()));
auto type = makeType<PShrTy>();
NdArrayRef out(type, shape);
int32_t* perm = out.data<int32_t>();
std::iota(perm, perm + shape.numel(), 0);
uint64_t ctr = 0;
yacl::crypto::ReplayShuffle(perm, perm + shape.numel(),
yacl::crypto::RandSeed(/*fast*/ true), &ctr);
return out.as(type);
}
namespace {
NdArrayRef ApplyPerm(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm, bool is_inv) {
SPU_ENFORCE_EQ(in.shape(), perm.shape());
const auto numel = in.numel();
// We need a compact mem for the permutation
absl::Span<const int32_t> perm_span;
std::vector<int32_t> perm_tmp;
if (perm.isCompact()) {
perm_span = absl::MakeConstSpan(perm.data<int32_t>(), numel);
} else {
perm_tmp.resize(numel);
NdArrayView<int32_t> xperm(perm);
for (int64_t i = 0; i < numel; ++i) {
perm_tmp[i] = xperm[i];
}
perm_span = absl::MakeConstSpan(perm_tmp);
}
OSNProtocol::Meta osn_meta;
osn_meta.is_inverse_perm = is_inv;
osn_meta.is_arithmetic = !in.eltype().isa<BShare>();
osn_meta.payload_type = in.eltype().as<Ring2k>()->field();
osn_meta.payload_width = SizeOf(osn_meta.payload_type) * 8;
if (osn_meta.is_arithmetic) {
osn_meta.payload_width = in.eltype().as<AShare>()->nbits();
} else {
osn_meta.payload_width = in.eltype().as<BShare>()->nbits();
}
osn_meta.numel = numel;
const int rank = ctx->getState<Communicator>()->getRank();
if (rank == 0 && osn_meta.is_arithmetic) {
printf("PermA %lld %d\n", in.numel(), osn_meta.payload_width);
}
if (rank == 0 && !osn_meta.is_arithmetic) {
printf("PermB %lld %d\n", in.numel(), osn_meta.payload_width);
}
OSNProtocol osn(osn_meta, perm_span);
auto out = DispatchUnaryFunc(
ctx, in,
[&](const NdArrayRef& input,
const std::shared_ptr<BasicOTProtocols>& ot) {
// (P_b)^-1 * (P_a)^-1 * P_a * P_b * v
if (is_inv) {
if (rank == 1) {
auto tmp = osn.Send(in, ot);
return osn.Recv(tmp, ot);
} else {
auto tmp = osn.Recv(in, ot);
return osn.Send(tmp, ot);
}
}
if (rank == 0) {
auto tmp = osn.Send(in, ot);
return osn.Recv(tmp, ot);
} else {
auto tmp = osn.Recv(in, ot);
return osn.Send(tmp, ot);
}
},
/*parallel*/ false);
return out;
}
} // namespace
NdArrayRef PermAM::proc(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm) const {
return ApplyPerm(ctx, in, perm, false);
}
NdArrayRef InvPermAM::proc(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm) const {
return ApplyPerm(ctx, in, perm, true);
}
NdArrayRef PermBM::proc(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm) const {
return ApplyPerm(ctx, in, perm, false);
}
NdArrayRef InvPermBM::proc(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm) const {
return ApplyPerm(ctx, in, perm, true);
}
NdArrayRef PermAP::proc(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm) const {
if (in.numel() > 0) {
return applyPerm(in, perm);
}
return NdArrayRef(in.eltype(), in.shape());
}
NdArrayRef InvPermAP::proc(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm) const {
if (in.numel() > 0) {
return applyInvPerm(in, perm);
}
return NdArrayRef(in.eltype(), in.shape());
}
NdArrayRef PermBP::proc(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm) const {
if (in.numel() > 0) {
return applyPerm(in, perm);
}
printf("PermBP ...\n");
return NdArrayRef(in.eltype(), in.shape());
}
NdArrayRef InvPermBP::proc(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm) const {
if (in.numel() > 0) {
return applyInvPerm(in, perm);
}
printf("InvPermBP ...\n");
return NdArrayRef(in.eltype(), in.shape());
}
} // namespace spu::mpc::cheetah