docker 最新的版本请查看 secretflow tags
https://hub.docker.com/r/secretflow/secretflow-anolis8/tags
启动容器
docker run -itd secretflow/secretflow-anolis8:${version}
docker exec -it docker_hash bash
import csv
def write_id_to_csv(count):
with open("id_{}.csv".format(str(count)), "w") as f:
writer = csv.writer(f)
writer.writerow(["id"])
for v in range(count):
writer.writerow([str(v)])
# 单机版,sf.utils.testing.cluster_def 建立SPU。请注意它只能在单机模式下使用,因为它使用了 127.0.0.1 作为默认ip。
import secretflow as sf
sf.shutdown()
sf.init(['alice', 'bob', 'carol'], num_cpus=8, log_to_driver=False)
# 虚拟化三个逻辑设备
alice, bob = sf.PYU('alice'), sf.PYU('bob')
spu = sf.SPU(sf.utils.testing.cluster_def(['alice', 'bob']))
# 求交
input_path = {alice: 'id_count.csv', bob: 'id_count.csv'}
output_path = {alice: '.data/alice_psi.csv', bob: '.data/bob_psi.csv'}
spu.psi_csv('id', input_path, output_path, 'alice', protocol="ECDH_PSI_2PC")
#spu.psi_csv('id', input_path, output_path, 'alice', protocol="KKRT_PSI_2PC")
#spu.psi_csv('id', input_path, output_path, 'alice', protocol="BC22_PSI_2PC")
使用容器隔离环境,启动多个ray 分布式计算节点,但都共用同一个宿主机的网络节点
以host模式启动容器,容器会和宿主机共用一个network namespace
# 启动两个容器
docker run --net=host -itd secretflow/secretflow-anolis8:0.7.7b1
docker run --net=host -itd secretflow/secretflow-anolis8:0.7.7b1
主节点启动ray 服务
RAY_DISABLE_REMOTE_CODE=true \
RAY_SECURITY_CONFIG_PATH=config.yml \
RAY_USE_TLS=0 \
ray start --head --node-ip-address="宿主机ip" --port="GCS server listening port" --resources='{"alice": 8}' --include-dashboard=False --disable-usage-stats
# RAY_USE_TLS 0 关闭tls验证
# {“alice”: 8} 意味着alice最多可以同时运行8个worker
子节点启动ray服务
RAY_DISABLE_REMOTE_CODE=true \
RAY_SECURITY_CONFIG_PATH=config.yml \
RAY_USE_TLS=0 \
ray start --address="主节点ip:主节点GCS_port" --resources='{"bob": 8}' --disable-usage-stats
查看节点启动状态,ray status ,两边同步的节点hash是否一致,服务是否正常
ray status
测试
import spu
import secretflow as sf
sf.init(address="主节点ip:GCS_port")
alice, bob = sf.PYU('alice'), sf.PYU('bob')
# 因为在同一服务器下面,以host模式启动,所以此处端口不要冲突
cluster_def = {
'nodes': [
{
'party': 'alice',
'id': '0',
# Use the address and port of alice instead.
# Please choose a unused port.
'address': 'ip:9327',
},
{
'party': 'bob',
'id': '1',
# Use the ip and port of bob instead.
# Please choose a unused port.
'address': 'ip:9328',
},
],
'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)
input_path = {alice: 'id_1000000.csv', bob: 'id_1000000.csv'}
output_path = {alice: './alice_psi.csv', bob: './bob_psi.csv'}
spu.psi_csv('id', input_path, output_path, 'alice')
首先保证两台服务器网络是可以互相访问的
yum install telnet -y
telnet ip port
两边分别以host模式启动容器
docker run --net=host -itd secretflow/secretflow-anolis8:0.7.7b1
测试A服务器容器内的端口是否能被B服务器的容器访问
在隐语框架中,SPU基于Brpc,这意味着SPU拥有一个独立于Ray网络之外的服务网格。换言之,你必须单独处理SPU的端口
在测试前先测试一下Brpc端口是否正常,在其中一方启动Brpc服务
import spu.binding._lib.link as spu_link
rank = 0
node = {
'party': 'alice',
'id': 'local:0',
'address': '127.0.0.1:9001',
# The listen address of this node
}
desc = spu_link.Desc()
desc.add_party(node['id'], node['address'])
link = spu_link.create_brpc(desc, rank)
另外一方容器内访问对方的端口状况, 如果正常则跳过
telnet ip port
sf.init(address="主节点ip:GCS_port")
alice, bob = sf.PYU('alice'), sf.PYU('bob')
cluster_def={
'nodes': [
{
'party': 'alice',
'id': '0',
# Use the address and port of alice instead.
# Please choose a unused port.
'address': 'ip:9327',
},
{
'party': 'bob',
'id': '1',
# Use the ip and port of bob instead.
# Please choose a unused port.
'address': 'ip:9327',
},
],
'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)
input_path = {alice: 'id_1000000.csv', bob: 'id_1000000.csv'}
output_path = {alice: './alice_psi.csv', bob: './bob_psi.csv'}
spu.psi_csv('id', input_path, output_path, 'alice')