From 6f2b45a376e331e56fdcbf94e1fbc914497af317 Mon Sep 17 00:00:00 2001 From: "Stuart A. Siegel" Date: Fri, 2 Aug 2024 13:32:53 -0400 Subject: [PATCH 1/5] flatten directory structure --- services/inference/{tsfmservices => tsfminference}/__init__.py | 0 .../{tsfmservices/common => tsfminference}/constants.py | 0 .../{tsfmservices/config => tsfminference}/default_config.yml | 0 .../{tsfmservices/inference => tsfminference}/inference.py | 0 .../{tsfmservices/common => tsfminference}/logging.conf | 0 .../{tsfmservices/inference => tsfminference}/main.py | 0 .../{tsfmservices/inference => tsfminference}/payloads.py | 0 .../inference/{tsfmservices/common => tsfminference}/util.py | 0 services/inference/tsfmservices/common/__init__.py | 2 -- services/inference/tsfmservices/inference/__init__.py | 3 --- 10 files changed, 5 deletions(-) rename services/inference/{tsfmservices => tsfminference}/__init__.py (100%) rename services/inference/{tsfmservices/common => tsfminference}/constants.py (100%) rename services/inference/{tsfmservices/config => tsfminference}/default_config.yml (100%) rename services/inference/{tsfmservices/inference => tsfminference}/inference.py (100%) rename services/inference/{tsfmservices/common => tsfminference}/logging.conf (100%) rename services/inference/{tsfmservices/inference => tsfminference}/main.py (100%) rename services/inference/{tsfmservices/inference => tsfminference}/payloads.py (100%) rename services/inference/{tsfmservices/common => tsfminference}/util.py (100%) delete mode 100644 services/inference/tsfmservices/common/__init__.py delete mode 100644 services/inference/tsfmservices/inference/__init__.py diff --git a/services/inference/tsfmservices/__init__.py b/services/inference/tsfminference/__init__.py similarity index 100% rename from services/inference/tsfmservices/__init__.py rename to services/inference/tsfminference/__init__.py diff --git a/services/inference/tsfmservices/common/constants.py b/services/inference/tsfminference/constants.py similarity index 100% rename from services/inference/tsfmservices/common/constants.py rename to services/inference/tsfminference/constants.py diff --git a/services/inference/tsfmservices/config/default_config.yml b/services/inference/tsfminference/default_config.yml similarity index 100% rename from services/inference/tsfmservices/config/default_config.yml rename to services/inference/tsfminference/default_config.yml diff --git a/services/inference/tsfmservices/inference/inference.py b/services/inference/tsfminference/inference.py similarity index 100% rename from services/inference/tsfmservices/inference/inference.py rename to services/inference/tsfminference/inference.py diff --git a/services/inference/tsfmservices/common/logging.conf b/services/inference/tsfminference/logging.conf similarity index 100% rename from services/inference/tsfmservices/common/logging.conf rename to services/inference/tsfminference/logging.conf diff --git a/services/inference/tsfmservices/inference/main.py b/services/inference/tsfminference/main.py similarity index 100% rename from services/inference/tsfmservices/inference/main.py rename to services/inference/tsfminference/main.py diff --git a/services/inference/tsfmservices/inference/payloads.py b/services/inference/tsfminference/payloads.py similarity index 100% rename from services/inference/tsfmservices/inference/payloads.py rename to services/inference/tsfminference/payloads.py diff --git a/services/inference/tsfmservices/common/util.py b/services/inference/tsfminference/util.py similarity index 100% rename from services/inference/tsfmservices/common/util.py rename to services/inference/tsfminference/util.py diff --git a/services/inference/tsfmservices/common/__init__.py b/services/inference/tsfmservices/common/__init__.py deleted file mode 100644 index 4f85bd0b..00000000 --- a/services/inference/tsfmservices/common/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -# Copyright contributors to the TSFM project -# diff --git a/services/inference/tsfmservices/inference/__init__.py b/services/inference/tsfmservices/inference/__init__.py deleted file mode 100644 index 6c6c3760..00000000 --- a/services/inference/tsfmservices/inference/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -# Copyright contributors to the TSFM project -# -from .inference import InferenceRuntime From 19c72523292c75681fd1089cd5907bf530aa2dcc Mon Sep 17 00:00:00 2001 From: "Stuart A. Siegel" Date: Fri, 2 Aug 2024 13:33:20 -0400 Subject: [PATCH 2/5] fixes to imports, etc. --- services/inference/.dockerignore | 2 +- services/inference/Dockerfile | 28 +++++++++---------- services/inference/Makefile | 10 +++---- services/inference/README.md | 6 ++-- services/inference/pyproject.toml | 4 +-- services/inference/tsfminference/__init__.py | 16 +---------- services/inference/tsfminference/inference.py | 10 ++++--- services/inference/tsfminference/main.py | 6 ++-- services/inference/tsfminference/payloads.py | 2 +- 9 files changed, 36 insertions(+), 48 deletions(-) diff --git a/services/inference/.dockerignore b/services/inference/.dockerignore index ed4bba40..a44c199e 100644 --- a/services/inference/.dockerignore +++ b/services/inference/.dockerignore @@ -1,5 +1,5 @@ ** -!tsfmservices +!tsfminference !poetry.lock !pyproject.toml diff --git a/services/inference/Dockerfile b/services/inference/Dockerfile index 7a7f51d6..b058082a 100644 --- a/services/inference/Dockerfile +++ b/services/inference/Dockerfile @@ -10,10 +10,11 @@ RUN microdnf -y update && \ ENV POETRY_VIRTUALENVS_IN_PROJECT=1 -COPY tsfmservices* /tsfmservices/tsfmservices -COPY pyproject.toml /tsfmservices/ -COPY poetry.lock /tsfmservices/ -WORKDIR /tsfmservices +RUN mkdir /inference +COPY tsfminference/* /inference/tsfminference/ +COPY pyproject.toml /inference/ +COPY poetry.lock /inference/ +WORKDIR /inference RUN pip3.11 install poetry && poetry install FROM registry.access.redhat.com/ubi9/ubi-minimal:latest AS deploy @@ -22,20 +23,19 @@ RUN microdnf -y update && \ shadow-utils python3.11 && \ microdnf clean all -WORKDIR /tsfmservices +WORKDIR /inference -COPY --from=builder /tsfmservices/ /tsfmservices/ +COPY --from=builder /inference /inference -ENV VIRTUAL_ENV=/tsfmservices/.venv +ENV VIRTUAL_ENV=/inference/.venv ENV PATH="$VIRTUAL_ENV/bin:$PATH" ENV HF_HOME=/tmp -RUN groupadd --system tsfmservices --gid 1001 && \ - adduser --system --uid 1001 --gid 0 --groups tsfmservices \ - --create-home --home-dir /tsfmservices --shell /sbin/nologin \ - --comment "TSFMServices User" tsfmservices +RUN groupadd --system tsfminference --gid 1001 && \ + adduser --system --uid 1001 --gid 0 --groups tsfminference \ + --create-home --home-dir /inference --shell /sbin/nologin \ + --comment "tsfminference User" tsfminference -USER tsfmservices +USER tsfminference -# CMD ["python", "-m", "tsfmservices.inference.main"] -CMD ["python", "-m", "uvicorn","tsfmservices.inference.main:app", "--host", "0.0.0.0", "--port", "8000" ] \ No newline at end of file +CMD ["python", "-m", "uvicorn","tsfminference.main:app", "--host", "0.0.0.0", "--port", "8000" ] \ No newline at end of file diff --git a/services/inference/Makefile b/services/inference/Makefile index 9d515e20..26a11df7 100644 --- a/services/inference/Makefile +++ b/services/inference/Makefile @@ -1,18 +1,18 @@ CONTAINER_BUILDER ?= docker -# starts the inference service (used mainly for test cases) +# starts the tsfminference service (used mainly for test cases) start_service_local: - python -m tsfmservices.inference.main & + python -m tsfminference.main & sleep 10 stop_service_local: - pkill -f 'python.*tsfmservices.*' + pkill -f 'python.*tsfminference.*' sleep 10 image: - $(CONTAINER_BUILDER) build -t tsfmservices -f Dockerfile . + $(CONTAINER_BUILDER) build -t tsfminference -f Dockerfile . start_service_image: image - $(CONTAINER_BUILDER) run -p 8000:8000 -d --rm --name tsfmserver tsfmservices + $(CONTAINER_BUILDER) run -p 8000:8000 -d --rm --name tsfmserver tsfminference sleep 10 stop_service_image: $(CONTAINER_BUILDER) stop tsfmserver diff --git a/services/inference/README.md b/services/inference/README.md index 90683065..3f1c422a 100644 --- a/services/inference/README.md +++ b/services/inference/README.md @@ -45,11 +45,11 @@ CONTAINER_BUILDER= make image # e.g, CONTAINER_BUILDER=docker make image ``` -After a successful build you should have a local image named `tsfmservices:latest` +After a successful build you should have a local image named `tsfminference:latest` ```sh -(py311) ➜ tsfm-services git:(revised-build-system) ✗ docker images | grep tsfmservices | head -n 1 -tsfmservices latest df592dcb0533 46 seconds ago 1.49GB +(py311) ➜ tsfm-services git:(revised-build-system) ✗ docker images | grep tsfminference | head -n 1 +tsfminference latest df592dcb0533 46 seconds ago 1.49GB # some of the numeric and hash values on your machine could be different ``` diff --git a/services/inference/pyproject.toml b/services/inference/pyproject.toml index 7f10cf1f..ff0ede45 100644 --- a/services/inference/pyproject.toml +++ b/services/inference/pyproject.toml @@ -1,5 +1,5 @@ [tool.poetry] -name = "tsfmservices" +name = "tsfminference" version = "0.0.1" description = "Service layer for TSFM granite models." authors = ["IBM"] @@ -12,7 +12,7 @@ python = ">=3.10,<3.12" numpy = { version = "<2" } tsfm_public = { git = "https://github.com/IBM/tsfm.git", tag = "v0.2.5" } -# trying to pick up cpu version for inference +# trying to pick up cpu version for tsfminference # to make image smaller torch = { version = ">2,<3", source = "pytorch" } diff --git a/services/inference/tsfminference/__init__.py b/services/inference/tsfminference/__init__.py index 2f23ecf5..4312d1c0 100644 --- a/services/inference/tsfminference/__init__.py +++ b/services/inference/tsfminference/__init__.py @@ -26,23 +26,9 @@ "%(levelname)s:p-%(process)d:t-%(thread)d:%(filename)s:%(funcName)s:%(message)s", ) -# Do we have redis? - -try: - # Third Party - import redis - import rq - - HAVE_REDIS_BACKEND = True -except ImportError: - HAVE_REDIS_BACKEND = False - -TSFM_USE_KFTO_ASYNC_BACKEND = int(os.getenv("TSFM_USE_KFTO_ASYNC_BACKEND", "0")) == 1 -USE_REDIS_ASYNC_BACKEND = HAVE_REDIS_BACKEND and not TSFM_USE_KFTO_ASYNC_BACKEND - TSFM_ALLOW_LOAD_FROM_HF_HUB = int(os.getenv("TSFM_ALLOW_LOAD_FROM_HF_HUB", "1")) == 1 TSFM_CONFIG_FILE = os.getenv( "TSFM_CONFIG_FILE", - os.path.realpath(os.path.join(os.path.dirname(__file__), "config", "default_config.yml")), + os.path.realpath(os.path.join(os.path.dirname(__file__), "default_config.yml")), ) diff --git a/services/inference/tsfminference/inference.py b/services/inference/tsfminference/inference.py index a5b987a1..a60f6cd7 100644 --- a/services/inference/tsfminference/inference.py +++ b/services/inference/tsfminference/inference.py @@ -1,6 +1,8 @@ # Copyright contributors to the TSFM project # -"""Inference Runtime""" +"""Tsfminference Runtime""" + +print("In module __package__, __name__ ==", __package__, __name__) import datetime import logging @@ -11,11 +13,11 @@ from fastapi import APIRouter, HTTPException from tsfm_public import TimeSeriesForecastingPipeline, TimeSeriesPreprocessor -from tsfmservices import TSFM_ALLOW_LOAD_FROM_HF_HUB -from ..common.constants import API_VERSION -from ..common.util import load_config, load_model, register_config +from . import TSFM_ALLOW_LOAD_FROM_HF_HUB +from .constants import API_VERSION from .payloads import ForecastingInferenceInput, PredictOutput +from .util import load_config, load_model, register_config LOGGER = logging.getLogger(__file__) diff --git a/services/inference/tsfminference/main.py b/services/inference/tsfminference/main.py index a804682a..d7d559b2 100644 --- a/services/inference/tsfminference/main.py +++ b/services/inference/tsfminference/main.py @@ -7,13 +7,13 @@ import yaml from fastapi import FastAPI -from tsfmservices import ( +from . import ( TSFM_CONFIG_FILE, TSFM_PYTHON_LOGGING_FORMAT, TSFM_PYTHON_LOGGING_LEVEL, ) -from tsfmservices.common.constants import API_VERSION -from tsfmservices.inference import InferenceRuntime +from .constants import API_VERSION +from .inference import InferenceRuntime logging.basicConfig( diff --git a/services/inference/tsfminference/payloads.py b/services/inference/tsfminference/payloads.py index 36bb3385..8d212bc3 100644 --- a/services/inference/tsfminference/payloads.py +++ b/services/inference/tsfminference/payloads.py @@ -1,6 +1,6 @@ # Copyright contributors to the TSFM project # -"""Payload definitions for inference""" +"""Payload definitions for tsfminference""" from typing import Annotated, Any, Dict, List, Optional From 9f1e3edaf2f1cde627c03d5a2967ea83d3abe6e2 Mon Sep 17 00:00:00 2001 From: "Stuart A. Siegel" Date: Fri, 2 Aug 2024 13:34:31 -0400 Subject: [PATCH 3/5] comments --- .github/workflows/inference-services-test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/inference-services-test.yml b/.github/workflows/inference-services-test.yml index e5c19820..8bbbeb8c 100644 --- a/.github/workflows/inference-services-test.yml +++ b/.github/workflows/inference-services-test.yml @@ -1,7 +1,7 @@ # This workflow will install Python dependencies, run tests and lint with a variety of Python versions # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python -name: Inference Service Tests +name: Tsfminference Service Tests on: push: @@ -32,7 +32,7 @@ jobs: pip install poetry cd services/inference poetry install -n --with dev - - name: Test local server inference service with pytest + - name: Test local server tsfminference service with pytest run: | source .venv/bin/activate cd services/inference From f2b0de1457e027946d6f89b34a48bdf752516caa Mon Sep 17 00:00:00 2001 From: "Stuart A. Siegel" Date: Fri, 2 Aug 2024 13:44:53 -0400 Subject: [PATCH 4/5] fix ruff failed check --- services/inference/tsfminference/inference.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/services/inference/tsfminference/inference.py b/services/inference/tsfminference/inference.py index a60f6cd7..6de4554e 100644 --- a/services/inference/tsfminference/inference.py +++ b/services/inference/tsfminference/inference.py @@ -2,8 +2,6 @@ # """Tsfminference Runtime""" -print("In module __package__, __name__ ==", __package__, __name__) - import datetime import logging from pathlib import Path From 88a17488589349009253086d99c81e9b166311d0 Mon Sep 17 00:00:00 2001 From: "Stuart A. Siegel" Date: Fri, 2 Aug 2024 13:57:48 -0400 Subject: [PATCH 5/5] fix dir name --- services/inference/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/inference/pyproject.toml b/services/inference/pyproject.toml index ff0ede45..354d3783 100644 --- a/services/inference/pyproject.toml +++ b/services/inference/pyproject.toml @@ -4,7 +4,7 @@ version = "0.0.1" description = "Service layer for TSFM granite models." authors = ["IBM"] license = "https://github.com/ibm-granite/granite-tsfm/blob/main/LICENSE" -packages = [{ include = "tsfmservices/**/*.py" }] +packages = [{ include = "tsfminference/**/*.py" }] [tool.poetry.dependencies] # including 3.9 causes poetry lock to run forever