-
Notifications
You must be signed in to change notification settings - Fork 35
ITEP-64780 weights uploader #225
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f300960
ITEP-64780
mgumowsk e98d273
download weights from s3 and url
mkrzyzop c2961ee
review changes
mgumowsk 75748af
format json file
mgumowsk 2171097
review change
mgumowsk 8a3830e
review changes
mgumowsk 4657186
Merge branch 'main' into mgumowsk/ITEP-64780-weights-uploader
mgumowsk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,4 @@ | ||
* | ||
!app | ||
!uv.lock | ||
!pyproject.toml |
This file contains hidden or 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 python:3.10-slim-bookworm AS base | ||
|
||
FROM base AS build | ||
|
||
ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy | ||
|
||
# Disable Python downloads, because we want to use the system interpreter | ||
# across both images. | ||
ENV UV_PYTHON_DOWNLOADS=0 | ||
|
||
# Copy the service dependencies | ||
WORKDIR /builder | ||
COPY --link --from=libs . ../libs | ||
|
||
WORKDIR /builder/weights_uploader/app | ||
|
||
COPY --link --from=ghcr.io/astral-sh/uv:0.6.12 /uv /bin/uv | ||
|
||
COPY --link app . | ||
|
||
RUN --mount=type=cache,target=/root/.cache/uv \ | ||
--mount=type=bind,source=uv.lock,target=uv.lock \ | ||
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \ | ||
uv venv --relocatable && \ | ||
uv sync --frozen --no-dev --no-editable | ||
|
||
FROM base AS runtime | ||
|
||
RUN apt-get update && \ | ||
apt-get install -y --no-install-recommends \ | ||
curl && \ | ||
apt-get clean && \ | ||
rm -rf /var/lib/apt/lists/* | ||
|
||
RUN useradd -l -u 10001 non-root && \ | ||
pip3 uninstall -y setuptools pip wheel && \ | ||
rm -rf /root/.cache/pip | ||
|
||
USER non-root | ||
|
||
# Copy the application from the builder | ||
COPY --link --from=build --chown=10001 /builder/weights_uploader/app /app | ||
|
||
# Place executables in the environment at the front of the path | ||
ENV PATH="/app/.venv/bin:$PATH" | ||
|
||
WORKDIR /app |
This file contains hidden or 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,6 @@ | ||
# Copyright (C) 2022-2025 Intel Corporation | ||
# LIMITED EDGE SOFTWARE DISTRIBUTION LICENSE | ||
|
||
include ../../../Makefile.shared-python | ||
|
||
DOCKER_BUILD_CONTEXT := --build-context libs=../../../libs |
Empty file.
119 changes: 119 additions & 0 deletions
119
platform/services/weights_uploader/app/pretrained_models/pretrained_models.py
This file contains hidden or 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,119 @@ | ||
# Copyright (C) 2022-2025 Intel Corporation | ||
# LIMITED EDGE SOFTWARE DISTRIBUTION LICENSE | ||
|
||
import hashlib | ||
import logging | ||
import os | ||
import shutil | ||
import urllib.error | ||
import urllib.request | ||
import zipfile | ||
from collections.abc import Callable | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
RETRIES = 5 | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def sha256sum(filepath: str): # noqa: ANN201, D103 | ||
sha256 = hashlib.sha256() | ||
with open(filepath, "rb") as f: | ||
while True: | ||
data = f.read(65536) | ||
if not data: | ||
break | ||
sha256.update(data) | ||
return sha256.hexdigest() | ||
|
||
|
||
def download_file(url: str, target_path: str, auto_unzip=True): # noqa: ANN001, ANN201, D103 | ||
logger.info(f"Downloading file: {url}") | ||
url_original_filename = os.path.basename(url) | ||
if "?" in url_original_filename: | ||
url_original_filename = url_original_filename.split("?")[0] | ||
|
||
target_dir_path = os.path.dirname(target_path) | ||
download_temp_target_path = os.path.join(target_dir_path, url_original_filename) | ||
|
||
with ( | ||
urllib.request.urlopen(url) as response, # noqa: S310 | ||
open(download_temp_target_path, "wb") as out_file, | ||
): | ||
shutil.copyfileobj(response, out_file) | ||
|
||
# do not use 'zipfile.is_zipfile'! | ||
# some '.pth' files are actually zip files and they should not be unzipped here | ||
if auto_unzip and download_temp_target_path.endswith(".zip"): | ||
with zipfile.ZipFile(download_temp_target_path) as zip_ref: | ||
files_in_zip = zip_ref.namelist() | ||
number_of_files_in_zip = len(files_in_zip) | ||
mgumowsk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if number_of_files_in_zip != 1: | ||
raise RuntimeError( | ||
f"Unexpected number of files: {number_of_files_in_zip}, expected: 1 in: {download_temp_target_path}" | ||
) | ||
zip_ref.extractall(target_dir_path) | ||
os.remove(download_temp_target_path) | ||
shutil.move(os.path.join(target_dir_path, files_in_zip[0]), target_path) | ||
elif os.path.dirname(download_temp_target_path) != os.path.dirname(target_path) or ( | ||
os.path.basename(download_temp_target_path) != os.path.basename(target_path) | ||
): | ||
shutil.move(download_temp_target_path, target_path) | ||
|
||
|
||
class MaxTriesExhausted(Exception): | ||
pass | ||
|
||
|
||
# no retry lib has been used here on purpose - to avoid installing additional libs | ||
def retry_call(call: Callable, retries: int = RETRIES, **kwargs): # noqa: ANN201, D103 | ||
for i in range(retries): | ||
logger.info(f"Try {i + 1}/{retries}") | ||
try: | ||
call(**kwargs) | ||
break | ||
except Exception: | ||
logger.exception(f"Failed try {i + 1}/{retries}") | ||
else: | ||
raise MaxTriesExhausted | ||
|
||
|
||
def download_pretrained_model(model_spec: dict, target_dir: str, weights_url: str | None = None): # noqa: ANN201, D103 | ||
model_external_url = model_spec["url"] | ||
target_path = model_spec["target"] | ||
auto_unzip = model_spec.get("unzip", True) | ||
sha_sum = model_spec.get("sha_sum") | ||
|
||
target_download_path = os.path.join(target_dir, os.path.basename(target_path)) | ||
if weights_url is not None: | ||
model_external_url = os.path.join(weights_url, os.path.basename(model_external_url)) | ||
|
||
if os.path.exists(target_download_path): | ||
if sha_sum is None: | ||
logger.warning(f"Model already existed: {target_download_path} but sha_sum is not specified") | ||
logger.warning(f"consider to add sha_sum to the model spec: {sha256sum(target_download_path)}") | ||
elif sha256sum(target_download_path) == sha_sum: | ||
logger.info(f"Model already downloaded: {target_download_path}") | ||
return | ||
else: | ||
logger.warning(f"Model already downloaded but SHA mismatch: {target_download_path}") | ||
logger.warning("Redownloading...") | ||
os.remove(target_download_path) | ||
|
||
try: | ||
retry_call( | ||
download_file, | ||
url=model_external_url, | ||
target_path=target_download_path, | ||
auto_unzip=auto_unzip, | ||
) | ||
except MaxTriesExhausted: | ||
raise | ||
|
||
# verify SHA | ||
if sha_sum is not None: | ||
received_sha = sha256sum(target_download_path) | ||
if sha_sum != received_sha: | ||
raise RuntimeError(f"Wrong SHA sum for: {target_download_path}. Expected: {sha_sum}, got: {received_sha}") | ||
logger.info("SHA match") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.