SecretFlow
SecretFlow运行要求
- Python >= 3.8
操作系统:
• CentOS 7 • Anolis8 • Ubuntu 18.04/20.04 • macOS 11.1+ • WSL2
资源:≥ 8核16GB
SecretFlow安装包
- secretflow
• 包含secretflow所有的requirements • 体积较大
- secretflow-lite
• 仅包含基础功能(不包含深度学习等依赖库) • 体积较小
SecretFlow安装方式
方式一:docker镜像
docker hub: docker run -it secretflow/secretflow-anolis8:latest docker run –it secretflow/secretflow-lite-anolis8:latest 阿里云: docker run -it secretflow-registry.cn-hangzhou.cr.aliyuncs.com/secretflow-anolis8:latest docker run –it secretflow-registry.cn-hangzhou.cr.aliyuncs.com /secretflow-lite-anolis8:latest
方式二:pypi
pip install –U secretflow pip install –U secretflow-lite 1.要求pip >= 19.3 2.建议使用conda管理python环境
方式三:源码
下载源码并建立Python虚拟环境
git clone https://github.com/secretflow/secretflow.git cd secretflow conda create -n secretflow python==3.8 conda activate secretflow
安装secretflow
# 涉及到C++编译,建议使用镜像secretflow/release-ci:latest python setup.py bdist_wheel pip install dist/*.whl 1.要求pip >= 19.3 2.建议使用conda管理python环境
SecretFlow部署
本地docker镜像中心
后台运行隐语docker容器
查看容器进程
进入隐语容器
仿真模式
单机仿真
address='local':表示启动一个本地的Ray节点
# local.py
import secretflow as sf
sf.init(parties=['alice', 'bob'], address='local')
alice = sf.PYU('alice')
bob = sf.PYU('bob')
a = alice(lambda x : x + 1)(2)
print(a)
b = bob(lambda x : x - 1)(2)
print(b)
集群仿真
部署两个节点,分别模拟alice、bob两个参与方
在第一台机器上部署Ray主节点,模拟参与方alice
ray start --head --node-ip-address="127.0.0.1" --port="8001" --resources='{"alice": 3}' --include-dashboard=False --disable-usage-stats
在第二台机器上部署Ray从节点,模拟参与方bob
ray start --address="172.17.0.3:8001" --resources=' {"bob": 3} ' --disable-usage-stats
PPT错误
详见手册:
https://www.secretflow.org.cn/zh-CN/docs/secretflow/v1.4.0b0/getting_started/deployment
address='172.17.0.3:8001'
# local2.py
import secretflow as sf
sf.init(parties=['alice', 'bob'], address='172.17.0.3:8001')
alice = sf.PYU('alice')
bob = sf.PYU('bob')
a = alice(lambda x : x + 1)(2)
print(a)
b = bob(lambda x : x - 1)(2)
print(b)
创建密态设备SPU
# test_spu.py
import spu
import secretflow as sf
# Use ray head adress please.
sf.init(parties=['alice', 'bob'], address='172.17.0.3:8001')
cluster_def={
'nodes': [
{
'party': 'alice',
# Please choose an unused port.
'address': 'ip:port of alice',
'listen_addr': '172.17.0.3:9001'
},
{
'party': 'bob',
# Please choose an unused port.
'address': 'ip:port of bob',
'listen_addr': '172.17.0.4:9001'
},
],
'runtime_config': {
'protocol': spu.spu_pb2.SEMI2K,
'field': spu.spu_pb2.FM128,
'sigmoid_mode': spu.spu_pb2.RuntimeConfig.SIGMOID_REAL,
}
}
spu = sf.SPU(cluster_def=cluster_def)
print(spu)
仿真集群通信网络
生产模式
部署两个节点,分别模拟alice、bob两个参与方
在第一台机器上部署Ray主节点,模拟参与方alice
ray start –-head \ --node-ip-address="{ip}" --port="{port} " \ --resources='{"alice": 16} ' \ --include-dashboard=False \ --disable-usage-stats
在第二台机器上部署Ray主节点,模拟参与方bob
ray start –-head \ --node-ip-address="{ip}" --port="{port} " \ --resources='{"bob": 16} ' \ --include-dashboard=False \ --disable-usage-stats
两台机器上分别执行代码
- alice执行python代码
cliuster_config中的address分别填写alice/bob的通信地址(注意不要和Ray的端口冲突)
import secretflow as sf
cluster_config ={
"parties": {
"alice": {
"address": "ip:port of alice"},
"bob": {
"address": "ip:port of bob"},
},
"self_party": "alice",
}
sf.init(address='{Ray head node address of alice}',
cluster_config=cluster_config)
- bob执行python代码
cliuster_config中的address分别填写alice/bob的通信地址(注意不要和Ray的端口冲突)
import secretflow as sf
cluster_config ={
"parties": {
"alice": {
"address": "ip:port of alice"},
"bob": {
"address": "ip:port of bob"},
},
"self_party": "bob",
}
sf.init(address='{Ray head node address of bob}',
cluster_config=cluster_config)
创建密态设备SPU
cluster_def注意不要和Ray通信端口、SecretFlow通信端口冲突
import spu
cluster_def = {
'nodes': [{
'party': 'alice','address': '{ip:port of alice}'},
{
'party': 'bob','address': '{ip:port of bob}'}],
'runtime_config': {
'protocol': spu.spu_pb2.SEMI2K,
'field': spu.spu_pb2.FM128,
'sigmoid_mode': spu.spu_pb2.RuntimeConfig.SIGMOID_REAL
}
}
spu = sf.SPU(cluster_def=cluster_def)
生产模式的通信网络
使用KUSCIA解决多端口问题
KUSCIA定位:屏蔽不同机构间基础设施的差异,为跨机构协作提供丰富且 可靠的资源管理和任务调度能力。
SPU、Ray通信多端口 》 统一网络基础设施 》 KUSCIA
SecretNote
https://github.com/secretflow/secretnote
类Jupyter Notebook,与Notebook一致的使用体验;
单操作界面,多节点代码自动执行;
多节点代码运行跟踪。