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

FEAT: Refactor device related code and add initial Intel GPU support #968

Merged
merged 5 commits into from
Feb 21, 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
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ all =
orjson
auto-gptq ; sys_platform!='darwin'
optimum
intel =
torch==2.1.0a0
intel_extension_for_pytorch==2.1.10+xpu
ggml =
llama-cpp-python>=0.2.25
ctransformers
Expand Down
6 changes: 6 additions & 0 deletions xinference/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
__version__ = _version.get_versions()["version"]


try:
import intel_extension_for_pytorch # noqa: F401
except:
pass


def _install():
from xoscar.backends.router import Router

Expand Down
176 changes: 110 additions & 66 deletions xinference/api/restful_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,11 @@ def serve(self, logging_conf: Optional[dict] = None):
"/v1/ui/{model_uid}",
self.build_gradio_interface,
methods=["POST"],
dependencies=[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/token", self.login_for_access_token, methods=["POST"]
Expand All @@ -246,176 +248,218 @@ def serve(self, logging_conf: Optional[dict] = None):
"/v1/models/instances",
self.get_instance_info,
methods=["GET"],
dependencies=[Security(self._auth_service, scopes=["models:list"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:list"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/models/{model_type}/{model_name}/versions",
self.get_model_versions,
methods=["GET"],
dependencies=[Security(self._auth_service, scopes=["models:list"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:list"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/models",
self.list_models,
methods=["GET"],
dependencies=[Security(self._auth_service, scopes=["models:list"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:list"])]
if self.is_authenticated()
else None
),
)

self._router.add_api_route(
"/v1/models/{model_uid}",
self.describe_model,
methods=["GET"],
dependencies=[Security(self._auth_service, scopes=["models:list"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:list"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/models/{model_uid}/events",
self.get_model_events,
methods=["GET"],
dependencies=[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/models/instance",
self.launch_model_by_version,
methods=["POST"],
dependencies=[Security(self._auth_service, scopes=["models:start"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:start"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/models",
self.launch_model,
methods=["POST"],
dependencies=[Security(self._auth_service, scopes=["models:start"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:start"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/experimental/speculative_llms",
self.launch_speculative_llm,
methods=["POST"],
dependencies=[Security(self._auth_service, scopes=["models:start"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:start"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/models/{model_uid}",
self.terminate_model,
methods=["DELETE"],
dependencies=[Security(self._auth_service, scopes=["models:stop"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:stop"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/completions",
self.create_completion,
methods=["POST"],
response_model=Completion,
dependencies=[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/embeddings",
self.create_embedding,
methods=["POST"],
dependencies=[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/rerank",
self.rerank,
methods=["POST"],
dependencies=[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/audio/transcriptions",
self.create_transcriptions,
methods=["POST"],
dependencies=[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/audio/translations",
self.create_translations,
methods=["POST"],
dependencies=[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/images/generations",
self.create_images,
methods=["POST"],
response_model=ImageList,
dependencies=[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/images/variations",
self.create_variations,
methods=["POST"],
response_model=ImageList,
dependencies=[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/chat/completions",
self.create_chat_completion,
methods=["POST"],
response_model=ChatCompletion,
dependencies=[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:read"])]
if self.is_authenticated()
else None
),
)

# for custom models
self._router.add_api_route(
"/v1/model_registrations/{model_type}",
self.register_model,
methods=["POST"],
dependencies=[Security(self._auth_service, scopes=["models:register"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:register"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/model_registrations/{model_type}/{model_name}",
self.unregister_model,
methods=["DELETE"],
dependencies=[Security(self._auth_service, scopes=["models:unregister"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:unregister"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/model_registrations/{model_type}",
self.list_model_registrations,
methods=["GET"],
dependencies=[Security(self._auth_service, scopes=["models:list"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:list"])]
if self.is_authenticated()
else None
),
)
self._router.add_api_route(
"/v1/model_registrations/{model_type}/{model_name}",
self.get_model_registrations,
methods=["GET"],
dependencies=[Security(self._auth_service, scopes=["models:list"])]
if self.is_authenticated()
else None,
dependencies=(
[Security(self._auth_service, scopes=["models:list"])]
if self.is_authenticated()
else None
),
)

# Clear the global Registry for the MetricsMiddleware, or
Expand Down
5 changes: 3 additions & 2 deletions xinference/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

logger = logging.getLogger(__name__)

from ..device_utils import empty_cache
from .utils import json_dumps, log_async

try:
Expand Down Expand Up @@ -130,7 +131,7 @@ async def __pre_destroy__(self):
try:
import gc

import torch
import torch # noqa: F401
except ImportError:
error_message = "Failed to import module 'torch'"
installation_guide = [
Expand All @@ -141,7 +142,7 @@ async def __pre_destroy__(self):

del self._model
gc.collect()
torch.cuda.empty_cache()
empty_cache()

def __init__(
self,
Expand Down
6 changes: 3 additions & 3 deletions xinference/core/supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ async def get_builtin_families() -> Dict[str, List[str]]:
}

async def get_devices_count(self) -> int:
from ..utils import cuda_count
from ..device_utils import gpu_count

if self.is_local_deployment():
return cuda_count()
# distributed deployment, choose a worker and return its cuda_count.
return gpu_count()
# distributed deployment, choose a worker and return its device_count.
# Assume that each worker has the same count of cards.
worker_ref = await self._choose_worker()
return await worker_ref.get_devices_count()
Expand Down
Loading
Loading