From 83172e9a993c784fe3f6c80de1a22ae78638d353 Mon Sep 17 00:00:00 2001 From: chyundunovDatamonsters Date: Mon, 18 Nov 2024 13:36:23 +0700 Subject: [PATCH] Adding files to deploy CodeGen application on AMD GPU (#1130) Signed-off-by: Chingis Yundunov Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- CodeGen/docker_compose/amd/gpu/rocm/README.md | 126 +++++++++++++ .../docker_compose/amd/gpu/rocm/compose.yaml | 78 ++++++++ .../docker_compose/amd/gpu/rocm/set_env.sh | 37 ++++ CodeGen/tests/test_compose_on_rocm.sh | 169 ++++++++++++++++++ 4 files changed, 410 insertions(+) create mode 100644 CodeGen/docker_compose/amd/gpu/rocm/README.md create mode 100644 CodeGen/docker_compose/amd/gpu/rocm/compose.yaml create mode 100644 CodeGen/docker_compose/amd/gpu/rocm/set_env.sh create mode 100644 CodeGen/tests/test_compose_on_rocm.sh diff --git a/CodeGen/docker_compose/amd/gpu/rocm/README.md b/CodeGen/docker_compose/amd/gpu/rocm/README.md new file mode 100644 index 000000000..dd01fdd0d --- /dev/null +++ b/CodeGen/docker_compose/amd/gpu/rocm/README.md @@ -0,0 +1,126 @@ +# Build and deploy CodeGen Application on AMD GPU (ROCm) + +## Build images + +### Build the LLM Docker Image + +```bash +### Cloning repo +git clone https://github.com/opea-project/GenAIComps.git +cd GenAIComps + +### Build Docker image +docker build -t opea/llm-tgi:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/llms/text-generation/tgi/Dockerfile . +``` + +### Build the MegaService Docker Image + +```bash +### Cloning repo +git clone https://github.com/opea-project/GenAIExamples +cd GenAIExamples/CodeGen + +### Build Docker image +docker build -t opea/codegen:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f Dockerfile . +``` + +### Build the UI Docker Image + +```bash +cd GenAIExamples/CodeGen/ui +### Build UI Docker image +docker build -t opea/codegen-ui:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f ./docker/Dockerfile . + +### Build React UI Docker image (React UI allows you to use file uploads) +docker build --no-cache -t opea/codegen-react-ui:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f ./docker/Dockerfile.react . +``` + +It is recommended to use the React UI as it works for downloading files. The use of React UI is set in the Docker Compose file + +## Deploy CodeGen Application + +### Features of Docker compose for AMD GPUs + +1. Added forwarding of GPU devices to the container TGI service with instructions: + +```yaml +shm_size: 1g +devices: + - /dev/kfd:/dev/kfd + - /dev/dri/:/dev/dri/ +cap_add: + - SYS_PTRACE +group_add: + - video +security_opt: + - seccomp:unconfined +``` + +In this case, all GPUs are thrown. To reset a specific GPU, you need to use specific device names cardN and renderN. + +For example: + +```yaml +shm_size: 1g +devices: + - /dev/kfd:/dev/kfd + - /dev/dri/card0:/dev/dri/card0 + - /dev/dri/render128:/dev/dri/render128 +cap_add: + - SYS_PTRACE +group_add: + - video +security_opt: + - seccomp:unconfined +``` + +To find out which GPU device IDs cardN and renderN correspond to the same GPU, use the GPU driver utility + +### Go to the directory with the Docker compose file + +```bash +cd GenAIExamples/CodeGen/docker_compose/amd/gpu/rocm +``` + +### Set environments + +In the file "GenAIExamples/CodeGen/docker_compose/amd/gpu/rocm/set_env.sh " it is necessary to set the required values. Parameter assignments are specified in the comments for each variable setting command + +```bash +chmod +x set_env.sh +. set_env.sh +``` + +### Run services + +``` +docker compose up -d +``` + +# Validate the MicroServices and MegaService + +## Validate TGI service + +```bash +curl http://${HOST_IP}:${CODEGEN_TGI_SERVICE_PORT}/generate \ + -X POST \ + -d '{"inputs":"Implement a high-level API for a TODO list application. The API takes as input an operation request and updates the TODO list in place. If the request is invalid, raise an exception.","parameters":{"max_new_tokens":256, "do_sample": true}}' \ + -H 'Content-Type: application/json' +``` + +## Validate LLM service + +```bash +curl http://${HOST_IP}:${CODEGEN_LLM_SERVICE_PORT}/v1/chat/completions\ + -X POST \ + -d '{"query":"Implement a high-level API for a TODO list application. The API takes as input an operation request and updates the TODO list in place. If the request is invalid, raise an exception.","max_tokens":256,"top_k":10,"top_p":0.95,"typical_p":0.95,"temperature":0.01,"repetition_penalty":1.03,"streaming":true}' \ + -H 'Content-Type: application/json' +``` + +## Validate MegaService + +```bash +curl http://${HOST_IP}:${CODEGEN_BACKEND_SERVICE_PORT}/v1/codegen -H "Content-Type: application/json" -d '{ + "messages": "Implement a high-level API for a TODO list application. The API takes as input an operation request and updates the TODO list in place. If the request is invalid, raise an exception." + }' +``` diff --git a/CodeGen/docker_compose/amd/gpu/rocm/compose.yaml b/CodeGen/docker_compose/amd/gpu/rocm/compose.yaml new file mode 100644 index 000000000..cf7d2369c --- /dev/null +++ b/CodeGen/docker_compose/amd/gpu/rocm/compose.yaml @@ -0,0 +1,78 @@ +# Copyright (C) 2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +services: + codegen-tgi-service: + image: ghcr.io/huggingface/text-generation-inference:2.3.1-rocm + container_name: codegen-tgi-service + ports: + - "${CODEGEN_TGI_SERVICE_PORT:-8028}:80" + volumes: + - "/var/lib/GenAI/data:/data" + environment: + no_proxy: ${no_proxy} + http_proxy: ${http_proxy} + https_proxy: ${https_proxy} + HUGGING_FACE_HUB_TOKEN: ${CODEGEN_HUGGINGFACEHUB_API_TOKEN} + HUGGINGFACEHUB_API_TOKEN: ${CODEGEN_HUGGINGFACEHUB_API_TOKEN} + shm_size: 1g + devices: + - /dev/kfd:/dev/kfd + - /dev/dri/:/dev/dri/ + cap_add: + - SYS_PTRACE + group_add: + - video + security_opt: + - seccomp:unconfined + ipc: host + command: --model-id ${CODEGEN_LLM_MODEL_ID} --max-input-length 1024 --max-total-tokens 2048 + codegen-llm-server: + image: ${REGISTRY:-opea}/llm-tgi:${TAG:-latest} + container_name: codegen-llm-server + depends_on: + - codegen-tgi-service + ports: + - "${CODEGEN_LLM_SERVICE_PORT:-9000}:9000" + ipc: host + environment: + no_proxy: ${no_proxy} + http_proxy: ${http_proxy} + https_proxy: ${https_proxy} + TGI_LLM_ENDPOINT: "http://codegen-tgi-service" + HUGGINGFACEHUB_API_TOKEN: ${CODEGEN_HUGGINGFACEHUB_API_TOKEN} + restart: unless-stopped + codegen-backend-server: + image: ${REGISTRY:-opea}/codegen:${TAG:-latest} + container_name: codegen-backend-server + depends_on: + - codegen-llm-server + ports: + - "${CODEGEN_BACKEND_SERVICE_PORT:-7778}:7778" + environment: + no_proxy: ${no_proxy} + https_proxy: ${https_proxy} + http_proxy: ${http_proxy} + MEGA_SERVICE_HOST_IP: ${CODEGEN_MEGA_SERVICE_HOST_IP} + LLM_SERVICE_HOST_IP: "codegen-llm-server" + ipc: host + restart: always + codegen-ui-server: + image: ${REGISTRY:-opea}/codegen-ui:${TAG:-latest} + container_name: codegen-ui-server + depends_on: + - codegen-backend-server + ports: + - "${CODEGEN_UI_SERVICE_PORT:-5173}:5173" + environment: + no_proxy: ${no_proxy} + https_proxy: ${https_proxy} + http_proxy: ${http_proxy} + BASIC_URL: ${CODEGEN_BACKEND_SERVICE_URL} + BACKEND_SERVICE_ENDPOINT: ${CODEGEN_BACKEND_SERVICE_URL} + ipc: host + restart: always + +networks: + default: + driver: bridge diff --git a/CodeGen/docker_compose/amd/gpu/rocm/set_env.sh b/CodeGen/docker_compose/amd/gpu/rocm/set_env.sh new file mode 100644 index 000000000..505c0d018 --- /dev/null +++ b/CodeGen/docker_compose/amd/gpu/rocm/set_env.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +# Copyright (C) 2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +### The IP address or domain name of the server on which the application is running +export HOST_IP=direct-supercomputer1.powerml.co + +### The port of the TGI service. On this port, the TGI service will accept connections +export CODEGEN_TGI_SERVICE_PORT=8028 + +### A token for accessing repositories with models +export CODEGEN_HUGGINGFACEHUB_API_TOKEN=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 + +### Model ID +export CODEGEN_LLM_MODEL_ID="Qwen/Qwen2.5-Coder-7B-Instruct" + +### The port of the LLM service. On this port, the LLM service will accept connections +export CODEGEN_LLM_SERVICE_PORT=9000 + +### The endpoint of the TGI service to which requests to this service will be sent (formed from previously set variables) +export CODEGEN_TGI_LLM_ENDPOINT="http://${HOST_IP}:${CODEGEN_TGI_SERVICE_PORT}" + +### The IP address or domain name of the server for CodeGen MegaService +export CODEGEN_MEGA_SERVICE_HOST_IP=${HOST_IP} + +### The port for CodeGen backend service +export CODEGEN_BACKEND_SERVICE_PORT=18150 + +### The URL of CodeGen backend service, used by the frontend service +export CODEGEN_BACKEND_SERVICE_URL="http://${HOST_IP}:${CODEGEN_BACKEND_SERVICE_PORT}/v1/codegen" + +### The endpoint of the LLM service to which requests to this service will be sent +export CODEGEN_LLM_SERVICE_HOST_IP=${HOST_IP} + +### The CodeGen service UI port +export CODEGEN_UI_SERVICE_PORT=18151 diff --git a/CodeGen/tests/test_compose_on_rocm.sh b/CodeGen/tests/test_compose_on_rocm.sh new file mode 100644 index 000000000..f1182f8db --- /dev/null +++ b/CodeGen/tests/test_compose_on_rocm.sh @@ -0,0 +1,169 @@ +#!/bin/bash + +# Copyright (C) 2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +set -xe +IMAGE_REPO=${IMAGE_REPO:-"opea"} +IMAGE_TAG=${IMAGE_TAG:-"latest"} +echo "REGISTRY=IMAGE_REPO=${IMAGE_REPO}" +echo "TAG=IMAGE_TAG=${IMAGE_TAG}" +export REGISTRY=${IMAGE_REPO} +export TAG=${IMAGE_TAG} + +WORKPATH=$(dirname "$PWD") +LOG_PATH="$WORKPATH/tests" +ip_address=$(hostname -I | awk '{print $1}') + +function build_docker_images() { + cd $WORKPATH/docker_image_build + git clone https://github.com/opea-project/GenAIComps.git && cd GenAIComps && git checkout "${opea_branch:-"main"}" && cd ../ + + echo "Build all the images with --no-cache, check docker_image_build.log for details..." + service_list="codegen codegen-ui llm-tgi" + docker compose -f build.yaml build ${service_list} --no-cache > ${LOG_PATH}/docker_image_build.log + + docker pull ghcr.io/huggingface/text-generation-inference:2.4.0-intel-cpu + docker images && sleep 1s +} + +function start_services() { + cd $WORKPATH/docker_compose/amd/gpu/rocm/ + + export CODEGEN_LLM_MODEL_ID="Qwen/Qwen2.5-Coder-7B-Instruct" + export CODEGEN_TGI_SERVICE_PORT=8028 + export CODEGEN_TGI_LLM_ENDPOINT="http://${ip_address}:${CODEGEN_TGI_SERVICE_PORT}" + export CODEGEN_LLM_SERVICE_PORT=9000 + export CODEGEN_HUGGINGFACEHUB_API_TOKEN=${HUGGINGFACEHUB_API_TOKEN} + export CODEGEN_MEGA_SERVICE_HOST_IP=${ip_address} + export CODEGEN_LLM_SERVICE_HOST_IP=${ip_address} + export CODEGEN_BACKEND_SERVICE_PORT=7778 + export CODEGEN_BACKEND_SERVICE_URL="http://${ip_address}:${CODEGEN_BACKEND_SERVICE_PORT}/v1/codegen" + export CODEGEN_UI_SERVICE_PORT=5173 + + sed -i "s/backend_address/$ip_address/g" $WORKPATH/ui/svelte/.env + + # Start Docker Containers + docker compose up -d > ${LOG_PATH}/start_services_with_compose.log + + n=0 + until [[ "$n" -ge 100 ]]; do + docker logs codegen-tgi-service > ${LOG_PATH}/codegen_tgi_service_start.log + if grep -q Connected ${LOG_PATH}/codegen_tgi_service_start.log; then + break + fi + sleep 5s + n=$((n+1)) + done +} + +function validate_services() { + local URL="$1" + local EXPECTED_RESULT="$2" + local SERVICE_NAME="$3" + local DOCKER_NAME="$4" + local INPUT_DATA="$5" + + local HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X POST -d "$INPUT_DATA" -H 'Content-Type: application/json' "$URL") + if [ "$HTTP_STATUS" -eq 200 ]; then + echo "[ $SERVICE_NAME ] HTTP status is 200. Checking content..." + + local CONTENT=$(curl -s -X POST -d "$INPUT_DATA" -H 'Content-Type: application/json' "$URL" | tee ${LOG_PATH}/${SERVICE_NAME}.log) + + if echo "$CONTENT" | grep -q "$EXPECTED_RESULT"; then + echo "[ $SERVICE_NAME ] Content is as expected." + else + echo "[ $SERVICE_NAME ] Content does not match the expected result: $CONTENT" + docker logs ${DOCKER_NAME} >> ${LOG_PATH}/${SERVICE_NAME}.log + exit 1 + fi + else + echo "[ $SERVICE_NAME ] HTTP status is not 200. Received status was $HTTP_STATUS" + docker logs ${DOCKER_NAME} >> ${LOG_PATH}/${SERVICE_NAME}.log + exit 1 + fi + sleep 5s +} + +function validate_microservices() { + # tgi for llm service + validate_services \ + "${ip_address}:8028/generate" \ + "generated_text" \ + "codegen-tgi-service" \ + "codegen-tgi-service" \ + '{"inputs":"def print_hello_world():","parameters":{"max_new_tokens":256, "do_sample": true}}' + + # llm microservice + validate_services \ + "${ip_address}:9000/v1/chat/completions" \ + "data: " \ + "codegen-llm-server" \ + "codegen-llm-server" \ + '{"query":"def print_hello_world():"}' + +} + +function validate_megaservice() { + # Curl the Mega Service + validate_services \ + "${ip_address}:7778/v1/codegen" \ + "print" \ + "codegen-backend-server" \ + "codegen-backend-server" \ + '{"messages": "def print_hello_world():"}' + +} + +function validate_frontend() { + cd $WORKPATH/ui/svelte + local conda_env_name="OPEA_e2e" + export PATH=${HOME}/miniconda3/bin/:$PATH + if conda info --envs | grep -q "$conda_env_name"; then + echo "$conda_env_name exist!" + else + conda create -n ${conda_env_name} python=3.12 -y + fi + source activate ${conda_env_name} + + sed -i "s/localhost/$ip_address/g" playwright.config.ts + + conda install -c conda-forge nodejs -y + npm install && npm ci && npx playwright install --with-deps + node -v && npm -v && pip list + + exit_status=0 + npx playwright test || exit_status=$? + + if [ $exit_status -ne 0 ]; then + echo "[TEST INFO]: ---------frontend test failed---------" + exit $exit_status + else + echo "[TEST INFO]: ---------frontend test passed---------" + fi +} + + +function stop_docker() { + cd $WORKPATH/docker_compose/amd/gpu/rocm/ + docker compose stop && docker compose rm -f +} + +function main() { + + stop_docker + + if [[ "$IMAGE_REPO" == "opea" ]]; then build_docker_images; fi + start_services + + validate_microservices + validate_megaservice + validate_frontend + + stop_docker + echo y | docker system prune + cd $WORKPATH + +} + +main