mirror of
https://github.com/data61/MP-SPDZ.git
synced 2026-01-09 13:37:58 -05:00
Merge branch 'refs/heads/master' into parallel_permutations
# Conflicts: # Processor/Processor.hpp # Protocols/FakeProtocol.h # Protocols/Rep3Shuffler.h # Protocols/Rep3Shuffler.hpp # Protocols/SecureShuffle.h # Protocols/SecureShuffle.hpp
This commit is contained in:
Submodule Programs/Circuits updated: 908452826c...cdd5927692
@@ -24,8 +24,10 @@ decision_tree.max_leaves = 2000
|
||||
if 'nearest' in program.args:
|
||||
sfix.round_nearest = True
|
||||
|
||||
layers = decision_tree.TreeTrainer(
|
||||
train[1], train[0], n_levels, binary=binary, n_threads=n_threads).train()
|
||||
trainer = decision_tree.TreeTrainer(
|
||||
train[1], train[0], n_levels, binary=binary, n_threads=n_threads)
|
||||
trainer.time = 'time' in program.args
|
||||
layers = trainer.train()
|
||||
|
||||
#decision_tree.output_decision_tree(layers)
|
||||
|
||||
|
||||
@@ -22,16 +22,17 @@ elif 'vertical' in program.args:
|
||||
b = sfix.input_tensor_via(1, X_train[:,X_train.shape[1] // 2:])
|
||||
X_train = a.concat_columns(b)
|
||||
y_train = sint.input_tensor_via(0, y_train)
|
||||
elif 'party0' in program.args:
|
||||
a = sfix.input_tensor_via(0, X_train[:,:X_train.shape[1] // 2])
|
||||
b = sfix.input_tensor_via(1, shape=X_train[:,X_train.shape[1] // 2:].shape)
|
||||
elif 'party0' in program.args or 'party1' in program.args:
|
||||
party = int('party1' in program.args)
|
||||
a = sfix.input_tensor_via(
|
||||
0, X_train[:,:X_train.shape[1] // 2] if party == 0 else None,
|
||||
shape=X_train[:,:X_train.shape[1] // 2].shape)
|
||||
b = sfix.input_tensor_via(
|
||||
1, X_train[:,X_train.shape[1] // 2:] if party == 1 else None,
|
||||
shape=X_train[:,X_train.shape[1] // 2:].shape)
|
||||
X_train = a.concat_columns(b)
|
||||
y_train = sint.input_tensor_via(0, y_train)
|
||||
elif 'party1' in program.args:
|
||||
a = sfix.input_tensor_via(0, shape=X_train[:,:X_train.shape[1] // 2].shape)
|
||||
b = sfix.input_tensor_via(1, X_train[:,X_train.shape[1] // 2:])
|
||||
X_train = a.concat_columns(b)
|
||||
y_train = sint.input_tensor_via(0, shape=y_train.shape)
|
||||
y_train = sint.input_tensor_via(0, y_train if party == 0 else None,
|
||||
shape=y_train.shape)
|
||||
else:
|
||||
X_train = sfix.input_tensor_via(0, X_train)
|
||||
y_train = sint.input_tensor_via(0, y_train)
|
||||
|
||||
@@ -3,7 +3,7 @@ from sklearn.model_selection import train_test_split
|
||||
from Compiler import decision_tree
|
||||
|
||||
data = pandas.read_csv(
|
||||
'https://datahub.io/machine-learning/adult/r/adult.csv')
|
||||
'https://raw.githubusercontent.com/jbrownlee/Datasets/master/adult-all.csv', header=None)
|
||||
#'/tmp/adult.csv')
|
||||
|
||||
data, attr_types = decision_tree.preprocess_pandas(data)
|
||||
|
||||
7
Programs/Source/export-a2b.py
Normal file
7
Programs/Source/export-a2b.py
Normal file
@@ -0,0 +1,7 @@
|
||||
@export
|
||||
def a2b(x, res):
|
||||
print_ln('x=%s', x.reveal())
|
||||
res[:] = sbitvec(x, length=16)
|
||||
print_ln('res=%s', x.reveal())
|
||||
|
||||
a2b(sint(size=10), sbitvec.get_type(16).Array(10))
|
||||
7
Programs/Source/export-b2a.py
Normal file
7
Programs/Source/export-b2a.py
Normal file
@@ -0,0 +1,7 @@
|
||||
@export
|
||||
def b2a(res, x):
|
||||
print_ln('x=%s', x.reveal())
|
||||
res[:] = sint(x[:])
|
||||
print_ln('res=%s', x.reveal())
|
||||
|
||||
b2a(sint.Array(size=10), sbitvec.get_type(16).Array(10))
|
||||
8
Programs/Source/export-msort.py
Normal file
8
Programs/Source/export-msort.py
Normal file
@@ -0,0 +1,8 @@
|
||||
@export
|
||||
def sort(x, key_indices):
|
||||
print_ln('x=%s', x.reveal())
|
||||
print_ln('key_indices=%s', key_indices)
|
||||
res = x.sort(key_indices=key_indices)
|
||||
print_ln('res=%s', x.reveal())
|
||||
|
||||
sort(sint.Matrix(500, 2), regint(0, size=1))
|
||||
7
Programs/Source/export-sort.py
Normal file
7
Programs/Source/export-sort.py
Normal file
@@ -0,0 +1,7 @@
|
||||
@export
|
||||
def sort(x):
|
||||
print_ln('x=%s', x.reveal())
|
||||
res = x.sort()
|
||||
print_ln('res=%s', x.reveal())
|
||||
|
||||
sort(sint.Array(1000))
|
||||
8
Programs/Source/export-trunc.py
Normal file
8
Programs/Source/export-trunc.py
Normal file
@@ -0,0 +1,8 @@
|
||||
@export
|
||||
def trunc_pr(x):
|
||||
print_ln('x=%s', x.reveal())
|
||||
res = x.round(32, 2)
|
||||
print_ln('res=%s', res.reveal())
|
||||
return res
|
||||
|
||||
trunc_pr(sint(0, size=1000))
|
||||
@@ -88,7 +88,7 @@ model = tf.keras.models.Sequential(AlexNet)
|
||||
|
||||
model.compile_by_args(program)
|
||||
|
||||
model.build(training_samples.sizes)
|
||||
model.build(training_samples.sizes, program=program)
|
||||
model.summary()
|
||||
|
||||
opt = model.fit(
|
||||
|
||||
@@ -38,9 +38,9 @@ from random import randint
|
||||
import sys
|
||||
program.bit_length = 128
|
||||
|
||||
n_parallel = int(sys.argv[2])
|
||||
n_total = int(sys.argv[3])
|
||||
nmessages = int(sys.argv[4])
|
||||
n_parallel = int(program.args[1])
|
||||
n_total = int(program.args[2])
|
||||
nmessages = int(program.args[3])
|
||||
|
||||
use_mimc_prf = True
|
||||
# Use just one PRF
|
||||
|
||||
@@ -36,6 +36,7 @@ model.build(test_samples.sizes)
|
||||
start = 0
|
||||
for var in model.trainable_variables:
|
||||
var.assign_all(0)
|
||||
# activate to use the model output by keras_mnist_lenet
|
||||
# start = var.read_from_file(start)
|
||||
|
||||
guesses = model.predict(test_samples)
|
||||
|
||||
@@ -82,22 +82,8 @@ def _(i):
|
||||
sgd.run(batch_size)
|
||||
stop_timer(1)
|
||||
|
||||
def get_correct(Y, n):
|
||||
n_correct = regint(0)
|
||||
for i in range(n):
|
||||
n_correct += (Y[i].reveal() > 0).bit_xor(
|
||||
layers[-2].Y[i][0][0][0].reveal() < 0)
|
||||
return n_correct
|
||||
|
||||
sgd.forward(N)
|
||||
|
||||
n_correct = get_correct(layers[-1].Y, N)
|
||||
n_correct, loss = sgd.reveal_correctness(layers[0].X, layers[-1].Y)
|
||||
print_ln('train_acc: %s (%s/%s)', cfix(n_correct) / N, n_correct, N)
|
||||
|
||||
training_address = layers[0].X.address
|
||||
layers[0].X.address = X.address
|
||||
sgd.forward(n_test)
|
||||
layers[0].X.address = training_address
|
||||
|
||||
n_correct = get_correct(Y, n_test)
|
||||
n_correct, loss = sgd.reveal_correctness(X, Y)
|
||||
print_ln('acc: %s (%s/%s)', cfix(n_correct) / n_test, n_correct, n_test)
|
||||
|
||||
@@ -63,9 +63,6 @@ else:
|
||||
if 'nearest' in program.args:
|
||||
sfix.round_nearest = True
|
||||
|
||||
if program.options.ring:
|
||||
assert sfix.f * 4 == int(program.options.ring)
|
||||
|
||||
debug_ml = ('debug_ml' in program.args) * 2 ** (sfix.f / 2)
|
||||
|
||||
if '1dense' in program.args:
|
||||
|
||||
11
Programs/Source/personal_client_example.py
Normal file
11
Programs/Source/personal_client_example.py
Normal file
@@ -0,0 +1,11 @@
|
||||
listen_for_clients(15000)
|
||||
socket = accept_client_connection(15000)
|
||||
|
||||
n = 1000
|
||||
|
||||
for i in range(2):
|
||||
x = personal.read_fix_from_socket(i, socket, n)
|
||||
sfix(x).write_fully_to_socket(socket)
|
||||
|
||||
res = sum(sfix.read_from_socket(socket, n))
|
||||
print_ln('%s', res.reveal())
|
||||
@@ -2,7 +2,7 @@ from Compiler import instructions_base
|
||||
import sys
|
||||
|
||||
program.bit_length = 128
|
||||
nparallel = int(sys.argv[2])
|
||||
nparallel = int(program.args[1])
|
||||
|
||||
instructions_base.set_global_vector_size(nparallel)
|
||||
use_cubes = True
|
||||
|
||||
@@ -21,3 +21,13 @@ test(a, 10000, 10000)
|
||||
test(b, 10000, 20000)
|
||||
test(a, 1000000, 1000000)
|
||||
test(b, 1000000, 2000000)
|
||||
|
||||
a = 1
|
||||
if True:
|
||||
if True:
|
||||
a = 2
|
||||
if True:
|
||||
a = 3
|
||||
else:
|
||||
a = 4
|
||||
crash()
|
||||
|
||||
53
Programs/Source/torch_densenet.py
Normal file
53
Programs/Source/torch_densenet.py
Normal file
@@ -0,0 +1,53 @@
|
||||
# this tests the pretrained DenseNet in secure computation
|
||||
|
||||
program.options_from_args()
|
||||
sfix.set_precision_from_args(program, adapt_ring=True)
|
||||
MultiArray.disable_index_checks()
|
||||
Array.check_indices = False
|
||||
|
||||
from Compiler import ml
|
||||
|
||||
try:
|
||||
ml.set_n_threads(int(program.args[2]))
|
||||
except:
|
||||
pass
|
||||
|
||||
import torchvision
|
||||
import torch
|
||||
import numpy
|
||||
import requests
|
||||
import io
|
||||
import PIL
|
||||
|
||||
from torchvision import transforms
|
||||
|
||||
model = getattr(torchvision.models.densenet, 'densenet' + program.args[1])(
|
||||
weights='DEFAULT')
|
||||
|
||||
r = requests.get('https://github.com/pytorch/hub/raw/master/images/dog.jpg')
|
||||
input_image = PIL.Image.open(io.BytesIO(r.content))
|
||||
preprocess = transforms.Compose([
|
||||
transforms.Resize(256),
|
||||
transforms.CenterCrop(224),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
||||
])
|
||||
input_tensor = preprocess(input_image)
|
||||
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
||||
|
||||
with torch.no_grad():
|
||||
output = int(model(input_batch).argmax())
|
||||
print('Model says %d' % output)
|
||||
|
||||
secret_input = sfix.input_tensor_via(
|
||||
0, numpy.moveaxis(input_batch.numpy(), 1, -1))
|
||||
|
||||
layers = ml.layers_from_torch(
|
||||
model, secret_input.shape, 1, input_via=0,
|
||||
layer_args={model.features.conv0: {'weight_type': sfix.get_prec_type(32)}})
|
||||
|
||||
optimizer = ml.Optimizer(layers)
|
||||
optimizer.output_stats = 'output_stats' in program.args
|
||||
|
||||
print_ln('Secure computation says %s',
|
||||
optimizer.eval(secret_input, top=True)[0].reveal())
|
||||
@@ -40,7 +40,7 @@ from Compiler import ml
|
||||
|
||||
ml.set_n_threads(int(program.args[2]))
|
||||
|
||||
layers = ml.layers_from_torch(net, data[0][1].shape, 128)
|
||||
layers = ml.layers_from_torch(net, data[0][1].shape, 128, program=program)
|
||||
layers[0].X = data[0][1]
|
||||
layers[-1].Y = data[0][0]
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ for train in True, False:
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
|
||||
net = nn.Sequential(
|
||||
layers = [
|
||||
nn.Conv2d(1, 20, 5),
|
||||
nn.ReLU(),
|
||||
nn.MaxPool2d(2),
|
||||
@@ -29,7 +29,12 @@ net = nn.Sequential(
|
||||
nn.Linear(800, 500),
|
||||
nn.ReLU(),
|
||||
nn.Linear(500, 10)
|
||||
)
|
||||
]
|
||||
|
||||
if 'bn' in program.args:
|
||||
layers.insert(3, nn.BatchNorm2d(20))
|
||||
|
||||
net = nn.Sequential(*layers)
|
||||
|
||||
# train for a bit
|
||||
transform = torchvision.transforms.Compose(
|
||||
|
||||
49
Programs/Source/torch_resnet.py
Normal file
49
Programs/Source/torch_resnet.py
Normal file
@@ -0,0 +1,49 @@
|
||||
# this tests the pretrained ResNet in secure computation
|
||||
|
||||
program.options_from_args()
|
||||
|
||||
from Compiler import ml
|
||||
|
||||
try:
|
||||
ml.set_n_threads(int(program.args[2]))
|
||||
except:
|
||||
pass
|
||||
|
||||
import torchvision
|
||||
import torch
|
||||
import numpy
|
||||
import requests
|
||||
import io
|
||||
import PIL
|
||||
|
||||
from torchvision import transforms
|
||||
|
||||
model = getattr(torchvision.models.resnet, 'resnet' + program.args[1])(
|
||||
weights='DEFAULT')
|
||||
|
||||
r = requests.get('https://github.com/pytorch/hub/raw/master/images/dog.jpg')
|
||||
input_image = PIL.Image.open(io.BytesIO(r.content))
|
||||
preprocess = transforms.Compose([
|
||||
transforms.Resize(256),
|
||||
transforms.CenterCrop(224),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
||||
])
|
||||
input_tensor = preprocess(input_image)
|
||||
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
||||
|
||||
with torch.no_grad():
|
||||
output = int(model(input_batch).argmax())
|
||||
print('Model says %d' % output)
|
||||
|
||||
secret_input = sfix.input_tensor_via(
|
||||
0, numpy.moveaxis(input_batch.numpy(), 1, -1))
|
||||
|
||||
layers = ml.layers_from_torch(model, secret_input.shape, 1, input_via=0)
|
||||
|
||||
optimizer = ml.Optimizer(layers, time_layers='time_layers' in program.args)
|
||||
|
||||
start_timer(1)
|
||||
print_ln('Secure computation says %s',
|
||||
optimizer.eval(secret_input, top=True)[0].reveal())
|
||||
stop_timer(1)
|
||||
53
Programs/Source/torch_squeeze.py
Normal file
53
Programs/Source/torch_squeeze.py
Normal file
@@ -0,0 +1,53 @@
|
||||
# this tests the pretrained SqueezeNet in secure computation
|
||||
|
||||
program.options_from_args()
|
||||
|
||||
from Compiler import ml
|
||||
|
||||
try:
|
||||
ml.set_n_threads(int(program.args[1]))
|
||||
except:
|
||||
pass
|
||||
|
||||
import torchvision
|
||||
import torch
|
||||
import numpy
|
||||
import requests
|
||||
import io
|
||||
import PIL
|
||||
|
||||
from torchvision import transforms
|
||||
|
||||
model = torchvision.models.get_model('SqueezeNet1_1', weights='DEFAULT')
|
||||
|
||||
r = requests.get('https://github.com/pytorch/hub/raw/master/images/dog.jpg')
|
||||
input_image = PIL.Image.open(io.BytesIO(r.content))
|
||||
preprocess = transforms.Compose([
|
||||
transforms.Resize(256),
|
||||
transforms.CenterCrop(224),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
||||
])
|
||||
input_tensor = preprocess(input_image)
|
||||
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
||||
|
||||
with torch.no_grad():
|
||||
output = int(model(input_batch).argmax())
|
||||
print('Model says %d' % output)
|
||||
|
||||
secret_input = sfix.input_tensor_via(
|
||||
0, numpy.moveaxis(input_batch.numpy(), 1, -1))
|
||||
|
||||
layer_args = {}
|
||||
if 'first_conv_high' in program.args:
|
||||
layer_args[getattr(model.features, '0')] = \
|
||||
{'weight_type': sfix.get_prec_type(32)}
|
||||
|
||||
layers = ml.layers_from_torch(model, secret_input.shape, 1, input_via=0,
|
||||
layer_args=layer_args)
|
||||
|
||||
optimizer = ml.Optimizer(layers)
|
||||
optimizer.output_stats = 'output_stats' in program.args
|
||||
|
||||
print_ln('Secure computation says %s',
|
||||
optimizer.eval(secret_input, top=True)[0].reveal())
|
||||
42
Programs/Source/torch_vgg.py
Normal file
42
Programs/Source/torch_vgg.py
Normal file
@@ -0,0 +1,42 @@
|
||||
# this tests the pretrained VGG in secure computation
|
||||
|
||||
program.options_from_args()
|
||||
|
||||
from Compiler import ml
|
||||
|
||||
try:
|
||||
ml.set_n_threads(int(program.args[2]))
|
||||
except:
|
||||
pass
|
||||
|
||||
import torchvision
|
||||
import torch
|
||||
import numpy
|
||||
import requests
|
||||
import io
|
||||
import PIL
|
||||
|
||||
from torchvision import transforms
|
||||
|
||||
name = 'vgg' + program.args[1]
|
||||
model = getattr(torchvision.models, name)(weights='DEFAULT')
|
||||
|
||||
r = requests.get('https://github.com/pytorch/hub/raw/master/images/dog.jpg')
|
||||
input_image = PIL.Image.open(io.BytesIO(r.content))
|
||||
input_tensor = transforms._presets.ImageClassification(crop_size=32)(input_image)
|
||||
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
||||
|
||||
with torch.no_grad():
|
||||
output = int(model(input_batch).argmax())
|
||||
print('Model says %d' % output)
|
||||
|
||||
secret_input = sfix.input_tensor_via(
|
||||
0, numpy.moveaxis(input_batch.numpy(), 1, -1))
|
||||
|
||||
layers = ml.layers_from_torch(model, secret_input.shape, 1, input_via=0)
|
||||
|
||||
optimizer = ml.Optimizer(layers)
|
||||
optimizer.time_layers = True
|
||||
|
||||
print_ln('Secure computation says %s',
|
||||
optimizer.eval(secret_input, top=True)[0].reveal())
|
||||
Reference in New Issue
Block a user