关于隐私:隐语纵向联邦-SecureBoost-Benchmark白皮书

4次阅读

共计 6452 个字符,预计需要花费 17 分钟才能阅读完成。

“隐语”是开源的可信隐衷计算框架,内置 MPC、TEE、同态等多种密态计算虚构设施供灵便抉择,提供丰盛的联邦学习算法和差分隐衷机制。

开源我的项目:
https://github.com/secretflow
https://gitee.com/secretflow

导语:

在数据迷信比赛中经典算法 XGB 备受关注。但有小伙伴放心,在纵向联邦中 XGB 是否足够高效,平安和效率是否能够兼得,隐衷计算是否耗时太长导致模型迭代迟缓?应用隐语中联邦算法 SecureBoost 的高效实现, 炼丹效率轻松 狂飙 10 倍!

隐语近期开源了基于纵向联邦算法 SecureBoost 算法,并进行了高性能实现。与机密分享计划的 SS-XGB 相比,SecureBoost 性能具备更好的体现,不过因为是非 MPC 算法,在平安方面低于 SS-XGB。

隐语 SecureBoost(下文简称:隐语 SGB)利用了平安底座和多方联结计算的分布式架构, 极大进步了密态计算效率和灵活性。只须要通过简略配置, 隐语 SGB 即可切换同态加密协议, 例如 Paillier 和 OU, 满足不同场景下的平安和计算效率的需要。

本文将介绍隐语 SGB 的具体测试环境、步骤和数据, 不便您理解协定的应用办法和性能数据, 从而更好地理解隐语 SGB, 满足您的业务需要。让咱们一起来领略隐语 SGB 的魅力吧!

测试方法和步骤:

一、测试机型

  • Python:3.8
  • pip: >= 19.3
  • OS: CentOS 7
  • CPU/Memory: 举荐最低配置是 8C16G
  • 硬盘:500G

二、装置 conda

应用 conda 治理 python 环境,如果机器没有 conda 须要先装置。

#sudo apt-get install wget
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

#装置
bash Miniconda3-latest-Linux-x86_64.sh

# 始终按回车而后输出 yes
please answer 'yes' or 'no':
>>> yes

# 抉择装置门路, 文件名前加点号示意暗藏文件
Miniconda3 will now be installed into this location:
>>> ~/.miniconda3

# 增加配置信息到 ~/.bashrc 文件
Do you wish the installer to initialize Miniconda3 by running conda init? [yes|no]
[no] >>> yes

#运行配置信息文件或重启电脑
source ~/.bashrc

#测试是否装置胜利,有显示版本号示意装置胜利
conda --version

三、装置 secretflow

conda create -n sf-benchmark python=3.8

conda activate sf-benchmark

pip install -U secretflow

四、数据要求

两方数据规模:

  • alice 方:100 万 50 维
  • bob 方:100 万 50 维

三方数据规模:

  • alice 方:100 万 34 维
  • bob 方:100 万 33 维
  • carol:100 万 33 维

五、Benchmark 脚本

import logging
import socket
import sys
import time

import spu
from sklearn.metrics import mean_squared_error, roc_auc_score

import secretflow as sf
from secretflow.data import FedNdarray, PartitionWay
from secretflow.device.driver import reveal, wait
from secretflow.ml.boost.sgb_v import Sgb
from secretflow.utils.simulation.datasets import create_df
from secretflow.data.vertical import read_csv as v_read_csv


# init log
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.info("test")

_parties = {
    # you may change the addresses
    # 将 alice、bob、carol 的 ip 替换为理论 ip
    'alice': {'address': '192.168.0.1:23041'},
    'bob': {'address': '192.168.0.2:23042'},
    'carol': {'address': '192.168.0.3:23043'},

}


def setup_sf(party, alice_ip, bob_ip, carol_ip):

    cluster_conf = {
        'parties': _parties,
        'self_party': party,
    }

    # init cluster
    _system_config = {'lineage_pinning_enabled': False}
    sf.init(
        address='local',
        num_cpus=8,
        log_to_driver=True,
        cluster_config=cluster_conf,
        exit_on_failure_cross_silo_sending=True,
        _system_config=_system_config,
        _memory=5 * 1024 * 1024 * 1024,
        cross_silo_messages_max_size_in_bytes = 2 * 1024 * 1024 * 1024 -1,
        object_store_memory=5 * 1024 * 1024 * 1024,
    )
    # SPU settings
    cluster_def = {
        'nodes': [{'party': 'alice', 'id': 'local:0', 'address': alice_ip},
            {'party': 'bob', 'id': 'local:1', 'address': bob_ip},
            {'party': 'carol', 'id': 'local:1', 'address': carol_ip},
        ],
        'runtime_config': {
            # SEMI2K support 2/3 PC, ABY3 only support 3PC, CHEETAH only support 2PC.
            # pls pay attention to size of nodes above. nodes size need match to PC setting.
            'protocol': spu.spu_pb2.ABY3,
            'field': spu.spu_pb2.FM64,
        },
    }

    # HEU settings
    heu_config = {'sk_keeper': {'party': 'alice'},
        'evaluators': [{'party': 'bob'},{'party': 'carol'}],
        'mode': 'PHEU',  # 这里批改同态加密相干配置
        'he_parameters': {
            'schema': 'paillier',
            'key_pair': {
                'generate': {'bit_size': 2048,},
            },
        },
        'encoding': {
            'cleartext_type': 'DT_I32',
            'encoder': "IntegerEncoder",
            'encoder_args': {"scale": 1},
        },
    }
    return cluster_def, heu_config


class SGB_benchmark:
    def __init__(self, cluster_def, heu_config):
        self.alice = sf.PYU('alice')
        self.bob = sf.PYU('bob')
        self.carol = sf.PYU('carol')
        self.heu = sf.HEU(heu_config, cluster_def['runtime_config']['field'])

    def run_sgb(self, test_name, v_data, label_data, y, logistic, subsample, colsample):
        sgb = Sgb(self.heu)
        start = time.time()
        params = {
            'num_boost_round': 5,
            'max_depth': 5,
            'sketch_eps': 0.08,
            'objective': 'logistic' if logistic else 'linear',
        'reg_lambda': 0.3,
        'subsample': subsample,
        'colsample_by_tree': colsample,
        }
        model = sgb.train(params, v_data, label_data)
    #    reveal(model.weights[-1])
        print(f"{test_name} train time: {time.time() - start}")
        start = time.time()
        yhat = model.predict(v_data)
        yhat = reveal(yhat)
        print(f"{test_name} predict time: {time.time() - start}")
        if logistic:
        print(f"{test_name} auc: {roc_auc_score(y, yhat)}")
else:
        print(f"{test_name} mse: {mean_squared_error(y, yhat)}")

        fed_yhat = model.predict(v_data, self.alice)
        assert len(fed_yhat.partitions) == 1 and self.alice in fed_yhat.partitions
        yhat = reveal(fed_yhat.partitions[self.alice])
        assert yhat.shape[0] == y.shape[0], f"{yhat.shape} == {y.shape}"
        if logistic:
        print(f"{test_name} auc: {roc_auc_score(y, yhat)}")
else:
        print(f"{test_name} mse: {mean_squared_error(y, yhat)}")

        def test_on_linear(self, sample_num, total_num):
        """sample_num: int. this number * 10000 = sample number in dataset."""
        io_start = time.perf_counter()
        common_path = "/root/sf-benchmark/data/{}w_{}d_3pc/independent_linear.".format(sample_num, total_num)
        vdf = v_read_csv({self.alice: common_path + "1.csv", self.bob: common_path + "2.csv", self.carol: common_path + "3.csv"},
        keys='id',
        drop_keys='id',
        )
        # split y out of dataset,
        # <<< !!! >>> change 'y' if label column name is not y in dataset.
        label_data = vdf["y"]
        # v_data remains all features.
        v_data = vdf.drop(columns="y")
        # <<< !!! >>> change bob if y not belong to bob.
        y = reveal(label_data.partitions[self.alice].data)
        wait([p.data for p in v_data.partitions.values()])
        io_end = time.perf_counter()
        print("io takes time", io_end - io_start)
        self.run_sgb("independent_linear", v_data, label_data, y, True, 1, 1)


        def run_test(party):
        cluster_def, heu_config = setup_sf(party, _parties['alice'], _parties['bob'], _parties['carol'])
        test_suite = SGB_benchmark(cluster_def, heu_config)
        test_suite.test_on_linear(100, 100)

        sf.shutdown()


        if __name__ == '__main__':
        import argparse

        parser = argparse.ArgumentParser(prog='sgb benchmark remote')
        parser.add_argument('party')
        args = parser.parse_args()
        run_test(args.party)

将脚本下载到测试机上,可命名为 sgb_benchmark.py,alice、bob、carol 三方共用 1 个脚本。

2 方 SGB 启动形式如下:

alice 方:python sgb_benchmark.py alice
bob 方:python sgb_benchmark.py bob

3 方 SGB 启动形式如下:

alice 方:python sgb_benchmark.py alice
bob 方:python sgb_benchmark.py bob
carol 方:python sgb_benchmark.py carol

SGB Benchmark 报告

解读:

本次 benchmark 的数据为百万百维。咱们在两组网络参数下进行试验。算法参数中的 schema 也有 ’paillier’ 和 ’ou’ 两种。本次试验训练的 XGB 树的数量为 5,深度为 5,特色分桶数量为 13,进行二分类工作。咱们别离在两方和三方场景下进行上述试验。两方状况下,alice 和 bob 各领有其中 50 维的数据。三方状况下,alice,bob 和 carol 别离领有(34,33,33)维数据。

整体来讲三方计算效率更高,体现了多方之间并行计算的劣势。

LAN 的试验模仿本地局域网的环境下的性能和 WAN 的试验模仿在低提早互联网环境下的性能。对于同态加密计划来说,计算应该是瓶颈,计算耗时对于网络提早的敏感性比机密分享计划要低得多,在 LAN 模式和 WAN 模式下计算耗时相差并不微小。

在设置 HEU 所用协定时,咱们别离配置了 paillier 和 ou 两种协定计算作为比照(密钥长度默认为 2048bit)。Paillier 和 OU 均为 IND-CPA 平安,语义平安(Semantic Security)的加密零碎,然而基于不同的艰难假如。在加密性能和密态加法的性能上 OU 要优于 Paillier,密文大小也是 Paillier 的一半,对于 OU 更具体的介绍参见下方链接。总体来讲,OU 相比于 Paillier 在隐语 SGB 上提供了 3~4 倍的计算性能减速并把内存需要升高一半。

参考资料:

Okamoto-Uchiyama 算法介绍

https://www.secretflow.org.cn/docs/heu/zh_CN/getting_started/…

🏠 隐语社区:
https://github.com/secretflow
https://gitee.com/secretflow
https://www.secretflow.org.cn(官网)

👇欢送关注:
公众号:隐语的小剧场
B 站:隐语 secretflow
邮箱:secretflow-contact@service.alipay.com

正文完
 0