secretflow / psi

The repo of Private Set Intersection(PSI) and Private Information Retrieval(PIR) from SecretFlow.
https://www.secretflow.org.cn/docs/psi
Apache License 2.0
22 stars 16 forks source link

关于使用python运行和secretnote分段运行有明显时间差别的问题 #131

Open winnylyc opened 2 months ago

winnylyc commented 2 months ago

您好,打扰您了。 我在benchmark labeled PSI时发现直接使用python运行和使用secretnote分段运行有明显时间差别。我想请教一下是什么因素导致了这样的差别。 我目前在尝试的是以下的代码。 server方:

import secretflow as sf
import spu
import time

cluster_config = {
    'parties' : {
        'alice': {
            'address': '127.0.0.1:59179',
            'listen_addr': '0.0.0.0:59179'
        },
        'bob': {
            'address': '127.0.0.1:53341',
            'listen_addr': '0.0.0.0:53341'
        }
    },
    'self_party': 'alice'
}
sf.shutdown
sf.init(address='local', cluster_config=cluster_config)
cluster_def = {
    "nodes": [
        {
            "party": "alice",
            "address": "127.0.0.1:45413"
        },
        {
            "party": "bob",
            "address": "127.0.0.1:47480"
        },
    ],
    "runtime_config": {
        "protocol": spu.spu_pb2.SEMI2K,
        "field": spu.spu_pb2.FM128
    },
}

spu = sf.SPU(
    cluster_def,
    link_desc={
        "connect_retry_times": 60,
        "connect_retry_interval_ms": 1000,
    }
)

start_time = time.time()
spu.pir_query(
    server="alice",
    client="bob",
    server_setup_path="./alice_exactpsi_setup_1e6_npq20",
    client_key_columns=["name"],
    client_input_path="./bob_exactpsi_1e6_to_1e2.csv",
    client_output_path="./bob_exactpsi_1e6_to_1e2_output_test.csv"
)
end_time = time.time()
elapsed_time = end_time - start_time
print("The function took", elapsed_time, "seconds to run.")

对于时间的输出是 The function took 12.396417617797852 seconds to run. client方:

import secretflow as sf
import spu
import time

cluster_config = {
    'parties' : {
        'alice': {
            'address': '127.0.0.1:59179',
            'listen_addr': '0.0.0.0:59179'
        },
        'bob': {
            'address': '127.0.0.1:53341',
            'listen_addr': '0.0.0.0:53341'
        }
    },
    'self_party': 'bob'
}
sf.shutdown
sf.init(address='local', cluster_config=cluster_config)
cluster_def = {
    "nodes": [
        {
            "party": "alice",
            "address": "127.0.0.1:45413"
        },
        {
            "party": "bob",
            "address": "127.0.0.1:47480"
        },
    ],
    "runtime_config": {
        "protocol": spu.spu_pb2.SEMI2K,
        "field": spu.spu_pb2.FM128
    },
}

spu = sf.SPU(
    cluster_def,
    link_desc={
        "connect_retry_times": 60,
        "connect_retry_interval_ms": 1000,
    }
)

start_time = time.time()
spu.pir_query(
    server="alice",
    client="bob",
    server_setup_path="./alice_exactpsi_setup_1e6_npq20",
    client_key_columns=["name"],
    client_input_path="./bob_exactpsi_1e6_to_1e2.csv",
    client_output_path="./bob_exactpsi_1e6_to_1e2_output_test.csv"
)
end_time = time.time()
elapsed_time = end_time - start_time
print("The function took", elapsed_time, "seconds to run.")

对于时间的输出是 The function took 14.504458665847778 seconds to run.

在secrenote上的运行参考https://www.bilibili.com/video/BV13M4m1R7gp 上面的教程,将运行分为三个部分,连接sf, 连接spu, 运行spu.pir_query。基本上运行spu.pir_query这一部分在10s左右。另外我也尝试了,将spu.pir_query这个部分放在两个代码块中分两个party运行,这种尝试下依然是10s,而且两边的时间差不多长(并没有12s和14s这样大的差别)。 还有一种尝试,就是在secretnote将整个代码放入代码块运行,这样的表现和直接运行.py文件一样,所以造成差别的因素因该主要是分段式运行。 对于这个差异我有两个问题:

  1. 为什么分段式运行代码的花费时间更小?有没有方法使单单调用pir_query的时间开销更小?
  2. 似乎receiver(clinet)总是比sender(server)的耗时更久,这里面是什么原因?

非常期待您的回答!感谢您的帮助!

6fj commented 2 months ago

我先回答第二个问题吧。

receiver在算法上需要对sender发送的密文处理后才能得到结果,并且还要最终写到文件中,因此你观察到的现象是合理。

对于第一个问题,可以麻烦放一下代码吗?感谢

winnylyc commented 2 months ago

非常感谢您的回答!

对于展示代码,我没想到特别好的方法来展示secretnote,所以这边就用截图来展示了。😓 image image image image image 可以看到这种情况下,receiver和sender都只要10s左右就能完成spu.pir_query,receiver和sender间的执行时间没有很大的时间差。

我也尝试了将这些所有代码块整合在两个代码块中运行(即一个代码块代表一个party),这种情况测下来和运行两个party各自运行一个.py文件是一样的。

基本上,分段运行时spu.pir_query会用10s左右,将全部函数整合在一起运行时则spu.pir_query会用12s到14s。

winnylyc commented 2 months ago

另外,我刚刚也在别的机器上尝试一下,发现性能越差的机器,这个整体运行和分段运行的时间差越大。我上面的测试是16个处理器,cpu型号是AMD Ryzen 7 7840H with Radeon 780M Graphics,差别是 分段:10s,整体: 13s。在4个处理器,型号为Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz的机器上,差别为 分段:38s, 整体:60s

6fj commented 1 month ago

hi @NewByVector 能看一下这个问题吗?感谢