Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repo-sync-2024-01-30T11:49:42+0800 #250

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bazel/patches/zlib.patch
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ index 15ceebe..1d19c83 100644
include(CheckIncludeFile)
include(CheckCSourceCompiles)
-enable_testing()

check_include_file(sys/types.h HAVE_SYS_TYPES_H)
check_include_file(stdint.h HAVE_STDINT_H)
@@ -193,26 +192,3 @@ endif()
Expand Down
2 changes: 1 addition & 1 deletion yacl/crypto/primitives/dpf/mpfss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void MpfssSend(const std::shared_ptr<link::Context>& ctx,
}
ctx->SendAsync(
ctx->NextRank(),
ByteContainerView(send_msgs.data(), send_msgs.size() * sizeof(uint128_t)),
ByteContainerView(send_msgs.data(), send_msgs.size() * sizeof(uint64_t)),
"MpVole_msg");
}

Expand Down
2 changes: 2 additions & 0 deletions yacl/math/gadget.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ uint128_t inline GfMul(uint64_t a, uint128_t b) {
// f2k-Universal Hash
// ------------------------

// see difference between universal hash and collision-resistent hash functions:
// https://crypto.stackexchange.com/a/88247/61581
template <typename T>
T UniversalHash(T seed, absl::Span<const T> data) {
T ret = 0;
Expand Down
1 change: 1 addition & 0 deletions yacl/math/galois_field/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ yacl_cc_library(
"//yacl/io/msgpack:buffer",
"//yacl/io/msgpack:spec_traits",
"//yacl/utils:parallel",
"//yacl/utils/spi/sketch",
"@com_google_absl//absl/types:span",
],
)
Expand Down
142 changes: 3 additions & 139 deletions yacl/math/galois_field/gf_scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "yacl/io/msgpack/spec_traits.h"
#include "yacl/math/galois_field/gf_spi.h"
#include "yacl/utils/parallel.h"
#include "yacl/utils/spi/sketch/scalar_define.h"

namespace yacl::math {

Expand Down Expand Up @@ -85,28 +86,6 @@ class GFScalarSketch : public GaloisField {
virtual T DeserializeT(ByteContainerView buffer) const = 0;

private:
#define DefineBoolUnaryFunc(FuncName) \
Item FuncName(const Item& x) const override { \
if (x.IsArray()) { \
auto xsp = x.AsSpan<T>(); \
/* std::vector<bool> cannot write in parallel */ \
std::vector<uint8_t> res; \
res.resize(xsp.length()); \
yacl::parallel_for(0, xsp.length(), [&](int64_t beg, int64_t end) { \
for (int64_t i = beg; i < end; ++i) { \
res[i] = FuncName(xsp[i]); \
} \
}); \
/* convert std::vector<uint8_t> to std::vector<bool> */ \
std::vector<bool> bv; \
bv.resize(res.size()); \
std::copy(res.begin(), res.end(), bv.begin()); \
return Item::Take(std::move(bv)); \
} else { \
return FuncName(x.As<T>()); \
} \
}

// if x is scalar, returns bool
// if x is vectored, returns std::vector<bool>
DefineBoolUnaryFunc(IsIdentityOne);
Expand Down Expand Up @@ -151,39 +130,6 @@ class GFScalarSketch : public GaloisField {
// operations defined on set //
//================================//

#define DefineUnaryFunc(FuncName) \
Item FuncName(const Item& x) const override { \
using RES_T = decltype(FuncName(std::declval<const T>())); \
\
if (x.IsArray()) { \
auto xsp = x.AsSpan<T>(); \
std::vector<RES_T> res; \
res.resize(xsp.length()); \
yacl::parallel_for(0, xsp.length(), [&](int64_t beg, int64_t end) { \
for (int64_t i = beg; i < end; ++i) { \
res[i] = FuncName(xsp[i]); \
} \
}); \
return Item::Take(std::move(res)); \
} else { \
return FuncName(x.As<T>()); \
} \
}

#define DefineUnaryInplaceFunc(FuncName) \
void FuncName(Item* x) const override { \
if (x->IsArray()) { \
auto xsp = x->AsSpan<T>(); \
yacl::parallel_for(0, xsp.length(), [&](int64_t beg, int64_t end) { \
for (int64_t i = beg; i < end; ++i) { \
FuncName(&xsp[i]); \
} \
}); \
} else { \
FuncName(x->As<T*>()); \
} \
}

// get the additive inverse −a for all elements in set
DefineUnaryFunc(Neg);
DefineUnaryInplaceFunc(NegInplace);
Expand All @@ -192,65 +138,6 @@ class GFScalarSketch : public GaloisField {
DefineUnaryFunc(Inv);
DefineUnaryInplaceFunc(InvInplace);

#define DefineBinaryFunc(FuncName) \
Item FuncName(const Item& x, const Item& y) const override { \
using RES_T = \
decltype(FuncName(std::declval<const T>(), std::declval<const T>())); \
\
switch (x, y) { \
case OperandType::Scalar2Scalar: { \
return FuncName(x.As<T>(), y.As<T>()); \
} \
case OperandType::Vector2Vector: { \
auto xsp = x.AsSpan<T>(); \
auto ysp = y.AsSpan<T>(); \
YACL_ENFORCE_EQ( \
xsp.length(), ysp.length(), \
"operands must have the same length, x.len={}, y.len={}", \
xsp.length(), ysp.length()); \
\
std::vector<RES_T> res; \
res.resize(xsp.length()); \
yacl::parallel_for(0, xsp.length(), [&](int64_t beg, int64_t end) { \
for (int64_t i = beg; i < end; ++i) { \
res[i] = FuncName(xsp[i], ysp[i]); \
} \
}); \
return Item::Take(std::move(res)); \
} \
default: \
YACL_THROW("GFScalarSketch method [{}] doesn't support broadcast now", \
#FuncName); \
} \
}

#define DefineBinaryInplaceFunc(FuncName) \
void FuncName(yacl::Item* x, const yacl::Item& y) const override { \
switch (*x, y) { \
case OperandType::Scalar2Scalar: { \
FuncName(x->As<T*>(), y.As<T>()); \
return; \
} \
case OperandType::Vector2Vector: { \
auto xsp = x->AsSpan<T>(); \
auto ysp = y.AsSpan<T>(); \
YACL_ENFORCE_EQ( \
xsp.length(), ysp.length(), \
"operands must have the same length, x.len={}, y.len={}", \
xsp.length(), ysp.length()); \
yacl::parallel_for(0, xsp.length(), [&](int64_t beg, int64_t end) { \
for (int64_t i = beg; i < end; ++i) { \
FuncName(&xsp[i], ysp[i]); \
} \
}); \
return; \
} \
default: \
YACL_THROW("GFScalarSketch method [{}] doesn't support broadcast now", \
#FuncName); \
} \
}

DefineBinaryFunc(Add);
DefineBinaryInplaceFunc(AddInplace);

Expand All @@ -264,34 +151,11 @@ class GFScalarSketch : public GaloisField {
DefineBinaryInplaceFunc(DivInplace);

Item Pow(const Item& x, const MPInt& y) const override {
if (x.IsArray()) {
auto xsp = x.AsSpan<T>();
std::vector<T> res;
res.resize(xsp.length());
yacl::parallel_for(0, xsp.length(), [&](int64_t beg, int64_t end) {
for (int64_t i = beg; i < end; ++i) {
res[i] = Pow(xsp[i], y);
}
});
return Item::Take(std::move(res));
} else {
return Pow(x.As<T>(), y);
}
CallUnaryFunc(Pow, T, x, y);
}

void PowInplace(Item* x, const MPInt& y) const override {
if (x->IsArray()) {
auto xsp = x->AsSpan<T>();
yacl::parallel_for(0, xsp.length(), [&](int64_t beg, int64_t end) {
for (int64_t i = beg; i < end; ++i) {
PowInplace(&xsp[i], y);
}
});
return;
} else {
PowInplace(x->As<T*>(), y);
return;
}
CallUnaryInplaceFunc(PowInplace, T, x, y);
}

Item Random() const override { return RandomT(); }
Expand Down
2 changes: 1 addition & 1 deletion yacl/math/galois_field/mpint_field/mpint_field_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ TEST_F(MPIntFieldTest, VectorIoWorks) {

// subspan
auto item1 = Item::Take<MPInt>({0_mp, 1_mp, 2_mp, 3_mp});
auto item2 = item1.SubSpan<MPInt>(0, 1);
auto item2 = item1.SubItem<MPInt>(0, 1);
ASSERT_TRUE(item2.IsView());
ASSERT_FALSE(item2.IsReadOnly());
ASSERT_TRUE(item2.WrappedTypeIs<absl::Span<MPInt>>());
Expand Down
2 changes: 1 addition & 1 deletion yacl/utils/spi/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 Ant Group Co., Ltd.
# 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.
Expand Down
2 changes: 1 addition & 1 deletion yacl/utils/spi/argument/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 Ant Group Co., Ltd.
# 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.
Expand Down
Loading
Loading