SecretFlow的安装
说明一下,本人使用的是WSL2安装的
- conda的安装
在命令行输入'conda',查看环境是否安装好:#下载anaconda wget https://mirrors.bfsu.edu.cn/anaconda/archive/Anaconda3-2022.10-Linux-x86_64.sh --no-check-certificate #安装anaconda bash Anaconda3-2022.10-Linux-x86_64.sh source ~/.bashrc
- 创建conda虚拟环境
conda create -n secretflow python=3.9
3.使用虚拟环境安装SecretFlow
#激活虚拟环境
conda activate secretflow
#安装secretflow(建议安装secretflow-lite,secretflow太大了)
pip install -U secretflow-lite -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
验证安装成功:
4.仿真模式
单机仿真
import secretflow as sf
sf.init(parties=['alice', 'bob'], address='local')
alice = sf.PYU('alice')
bob = sf.PYU('bob')
print(alice(lambda x:x+1)(2))
print(bob(lambda x:x-1)(2))
集群仿真
#启动主节点
ray start --head --node-ip-address="127.0.0.1" --port="8080" --resources='{"alice": 16}' --include-dashboard=False --disable-usage-stats
#启动从节点
ray start --address="127.0.0.1:8080" --resources='{"bob": 16}' --disable-usage-stats
import secretflow as sf
sf.init(parties=['alice', 'bob'], address='127.0.0.1:8080')
alice = sf.PYU('alice')
bob = sf.PYU('bob')
print(alice(lambda x:x)(2))
print(bob(lambda x:x)(2))
集群模式下启动SPU
import spu
import secretflow as sf
sf.init(parties=['alice', 'bob'], address='127.0.0.1:8080')
cluster_def={
'nodes': [
{
'party': 'alice',
# Please choose an unused port.
'address': '127.0.0.1:8081',
'listen_addr': '127.0.0.1:8081'
},
{
'party': 'bob',
# Please choose an unused port.
'address': '127.0.0.1:8082',
'listen_addr': '127.0.0.1:8082'
},
],
'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)
5.生产模式
在两台机器(我是用docker模拟的)启动一个cluster,主节点为bob:
ray start --head --node-ip-address="127.0.0.1" --port="8090" --resources='{"bob": 16}' --include-dashboard=False --disable-usage-stats
import spu
import secretflow as sf
cluster_config={
'parties':{
'alice':{'address':'127.0.0.1:8080'},
'bob':{'address':'172.17.0.2:8090'}
},
'self_party':'alice'
}
sf.init(address='172.17.0.2:8090',cluster_config=cluster_config)
SecretNote的安装
只是一个可视化界面,不做详细功能介绍了,能打开可视化界面就行
1.启动容器
创建docker-compose.yml文件
services:
alice:
image: 'secretflow/secretnote:unstable-amd64'
platform: linux/amd64
environment:
- SELF_PARTY=alice
- ALL_PARTIES=alice,bob
ports:
- 8090:8888
entrypoint: /root/scripts/start.sh
volumes:
- /root/scripts
bob:
image: 'secretflow/secretnote:unstable-amd64'
platform: linux/amd64
environment:
- SELF_PARTY=bob
- ALL_PARTIES=alice,bob
ports:
- 8092:8888
entrypoint: /root/scripts/start.sh
volumes:
- /root/scripts
启动两个镜像
docker compose up
2.可视化界面效果展示
注意使用自己的ip而不是127.0.0.1
安装并将两个节点加入secretNote后,可参照psi_example完成psi任务,PSI效果展示: