-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
305 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from time import sleep | ||
import os | ||
|
||
import union | ||
|
||
image = union.ImageSpec( | ||
registry=os.environ.get("DOCKER_REGISTRY", None), | ||
packages=["union"], | ||
) | ||
|
||
actor = union.ActorEnvironment( | ||
name="my-actor", | ||
container_image=image, | ||
replica_count=1, | ||
) | ||
|
||
|
||
@actor.cache | ||
def load_model(state: int) -> callable: | ||
sleep(4) # simulate model loading | ||
return lambda value: state + value | ||
|
||
|
||
@actor.task | ||
def evaluate(value: int, state: int) -> int: | ||
model = load_model(state=state) | ||
return model(value) | ||
|
||
|
||
@union.workflow | ||
def wf(init_value: int = 1, state: int = 3) -> int: | ||
out = evaluate(value=init_value, state=state) | ||
out = evaluate(value=out, state=state) | ||
out = evaluate(value=out, state=state) | ||
out = evaluate(value=out, state=state) | ||
return out |
60 changes: 60 additions & 0 deletions
60
user_guide/core_concepts/actors/byoc/caching_custom_object.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from functools import partial | ||
from pathlib import Path | ||
from time import sleep | ||
import os | ||
|
||
import union | ||
|
||
image = union.ImageSpec( | ||
registry=os.environ.get("DOCKER_REGISTRY", None), | ||
packages=["union"], | ||
) | ||
|
||
actor = union.ActorEnvironment( | ||
name="my-actor", | ||
container_image=image, | ||
replica_count=2, | ||
) | ||
|
||
|
||
class MyModel: | ||
"""Simple model that multiples value with model_state.""" | ||
|
||
def __init__(self, model_state: int): | ||
self.model_state = model_state | ||
|
||
def __call__(self, value: int): | ||
return self.model_state * value | ||
|
||
|
||
@task(container_image=image, cache=True, cache_version="v1") | ||
def create_model_state() -> union.FlyteFile: | ||
working_dir = Path(union.current_context().working_directory) | ||
model_state_path = working_dir / "model_state.txt" | ||
model_state_path.write_text("4") | ||
return model_state_path | ||
|
||
|
||
@actor.cache | ||
def load_model(model_state_path: union.FlyteFile) -> MyModel: | ||
# Simulate model loading time. This can take a long time | ||
# because the FlyteFile download is large, or when the | ||
# model is loaded onto the GPU. | ||
sleep(10) | ||
with model_state_path.open("r") as f: | ||
model_state = int(f.read()) | ||
|
||
return MyModel(model_state=model_state) | ||
|
||
|
||
@actor.task | ||
def inference(value: int, model_state_path: union.FlyteFile) -> int: | ||
model = load_model(model_state_path) | ||
return model(value) | ||
|
||
|
||
@workflow | ||
def run_inference(values: list[int] = list(range(20))) -> list[int]: | ||
model_state = create_model_state() | ||
inference_ = partial(inference, model_state_path=model_state) | ||
return union.map_task(inference_)(value=values) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from time import sleep | ||
import os | ||
|
||
import union | ||
|
||
image = union.ImageSpec( | ||
registry=os.environ.get("DOCKER_REGISTRY", None), | ||
packages=["union"], | ||
) | ||
|
||
actor = union.ActorEnvironment( | ||
name="my-actor", | ||
container_image=image, | ||
replica_count=1, | ||
) | ||
|
||
|
||
class MyObj: | ||
def __init__(self, state: int): | ||
self.state = state | ||
|
||
def __hash__(self): | ||
return hash(self.state) | ||
|
||
def __eq__(self, other): | ||
return self.state == other.state | ||
|
||
|
||
@actor.cache | ||
def get_state(obj: MyObj) -> int: | ||
sleep(2) | ||
return obj.state | ||
|
||
|
||
@actor.task | ||
def construct_and_get_value(state: int) -> int: | ||
obj = MyObj(state=state) | ||
return get_state(obj) | ||
|
||
|
||
@union.workflow | ||
def wf(state: int = 2) -> int: | ||
value = construct_and_get_value(state=state) | ||
value = construct_and_get_value(state=value) | ||
value = construct_and_get_value(state=value) | ||
value = construct_and_get_value(state=value) | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
user_guide/core_concepts/actors/serverless/caching_basic.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from time import sleep | ||
|
||
import union | ||
|
||
|
||
actor = union.ActorEnvironment( | ||
name="my-actor", | ||
replica_count=1, | ||
) | ||
|
||
|
||
@actor.cache | ||
def load_model(state: int) -> callable: | ||
sleep(4) # simulate model loading | ||
return lambda value: state + value | ||
|
||
|
||
@actor.task | ||
def evaluate(value: int, state: int) -> int: | ||
model = load_model(state=state) | ||
return model(value) | ||
|
||
|
||
@union.workflow | ||
def wf(init_value: int = 1, state: int = 3) -> int: | ||
out = evaluate(value=init_value, state=state) | ||
out = evaluate(value=out, state=state) | ||
out = evaluate(value=out, state=state) | ||
out = evaluate(value=out, state=state) | ||
return out |
53 changes: 53 additions & 0 deletions
53
user_guide/core_concepts/actors/serverless/caching_custom_object.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from functools import partial | ||
from pathlib import Path | ||
from time import sleep | ||
|
||
import union | ||
|
||
actor = union.ActorEnvironment( | ||
name="my-actor", | ||
replica_count=2, | ||
) | ||
|
||
|
||
class MyModel: | ||
"""Simple model that multiples value with model_state.""" | ||
|
||
def __init__(self, model_state: int): | ||
self.model_state = model_state | ||
|
||
def __call__(self, value: int): | ||
return self.model_state * value | ||
|
||
|
||
@task(container_image=image, cache=True, cache_version="v1") | ||
def create_model_state() -> union.FlyteFile: | ||
working_dir = Path(union.current_context().working_directory) | ||
model_state_path = working_dir / "model_state.txt" | ||
model_state_path.write_text("4") | ||
return model_state_path | ||
|
||
|
||
@actor.cache | ||
def load_model(model_state_path: union.FlyteFile) -> MyModel: | ||
# Simulate model loading time. This can take a long time | ||
# because the FlyteFile download is large, or when the | ||
# model is loaded onto the GPU. | ||
sleep(10) | ||
with model_state_path.open("r") as f: | ||
model_state = int(f.read()) | ||
|
||
return MyModel(model_state=model_state) | ||
|
||
|
||
@actor.task | ||
def inference(value: int, model_state_path: union.FlyteFile) -> int: | ||
model = load_model(model_state_path) | ||
return model(value) | ||
|
||
|
||
@workflow | ||
def run_inference(values: list[int] = list(range(20))) -> list[int]: | ||
model_state = create_model_state() | ||
inference_ = partial(inference, model_state_path=model_state) | ||
return union.map_task(inference_)(value=values) |
Oops, something went wrong.