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

[Deploy] Download template from s3; Using home sign. #1957

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions python/fedml/api/modules/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import shutil
import yaml

import urllib.request
import zipfile

from fedml.computing.scheduler.model_scheduler.device_model_cards import FedMLModelCards
from fedml.api.modules.utils import fedml_login
from fedml.computing.scheduler.comm_utils.security_utils import get_api_key, save_api_key
Expand Down Expand Up @@ -44,27 +47,53 @@ def create(name: str, model: str = None, model_config: str = None) -> bool:


def create_from_hf(name: str, model: str = None) -> bool:
"""
Pull template from s3
[Deprecated Solution] Packaging the template to whl file, i.e.
# setup.py
package_data={
"fedml.serving": ["**/*.sh", "**/*.yaml", "**/*.json", "**/*.txt", "**/*.md", "**/*.jinja"],
},
# fedml/api/modules/model.py
hf_templ_fd_src = os.path.join(os.path.dirname(__file__), "..", "..", "serving", "templates", "hf_template")
dst_parent_fd = os.path.join(os.path.expanduser("~"), ".fedml","fedml-model-client", "fedml", "hf_model_from_template")
"""
templ_zip_url = "https://fedml-deploy-template.s3.us-west-2.amazonaws.com/hf_template.zip"
hf_templ_parent_fd = os.path.join(os.path.expanduser("~"), ".fedml", "fedml-model-client", "fedml")
hf_templ_fd = os.path.join(hf_templ_parent_fd, "hf_template")

if os.path.exists(hf_templ_fd):
shutil.rmtree(hf_templ_fd)
os.makedirs(hf_templ_fd)

print(f"Downloading the template from {templ_zip_url} ... to {hf_templ_parent_fd}")
urllib.request.urlretrieve(templ_zip_url, os.path.join(hf_templ_parent_fd, "hf_template.zip"))

with zipfile.ZipFile(os.path.join(hf_templ_parent_fd, "hf_template.zip"), 'r') as zip_ref:
zip_ref.extractall(hf_templ_parent_fd)

# Fulfill the template to a new model card that is deployable
dst_parent_fd = os.path.join(os.path.expanduser("~"), ".fedml",
"fedml-model-client", "fedml", "hf_model_from_template")
if not os.path.exists(dst_parent_fd):
os.makedirs(dst_parent_fd)

dst_fd = os.path.join(dst_parent_fd, name)
if os.path.exists(dst_fd):
shutil.rmtree(dst_fd)
shutil.copytree(hf_templ_fd_src, dst_fd)
shutil.rmtree(dst_fd)

print(f"Copying the template from {hf_templ_fd} ... to {dst_fd}")
shutil.copytree(hf_templ_fd, dst_fd)

config_file = os.path.join(dst_fd, "config.yaml")
with open(config_file, 'r') as file:
config = yaml.safe_load(file)
# Change the hf_model_name in the config.yaml to model
config["environment_variables"]["MODEL_NAME_OR_PATH"] = model
config["environment_variables"]["MODEL_NAME_OR_PATH"] = model # Change the hf_model_name in the config.yaml
with open(config_file, 'w') as file:
yaml.dump(config, file)

res = create(name, model_config=config_file)
if res:
print(f"Model source code (generated from template {hf_templ_fd_src}) is located at {dst_fd}")
print(f"Model source code (generated from template {hf_templ_fd}) is located at {dst_fd}")
return res


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ def start_deployment(end_point_id, end_point_name, model_id, model_version,
"mode": "rw"
}
environment["MAIN_ENTRY"] = relative_entry

if not enable_custom_image:
# For some image, the default user is root. Unified to fedml.
environment["HOME"] = "/home/fedml"
environment["BOOTSTRAP_DIR"] = dst_bootstrap_dir
environment["FEDML_CURRENT_RUN_ID"] = end_point_id
environment["FEDML_CURRENT_EDGE_ID"] = edge_id
Expand Down
2 changes: 1 addition & 1 deletion python/fedml/serving/templates/hf_template/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ endpoint_api_type: "text2text_llm_openai_chat_completions"

# set environment variables below
environment_variables:
HF_HOME: "/home/fedml/fedml_serving/model_and_config"
HF_HOME: "~/fedml_serving/model_and_config"
# HUGGING_FACE_HUB_TOKEN: "<your hugging face token>" # this is required for private hugging face models
MODEL_NAME_OR_PATH: "mistralai/Mistral-7B-Instruct-v0.1" # model name (hugging face ID) or path
MODEL_DTYPE: "auto"
Expand Down
Loading