data61 / MP-SPDZ

Versatile framework for multi-party computation
Other
897 stars 278 forks source link

Online Batch Inference of HiNet #1472

Closed kanav99 closed 1 week ago

kanav99 commented 1 month ago

I want to perform a benchmark of two party batch inference (not training) of HiNet (based on this code). I want to run it using SPDZ2k. I wanted to make sure if I am using the right code and steps to get the numbers.

This is the code I am using:

program.options_from_args()
program.use_edabit(True)
program.use_trunc_pr = False
sfix.set_precision(16, 31)

from Compiler import ml
tf = ml
tf.set_n_threads(4)

bs = 100

test_samples = MultiArray([bs, 32, 32, 3], sfix)
layers = [
    tf.keras.layers.Conv2D(64, 5, 1, 1),
    tf.keras.layers.MaxPooling2D(3, 2, 0),
    tf.keras.layers.Activation('relu'),
    tf.keras.layers.Conv2D(64, 5, 1, 1),
    tf.keras.layers.MaxPooling2D(3, 2, 0),
    tf.keras.layers.Activation('relu'),
    tf.keras.layers.Conv2D(64, 5, 1, 1),
    tf.keras.layers.MaxPooling2D(3, 2, 0),
    tf.keras.layers.Activation('relu'),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(10)
]

model = tf.keras.models.Sequential(layers)

model.build(test_samples.sizes, bs)

start = 0
for var in model.trainable_variables:
    var.assign_all(0)

opt = model.opt
opt.time_layers = True
start_timer(5)
opt.forward(bs)
stop_timer(5)

I compile it using:

python3 compile.py -R 64

And run it using:

./spdz2k-party.x -N 2 --verbose -h 172.31.42.195 -F hinet -p 0

Also, I have commented out this line as I don't need to calculate softmax. The MP-SPDZ code was compiled with -DINSECURE flag.

Would you be able to verify if this this the most optimal way to run this task?

Thanks

mkskeller commented 1 month ago

I don't think there is a clearly defined notion of optimal to give a conclusive answer. For example, you could reduce the precision and still get acceptable results depending on the data. Similarly, the optimal number of threads depends on the hardware as well as the computation. What I can say is that I don't see any obvious issues with your approach. Since you are looking for an online-only benchmark, using edaBits is almost certainly faster than not doing so. However, one could argue that a cut-down online-only benchmark would use matrix triples instead of computing matrix multiplications and convolutions from basic triples. As a result, you will probably find that the cost is dominated by the convolution layers, which might not be the case when using matrix triples.

kanav99 commented 1 month ago

Thanks for the clarifications!

kanav99 commented 1 month ago

In the program above, I have the line sfix.set_precision(16, 31), and I am compiling with command python3 compile.py -R 64.

Ideally, I want 31-bit fixed-point values with 16-bit precision, and SPDZ2k slack s = 64 (for malicious security), and hence, secret sharing to happen over 128-bit rings (64-bits for fitting intermediate result of fixed-point multiplication and 64-bit for slack). For this, should I have instead used sfix.set_precision(16, 31) and python3 compile.py -R 128?

mkskeller commented 1 month ago

No, the compilation is independent of the SPDZ2k security parameter, so -R 64 is the right choice.

kanav99 commented 1 month ago

Thanks for the clarification!