Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing placement names for local testing and remote execution. #55

Merged
merged 5 commits into from Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/python-functions/Makefile
@@ -0,0 +1,22 @@

MAIN_IMAGE?=tfencrypted/runtime-base-worker:latest

run:
docker run \
--tty \
--volume $(realpath ../../moose):/moose \
--volume $(realpath .):/examples/python-functions \
${MAIN_IMAGE} \
sh -c "python3 /examples/python-functions/main.py --verbose --cluster-spec /examples/python-functions/cluster-spec.yaml"

test-localhost:
mortendahl marked this conversation as resolved.
Show resolved Hide resolved
docker run \
--tty \
--volume $(realpath ../../moose):/moose \
--volume $(realpath .):/examples/python-functions \
${MAIN_IMAGE} \
sh -c "python3 /examples/python-functions/main.py --runtime test --verbose "



.PHONY: run test-localhost
17 changes: 17 additions & 0 deletions examples/python-functions/README.md
@@ -0,0 +1,17 @@
# Calling Python functions from Moose

This example shows you how you can call python functions on cleartext within Moose.

We recommend running the example using the included docker-compose.yaml file to start up the needed workers, and docker for running `main.py`. To do so, first run the following from this directory to start up the required workers:

```
docker-compose up --build
```

Then run the following to execute `main.py` against there:

```
make run
```

To stop the workers again run `docker-compose down`.
12 changes: 12 additions & 0 deletions examples/python-functions/docker-compose.yaml
Expand Up @@ -6,6 +6,9 @@ services:
build:
context: .
dockerfile: Dockerfile.worker
volumes:
- "../../moose:/moose"
- ".:/examples/python-functions"
ports:
- 50000:50000
environment:
Expand All @@ -15,6 +18,9 @@ services:
build:
context: .
dockerfile: Dockerfile.worker
volumes:
- "../../moose:/moose"
- ".:/examples/python-functions"
ports:
- 50001:50001
environment:
Expand All @@ -24,6 +30,9 @@ services:
build:
context: .
dockerfile: Dockerfile.worker
volumes:
- "../../moose:/moose"
- ".:/examples/python-functions"
ports:
- 50002:50002
environment:
Expand All @@ -33,6 +42,9 @@ services:
build:
context: .
dockerfile: Dockerfile.worker
volumes:
- "../../moose:/moose"
- ".:/examples/python-functions"
ports:
- 50003:50003
environment:
Expand Down
12 changes: 6 additions & 6 deletions examples/python-functions/main.py
Expand Up @@ -26,6 +26,8 @@
aggregator = HostPlacement(name="aggregator")
outputter = HostPlacement(name="outputter")

all_placements = [inputter0, inputter1, aggregator, outputter]


@function
def mul_fn(x, y):
Expand Down Expand Up @@ -58,20 +60,18 @@ def my_comp():

if __name__ == "__main__":
if args.runtime == "test":
runtime = TestRuntime(num_workers=len(concrete_comp.devices()))
runtime = TestRuntime(workers=concrete_comp.devices())
elif args.runtime == "remote":
runtime = RemoteRuntime(args.cluster_spec)
assert len(runtime.executors) == len(concrete_comp.devices())
assert set(concrete_comp.devices()).subset(runtime.executors.keys())
else:
raise ValueError(f"Unknown runtime '{args.runtime}'")

runtime.evaluate_computation(
computation=concrete_comp,
placement_assignment={
inputter0: runtime.executors[0],
inputter1: runtime.executors[1],
aggregator: runtime.executors[2],
outputter: runtime.executors[3],
placement: runtime.executors[placement.name]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I think it was better before then, ie without the all_placements :D

for placement in all_placements
},
)

Expand Down
2 changes: 1 addition & 1 deletion moose/executor/executor.py
Expand Up @@ -325,7 +325,7 @@ def execute_synchronous_block(self, op, session_id, **control_inputs):
# default value of prime
prime = 170141183460469231731687303715885907969
# Inverse mod prime to get clear value Integer
invR = 96651956244403355751989957128965938997 # (2^128^-1 mod prime)
invR = 96651956244403355751989957128965938997 # (2^128^-1 mod prime)

mpspdz_dir = (
f"/MP-SPDZ/tmp/{session_id}/{op.invocation_key}/Player-Data/Private-Output"
Expand Down
6 changes: 3 additions & 3 deletions moose/executor/executor_test.py
Expand Up @@ -25,15 +25,15 @@ def _create_test_players(number_of_players=2):


def _run_computation(comp, players):
runtime = TestRuntime(num_workers=len(players))
runtime = TestRuntime([worker.name for worker in players])
placement_assignment = {
players[i]: runtime.executors[i] for i in range(len(players))
players[i]: runtime.executors[players[i].name] for i in range(len(players))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be rewritten as player: runtime.executors[player.name] for player in players

}
concrete_comp = comp.trace_func()
runtime.evaluate_computation(
concrete_comp, placement_assignment=placement_assignment
)
computation_result = runtime.executors[-1].store
computation_result = runtime.executors[players[-1].name].store
return computation_result


Expand Down
12 changes: 7 additions & 5 deletions moose/runtime.py
Expand Up @@ -46,12 +46,14 @@ def __init__(self, cluster_spec: Union[Dict, str]) -> None:


class TestRuntime(Runtime):
def __init__(self, num_workers) -> None:
def __init__(self, workers) -> None:
mortendahl marked this conversation as resolved.
Show resolved Hide resolved
channel_manager = ChannelManager()
self.executors = [
KernelBasedExecutor(name=f"worker{i}", channel_manager=channel_manager)
for i in range(num_workers)
]
self.executors = {
placement_name: KernelBasedExecutor(
name=placement_name, channel_manager=channel_manager
)
for placement_name in workers
}


_RUNTIME: Optional[Runtime] = None
Expand Down