Commit 34816b3b authored by svc-mlops's avatar svc-mlops

Khởi tạo classify_topic (mô phỏng) v1.0.0

parents
Pipeline #1 failed with stages
in 11 minutes
File added
File added
File added
File added
File added
File added
# Mô phỏng CI hiện tại của bên serving (sprint 1 giữ nguyên luồng này).
# GitLab 11.7.5: chỉ dùng only / except / when, không dùng rules / needs / workflow.
image: k3d-nexus:5000/ci-base:1.0.0
variables:
MINIO_PATH: "models/hieuhm18/classify_topic"
stages:
- validate
- publish
- deploy-uat
- smoke-test
- promote-prod
validate:
stage: validate
script:
- python3 scripts/validate_repo.py
- python3 -m pytest -q tests/
publish:
stage: publish
only:
- master
script:
- export VERSION_NAME=$(tr -d '[:space:]' < VERSION)
- test -n "$VERSION_NAME"
- echo "Version = v${VERSION_NAME} (commit ${CI_COMMIT_SHORT_SHA})"
- . scripts/minio_env.sh
- python3 scripts/package_runtime.py "v${VERSION_NAME}" staging
- echo "$CI_COMMIT_SHORT_SHA" > staging/SOURCE_COMMIT
- EXIST=$(mc cat "myminio/${MINIO_PATH}/v${VERSION_NAME}/SOURCE_COMMIT" 2>/dev/null || true)
- if [ -n "$EXIST" ] && [ "$EXIST" != "$CI_COMMIT_SHORT_SHA" ]; then echo "v${VERSION_NAME} đã có trên MinIO từ commit $EXIST"; exit 1; fi
- mc mirror --overwrite staging/ "myminio/${MINIO_PATH}/v${VERSION_NAME}/"
deploy-uat:
stage: deploy-uat
only:
- master
script:
- export VERSION_NAME=$(tr -d '[:space:]' < VERSION)
- python3 scripts/set_version.py deploy/classify-topic/overlays/uat "v${VERSION_NAME}"
- bash scripts/commit_version.sh uat "v${VERSION_NAME}" || echo "(không commit được bump, vẫn apply)"
- bash scripts/kretry.sh kubectl apply -k deploy/classify-topic/overlays/uat
- bash scripts/kretry.sh kubectl -n uat rollout status deployment/classify-topic-uat --timeout=600s
smoke-test:
stage: smoke-test
only:
- master
script:
- SVC_IP=$(bash scripts/kretry.sh kubectl -n uat get svc classify-topic-uat -o jsonpath='{.spec.clusterIP}')
- echo "Service UAT = $SVC_IP"
- python3 scripts/smoke_test.py "http://${SVC_IP}:8200/v2/models/ensemble_PhoBERT/infer" 2
promote-prod:
stage: promote-prod
only:
- master
when: manual
script:
- export VERSION_NAME=$(tr -d '[:space:]' < VERSION)
- python3 scripts/set_version.py deploy/classify-topic/overlays/prod "v${VERSION_NAME}"
- bash scripts/commit_version.sh prod "v${VERSION_NAME}" || echo "(không commit được bump, vẫn apply)"
- bash scripts/kretry.sh kubectl apply -k deploy/classify-topic/overlays/prod
- bash scripts/kretry.sh kubectl -n ocr rollout status deployment/classify-topic --timeout=900s
# classify_topic (bản mô phỏng)
Repo này được đẩy lên GitLab mô phỏng làm repo `ocr_ai_sme/classify_topic`.
- `model_repository/`: ensemble Triton `preprocess_PhoBERT → PhoBERT → postprocess_PhoBERT`. Model nhỏ thay PhoBERT, cây nhãn 4/12/24/48.
- `deploy/classify-topic/`: Kustomize base + overlay `uat` (ns `uat`), `prod` (ns `ocr`).
- `.gitlab-ci.yml`: CI hiện tại của bên serving (validate → publish → deploy-uat → smoke-test → promote-prod bấm tay).
- `dags/`: DAG `train_pipeline` cho Airflow trên cụm vận hành (sprint 1). Airflow kéo thư mục này bằng git-sync.
- `scripts/`, `tests/`: kiểm repo, đóng gói, bump version, smoke test.
"""Client gọi API bước trên máy GPU 106 (cùng tên hàm với module chatbot_api mà DAG tạm đang dùng).
Cấu hình qua biến môi trường: MOCK106_URL, MOCK106_TOKEN.
"""
from __future__ import annotations
import os
import time
import requests
from airflow.exceptions import AirflowException
def _base() -> str:
return os.environ.get("MOCK106_URL", "http://train106.sim:8000").rstrip("/")
def _headers() -> dict:
return {"X-API-Key": os.environ.get("MOCK106_TOKEN", "")}
def check_service() -> dict:
r = requests.get(f"{_base()}/health", timeout=15)
r.raise_for_status()
info = r.json()
print(f"106: {info}")
return info
def trigger_step(step: str, bot_id: str, dataset: str, config_overrides: dict | None = None,
cancel_running: bool = False) -> str:
body = {"step": step, "bot_id": bot_id, "dataset": dataset,
"config_overrides": config_overrides, "cancel_running": cancel_running}
r = requests.post(f"{_base()}/jobs", json=body, headers=_headers(), timeout=120)
if r.status_code == 409:
raise AirflowException(f"106 đang bận: {r.json().get('detail')}. Đặt cancel_running=true để huỷ job cũ")
r.raise_for_status()
jid = r.json()["job_id"]
print(f"đã gửi bước {step} lên 106: job {jid}")
return jid
def get_job_log(job_id: str) -> str:
r = requests.get(f"{_base()}/jobs/{job_id}/log", headers=_headers(), timeout=30)
r.raise_for_status()
return r.text
def wait_job(job_id: str, timeout_sec: int, poll_sec: int = 5) -> dict:
deadline = time.time() + timeout_sec
printed = 0
while time.time() < deadline:
r = requests.get(f"{_base()}/jobs/{job_id}", headers=_headers(), timeout=30)
r.raise_for_status()
info = r.json()
lines = get_job_log(job_id).splitlines()
for line in lines[printed:]:
print(f" 106| {line}")
printed = len(lines)
if info["status"] == "succeeded":
return info
if info["status"] in ("failed", "cancelled"):
raise AirflowException(f"job {job_id} ({info['step']}) {info['status']}: {info.get('error')}")
time.sleep(poll_sec)
raise AirflowException(f"job {job_id} quá {timeout_sec}s")
def verify_paths(paths: list[str]) -> dict:
r = requests.post(f"{_base()}/verify", json={"paths": paths}, headers=_headers(), timeout=60)
r.raise_for_status()
return r.json()
def download_artifact(name: str, dest: str, bot_id: str) -> int:
os.makedirs(os.path.dirname(dest) or ".", exist_ok=True)
with requests.get(f"{_base()}/artifacts/{name}", params={"bot_id": bot_id},
headers=_headers(), stream=True, timeout=600) as r:
r.raise_for_status()
with open(dest, "wb") as f:
for chunk in r.iter_content(1 << 20):
f.write(chunk)
return os.path.getsize(dest)
"""DAG train_pipeline, sprint 1: chuyển DAG tạm chatbot_train_full sang cụm vận hành.
check_106 -> train -> export_onnx -> evaluate -> gate -> package_tokenizer -> package_json
-> verify -> register (MLflow, mới) -> push_gitlab -> CI cũ của bên serving
So với DAG tạm:
- Giữ các bước gọi API máy 106, override config, cancel_running, và logic cổng chặn cũ
(Fail so với hôm trước hoặc dưới sàn -> REJECT; dưới mục tiêu -> cảnh báo).
- Thêm register: ghi MLflow (params, điểm, dataset, mã job 106, model.onnx + tokenizer),
tạo model version và gắn alias "candidate".
- push_gitlab: như task CI_CD cũ (đẩy model.onnx + 4 tokenizer, bump VERSION, push master),
nhưng clone mới mỗi lần và dùng tài khoản dịch vụ; ghi version + commit ngược lại MLflow.
Cấu hình qua biến môi trường: MOCK106_URL, MOCK106_TOKEN, MLFLOW_TRACKING_URI,
GITLAB_URL, GITLAB_REPO, GITLAB_USER, GITLAB_TOKEN.
"""
from __future__ import annotations
import json
import os
import re
import subprocess
import tempfile
import pendulum
from airflow.exceptions import AirflowException
from airflow.models.param import Param
from airflow.sdk import dag, task
from chatbot_api import (check_service, download_artifact, get_job_log,
trigger_step, verify_paths, wait_job)
TIMEOUT_NLP = 12 * 3600
TIMEOUT_LIGHT = 4 * 3600
STEP_TASKS = ("train", "export_onnx", "package_tokenizer", "package_json")
PUSH_FILES = {
"model.onnx": "model_repository/PhoBERT/1/model.onnx",
**{f"label_level{k}_tokenizer.json":
f"model_repository/postprocess_PhoBERT/tokenizer/{{bot_id}}/label_level{k}_tokenizer.json"
for k in (1, 2, 3, 4)},
}
_SCRIPT_STATUS_RE = re.compile(r"So với ngày hôm trước[^:\n]*:\s*(Pass|Fail)")
CFG_PARAMS = {"num_epochs": "NUM_EPOCHS", "start_epoch": "START_EPOCH", "batch_size": "BATCH_SIZE",
"max_seq_length": "MAX_SEQ_LENGTH", "lr": "LR"}
def _overrides(p: dict) -> dict | None:
ovr: dict = {}
for pk, ck in CFG_PARAMS.items():
raw = str(p.get(pk) or "").strip()
if raw:
ovr[ck] = float(raw) if pk == "lr" else int(raw)
extra = str(p.get("config_json") or "").strip()
if extra:
try:
obj = json.loads(extra)
except json.JSONDecodeError as e:
raise AirflowException(f"config_json không phải JSON hợp lệ: {e}")
if not isinstance(obj, dict):
raise AirflowException("config_json phải là JSON object")
ovr.update(obj)
return ovr or None
def _truthy(v) -> bool:
return str(v or "").strip().lower() in ("true", "1", "yes")
def _model_name(bot_id: str) -> str:
return f"classify_topic_bot{bot_id}"
def _step(step: str, p: dict, timeout: int, overrides: dict | None = None) -> dict:
jid = trigger_step(step, str(p["bot_id"]), str(p["dataset"]), config_overrides=overrides,
cancel_running=_truthy(p.get("cancel_running")))
return wait_job(jid, timeout_sec=timeout)
@dag(
dag_id="train_pipeline",
schedule=None,
start_date=pendulum.datetime(2026, 11, 1, tz="Asia/Ho_Chi_Minh"),
catchup=False,
max_active_runs=1,
default_args={"retries": 0},
params={
"bot_id": Param("2", type="string"),
"dataset": Param("chatbot20261119", type="string",
title="Bộ dữ liệu trên 106 (mô phỏng có: chatbot20261030, chatbot20261119, chatbot_nhieu_nhan)"),
"num_epochs": Param("", type=["string", "null"], title="NUM_EPOCHS (trống = mặc định)"),
"start_epoch": Param("", type=["string", "null"], title="START_EPOCH"),
"lr": Param("", type=["string", "null"], title="LR"),
"batch_size": Param("", type=["string", "null"], title="BATCH_SIZE"),
"max_seq_length": Param("", type=["string", "null"], title="MAX_SEQ_LENGTH"),
"config_json": Param("", type=["string", "null"], title="Override khác, JSON object"),
"min_accuracy": Param("0.90", type=["string", "null"], title="Mục tiêu, dưới mức này chỉ CẢNH BÁO"),
"min_accuracy_floor": Param("0.85", type=["string", "null"], title="Sàn, dưới mức này REJECT"),
"push_gitlab": Param("true", type=["string", "null"],
title="\"true\" = push model lên GitLab, CI cũ sẽ publish MinIO + deploy UAT"),
"cancel_running": Param("false", type=["string", "null"], title="\"true\" = huỷ job đang chạy trên 106"),
},
tags=["mlops", "classify_topic", "sprint1"],
)
def train_pipeline():
@task(task_id="check_106")
def _check() -> None:
check_service()
@task(task_id="train")
def _train(**context) -> dict:
p = context["params"]
ovr = _overrides(p)
if ovr:
print(f"override config cho run này: {ovr}")
return _step("nlp", p, TIMEOUT_NLP, ovr)
@task(task_id="export_onnx")
def _onnx(**context) -> dict:
return _step("onnx", context["params"], TIMEOUT_LIGHT)
@task(task_id="evaluate")
def _evaluate(**context) -> dict:
return _step("smoke_test", context["params"], TIMEOUT_LIGHT)
@task(task_id="gate")
def _gate(smoke: dict, **context) -> dict:
"""Giữ logic cổng chặn cũ (sprint 2 đổi sang metrics.json + so model prod)."""
p = context["params"]
target = str(p.get("min_accuracy") or "").strip()
floor = str(p.get("min_accuracy_floor") or "").strip()
metrics = smoke.get("metrics") or {}
acc = metrics.get("accuracy", metrics.get("acc"))
jid = smoke.get("job_id")
m = _SCRIPT_STATUS_RE.search(get_job_log(jid) if jid else "")
status = m.group(1) if m else None
print(f"[gate] accuracy={acc} | so hôm trước={status or 'không thấy'} | sàn={floor or '-'} | mục tiêu={target or '-'}")
if not target and not floor:
print("bỏ qua gate (cả hai ngưỡng trống)")
return {"accuracy": acc, "decision": "skipped"}
if acc is None:
raise AirflowException(f"không lấy được accuracy từ job {jid}")
if status == "Fail":
raise AirflowException(f"MODEL BỊ REJECT: accuracy {acc} tụt quá 2% so với lần chạy của ngày hôm trước")
if floor and float(acc) < float(floor):
raise AirflowException(f"MODEL BỊ REJECT: accuracy {acc} < sàn {floor}")
if target and float(acc) < float(target):
print(f"CẢNH BÁO: accuracy {acc} < mục tiêu {target}, vẫn chạy tiếp")
return {"accuracy": acc, "decision": "warn"}
return {"accuracy": acc, "decision": "pass"}
@task(task_id="package_tokenizer")
def _tokenizer(**context) -> dict:
return _step("tokenizer", context["params"], TIMEOUT_LIGHT)
@task(task_id="package_json")
def _json(**context) -> dict:
return _step("json", context["params"], TIMEOUT_LIGHT)
@task(task_id="verify")
def _verify(**context) -> None:
ti = context["ti"]
paths = []
for tid in STEP_TASKS:
info = ti.xcom_pull(task_ids=tid) or {}
paths += [o["path"] for o in info.get("output_paths", [])]
if not paths:
print("CẢNH BÁO: không có output path nào, bỏ qua verify (giữ như DAG tạm; sprint 2 đổi thành lỗi)")
return
res = verify_paths(paths)
for o in res.get("results", []):
print(f" {o['exists']!s:5} {o['size']:>12} {o['path']}")
bad = [r["path"] for r in res.get("results", []) if not r["exists"] or r["size"] == 0]
if bad:
raise AirflowException(f"{len(bad)} file thiếu hoặc rỗng: {bad}")
print(f"verify OK: {len(paths)} file")
@task(task_id="register")
def _register(**context) -> dict:
"""Ghi lần train vào MLflow và tạo model version alias "candidate"."""
import mlflow
from mlflow.exceptions import MlflowException
from mlflow.tracking import MlflowClient
p, ti = context["params"], context["ti"]
bot, dataset = str(p["bot_id"]), str(p["dataset"])
info = {tid: ti.xcom_pull(task_ids=tid) or {}
for tid in ("train", "export_onnx", "evaluate", "gate")}
mlflow.set_tracking_uri(os.environ["MLFLOW_TRACKING_URI"])
mlflow.set_experiment(_model_name(bot))
client = MlflowClient()
name = _model_name(bot)
with tempfile.TemporaryDirectory() as d, \
mlflow.start_run(run_name=f"{dataset}-{context['run_id']}") as run:
mlflow.log_params({"bot_id": bot, "dataset": dataset,
**{f"cfg_{k}": v for k, v in (_overrides(p) or {}).items()}})
mlflow.set_tags({
"dataset": dataset,
"train_job_id": info["train"].get("job_id"),
"onnx_job_id": info["export_onnx"].get("job_id"),
"eval_job_id": info["evaluate"].get("job_id"),
"train_run_dir": (info["train"].get("metrics") or {}).get("run_dir"),
"gate_decision": info["gate"].get("decision"),
"airflow_dag_run": context["run_id"],
})
nums = {**{f"train_{k}": v for k, v in (info["train"].get("metrics") or {}).items()},
**(info["evaluate"].get("metrics") or {}),
"onnx_max_abs_diff": (info["export_onnx"].get("metrics") or {}).get("max_abs_diff")}
mlflow.log_metrics({k: float(v) for k, v in nums.items()
if isinstance(v, (int, float)) and not isinstance(v, bool)})
for fname in PUSH_FILES:
download_artifact(fname, os.path.join(d, "model", fname), bot)
mlflow.log_artifacts(os.path.join(d, "model"), artifact_path="model")
try:
client.create_registered_model(name, description="Phân loại chủ đề 4 cấp, bot " + bot)
except MlflowException:
pass # đã có
mv = client.create_model_version(name, source=f"{run.info.artifact_uri}/model",
run_id=run.info.run_id, tags={"dataset": dataset})
client.set_registered_model_alias(name, "candidate", mv.version)
print(f"MLflow: run {run.info.run_id}, model {name} version {mv.version} -> alias candidate")
return {"run_id": run.info.run_id, "model_name": name, "model_version": mv.version}
def _git(*args, cwd: str, secret: str = "") -> str:
r = subprocess.run(["git", *args], cwd=cwd, capture_output=True, text=True, timeout=1800,
env={**os.environ, "GIT_TERMINAL_PROMPT": "0"})
if r.returncode != 0:
msg = (r.stderr or r.stdout).strip()[-800:]
raise AirflowException(f"git {args[0]} lỗi: {msg.replace(secret, '***') if secret else msg}")
return r.stdout.strip()
@task(task_id="push_gitlab")
def _push(**context) -> dict:
"""Như task CI_CD cũ: model.onnx + 4 tokenizer vào repo, bump VERSION, push master -> CI cũ chạy."""
p, ti = context["params"], context["ti"]
if not _truthy(p.get("push_gitlab")):
print("push_gitlab=false -> dừng ở đây, không có gì lên UAT")
return {"pushed": False}
token = os.environ.get("GITLAB_TOKEN", "")
if not token:
raise AirflowException("thiếu GITLAB_TOKEN (Secret mlops-gitlab)")
bot, dataset = str(p["bot_id"]), str(p["dataset"])
host = os.environ["GITLAB_URL"].split("://", 1)[1]
url = f"http://{os.environ.get('GITLAB_USER', 'svc-mlops')}:{token}@{host}/{os.environ['GITLAB_REPO']}.git"
repo = tempfile.mkdtemp(prefix="classify_topic_")
_git("clone", "-q", "--depth", "1", "--branch", "master", url, repo, cwd="/tmp", secret=token)
for fname, rel in PUSH_FILES.items():
size = download_artifact(fname, os.path.join(repo, rel.format(bot_id=bot)), bot)
print(f"đã tải {fname} -> {rel.format(bot_id=bot)} ({size} bytes)")
if size == 0:
raise AirflowException(f"{fname} rỗng")
if not _git("status", "--porcelain", cwd=repo):
print("model trùng bản trên master -> không push")
return {"pushed": False, "reason": "no_changes"}
ver_path = os.path.join(repo, "VERSION")
old = open(ver_path, encoding="utf-8").read().strip()
parts = old.split(".")
parts[-1] = str(int(parts[-1]) + 1)
new = ".".join(parts)
open(ver_path, "w", encoding="utf-8").write(new + "\n")
smoke = ti.xcom_pull(task_ids="evaluate") or {}
reg = ti.xcom_pull(task_ids="register") or {}
acc = (smoke.get("metrics") or {}).get("accuracy")
msg = (f"TRAIN: bot_id={bot} dataset={dataset} acc={acc} (job {smoke.get('job_id')}) "
f"mlflow={reg.get('model_name')}/{reg.get('model_version')} bump v{new}")
_git("add", "-A", cwd=repo)
_git("-c", "user.name=svc-mlops", "-c", "user.email=svc-mlops@users.noreply",
"commit", "-q", "-m", msg, cwd=repo)
_git("push", "-q", url, "HEAD:master", cwd=repo, secret=token)
sha = _git("rev-parse", "HEAD", cwd=repo)
print(f"đã push {sha[:8]}: {msg}")
if reg.get("run_id"):
import mlflow
from mlflow.tracking import MlflowClient
mlflow.set_tracking_uri(os.environ["MLFLOW_TRACKING_URI"])
c = MlflowClient()
c.set_tag(reg["run_id"], "model_version", f"v{new}")
c.set_tag(reg["run_id"], "git_sha", sha)
c.set_model_version_tag(reg["model_name"], reg["model_version"], "model_version", f"v{new}")
print(f"CI cũ sẽ chạy: validate -> publish v{new} -> deploy UAT -> smoke test (prod bấm tay)")
return {"pushed": True, "version": f"v{new}", "git_sha": sha}
smoke = _evaluate()
gate = _gate(smoke)
_check() >> _train() >> _onnx() >> smoke
gate >> _tokenizer() >> _json() >> _verify() >> _register() >> _push()
train_pipeline()
# Triton 23.03 + initContainer tải gói model theo MODEL_VERSION từ MinIO.
# Mô phỏng: bỏ schedulerName kai-scheduler và nodeAffinity GPU (cụm k3d không có GPU), giữ annotation gpu-fraction.
apiVersion: apps/v1
kind: Deployment
metadata:
name: classify-topic
labels:
app: classify-topic
spec:
replicas: 1
selector:
matchLabels:
app: classify-topic
template:
metadata:
labels:
app: classify-topic
annotations:
gpu-fraction: "0.15"
spec:
initContainers:
- name: fetch-model
image: k3d-nexus:5000/ci-base:1.0.0
command: ["sh", "-c"]
args:
- >-
mc alias set m http://minio-service.storage.svc:9000 "$MINIO_USER" "$MINIO_KEY" >/dev/null &&
mc mirror --overwrite "m/${MINIO_PATH}/${MODEL_VERSION}/" /workspace/ &&
test -f /workspace/model_repository/PhoBERT/1/model.onnx &&
echo "Đã tải ${MODEL_VERSION}"
envFrom:
- configMapRef:
name: classify-topic-version
env:
- name: MINIO_PATH
value: models/hieuhm18/classify_topic
- name: MINIO_USER
valueFrom:
secretKeyRef: {name: minio-creds, key: username}
- name: MINIO_KEY
valueFrom:
secretKeyRef: {name: minio-creds, key: password}
volumeMounts:
- {name: workspace, mountPath: /workspace}
containers:
- name: triton
image: k3d-nexus:5000/tritonserver:23.03-py3
command: ["tritonserver"]
args:
- --model-repository=/workspace/model_repository
- --http-port=8200
- --grpc-port=8201
- --metrics-port=8299
envFrom:
- configMapRef:
name: classify-topic-version
ports:
- {name: http, containerPort: 8200}
- {name: metrics, containerPort: 8299}
readinessProbe:
httpGet: {path: /v2/health/ready, port: 8200}
periodSeconds: 5
resources:
requests: {cpu: 250m, memory: 512Mi}
limits: {memory: 3Gi}
volumeMounts:
- {name: workspace, mountPath: /workspace}
volumes:
- name: workspace
emptyDir: {}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
apiVersion: v1
kind: Service
metadata:
name: classify-topic
spec:
type: NodePort
selector:
app: classify-topic
ports:
- {name: http, port: 8200, targetPort: 8200}
- {name: metrics, port: 8299, targetPort: 8299}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: ocr
resources:
- ../../base
- pdb.yaml
configMapGenerator:
- name: classify-topic-version
literals:
- MODEL_VERSION=v1.0.0
patches:
- path: patch-gpu.yaml
- path: patch-service.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: classify-topic
spec:
replicas: 2
template:
metadata:
annotations:
gpu-fraction: "0.3"
apiVersion: v1
kind: Service
metadata:
name: classify-topic
spec:
ports:
- {name: http, port: 8200, nodePort: 8215}
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: classify-topic
spec:
minAvailable: 1
selector:
matchLabels:
app: classify-topic
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: uat
nameSuffix: -uat
resources:
- ../../base
configMapGenerator:
- name: classify-topic-version
literals:
- MODEL_VERSION=v1.0.0
patches:
- path: patch-gpu.yaml
- path: patch-service.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: classify-topic
spec:
replicas: 1
template:
metadata:
annotations:
gpu-fraction: "0.15"
apiVersion: v1
kind: Service
metadata:
name: classify-topic
spec:
ports:
- {name: http, port: 8200, nodePort: 8216}
# Bản mô phỏng: model nhỏ thay PhoBERT, chạy CPU (cụm thật chạy GPU L40S).
name: "PhoBERT"
platform: "onnxruntime_onnx"
max_batch_size: 0
input [
{ name: "input_ids" data_type: TYPE_INT64 dims: [ 1, 32 ] }
]
output [
{ name: "output_l1" data_type: TYPE_FP32 dims: [ 1, 5 ] },
{ name: "output_l2" data_type: TYPE_FP32 dims: [ 1, 13 ] },
{ name: "output_l3" data_type: TYPE_FP32 dims: [ 1, 25 ] },
{ name: "output_l4" data_type: TYPE_FP32 dims: [ 1, 49 ] }
]
instance_group [ { count: 2 kind: KIND_CPU } ]
name: "ensemble_PhoBERT"
platform: "ensemble"
max_batch_size: 0
input [
{ name: "INPUT_TEXT" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "BOT_ID" data_type: TYPE_STRING dims: [ 1 ] }
]
output [
{ name: "TEXT_LABEL_L1" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_LABEL_L2" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_LABEL_L3" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_LABEL_L4" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_CONF_L1" data_type: TYPE_FP32 dims: [ 1 ] },
{ name: "TEXT_CONF_L2" data_type: TYPE_FP32 dims: [ 1 ] },
{ name: "TEXT_CONF_L3" data_type: TYPE_FP32 dims: [ 1 ] },
{ name: "TEXT_CONF_L4" data_type: TYPE_FP32 dims: [ 1 ] }
]
ensemble_scheduling {
step [
{
model_name: "preprocess_PhoBERT"
model_version: -1
input_map { key: "INPUT_TEXT" value: "INPUT_TEXT" }
input_map { key: "BOT_ID" value: "BOT_ID" }
output_map { key: "INPUT_IDS" value: "input_ids_t" }
},
{
model_name: "PhoBERT"
model_version: -1
input_map { key: "input_ids" value: "input_ids_t" }
output_map { key: "output_l1" value: "probs_l1" }
output_map { key: "output_l2" value: "probs_l2" }
output_map { key: "output_l3" value: "probs_l3" }
output_map { key: "output_l4" value: "probs_l4" }
},
{
model_name: "postprocess_PhoBERT"
model_version: -1
input_map { key: "BOT_ID" value: "BOT_ID" }
input_map { key: "INPUT_LABELS_L1" value: "probs_l1" }
input_map { key: "INPUT_LABELS_L2" value: "probs_l2" }
input_map { key: "INPUT_LABELS_L3" value: "probs_l3" }
input_map { key: "INPUT_LABELS_L4" value: "probs_l4" }
output_map { key: "TEXT_LABEL_L1" value: "TEXT_LABEL_L1" }
output_map { key: "TEXT_LABEL_L2" value: "TEXT_LABEL_L2" }
output_map { key: "TEXT_LABEL_L3" value: "TEXT_LABEL_L3" }
output_map { key: "TEXT_LABEL_L4" value: "TEXT_LABEL_L4" }
output_map { key: "TEXT_CONF_L1" value: "TEXT_CONF_L1" }
output_map { key: "TEXT_CONF_L2" value: "TEXT_CONF_L2" }
output_map { key: "TEXT_CONF_L3" value: "TEXT_CONF_L3" }
output_map { key: "TEXT_CONF_L4" value: "TEXT_CONF_L4" }
}
]
}
"""Khối postprocess: argmax 4 cấp, id -> tên nhãn theo bot, kiểm cây nhãn (con không thuộc cha -> "nan")."""
import glob
import json
import os
import numpy as np
import triton_python_backend_utils as pb_utils
LEVELS = (1, 2, 3, 4)
class TritonPythonModel:
def initialize(self, args):
base = os.path.join(args["model_repository"], "tokenizer")
self.tok = {}
for bot_dir in sorted(glob.glob(os.path.join(base, "*"))):
bot = os.path.basename(bot_dir)
levels = {}
for k in LEVELS:
with open(os.path.join(bot_dir, "label_level%d_tokenizer.json" % k), encoding="utf-8") as f:
levels[k] = json.load(f)
self.tok[bot] = levels
pb_utils.Logger.log_info("postprocess: nạp tokenizer cho bot %s" % sorted(self.tok))
def execute(self, requests):
responses = []
for req in requests:
bot = pb_utils.get_input_tensor_by_name(req, "BOT_ID").as_numpy().reshape(-1)[0]
bot = bot.decode("utf-8") if isinstance(bot, bytes) else str(bot)
if bot not in self.tok:
err = pb_utils.TritonError("BOT_ID %s chưa có tokenizer nhãn" % bot)
responses.append(pb_utils.InferenceResponse(output_tensors=[], error=err))
continue
labels, confs = [], []
parent_label = None
for k in LEVELS:
probs = pb_utils.get_input_tensor_by_name(req, "INPUT_LABELS_L%d" % k).as_numpy().reshape(-1)
idx = int(np.argmax(probs))
name = self.tok[bot][k]["labels"][idx]
parent = self.tok[bot][k]["parent"].get(name)
if k > 1 and (parent_label in (None, "nan") or parent != parent_label):
name = "nan"
labels.append(name)
confs.append(float(probs[idx]))
parent_label = name
out = [pb_utils.Tensor("TEXT_LABEL_L%d" % k, np.array([labels[k - 1].encode("utf-8")], dtype=np.object_))
for k in LEVELS]
out += [pb_utils.Tensor("TEXT_CONF_L%d" % k, np.array([confs[k - 1]], dtype=np.float32)) for k in LEVELS]
responses.append(pb_utils.InferenceResponse(output_tensors=out))
return responses
name: "postprocess_PhoBERT"
backend: "python"
max_batch_size: 0
input [
{ name: "BOT_ID" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "INPUT_LABELS_L1" data_type: TYPE_FP32 dims: [ 1, 5 ] },
{ name: "INPUT_LABELS_L2" data_type: TYPE_FP32 dims: [ 1, 13 ] },
{ name: "INPUT_LABELS_L3" data_type: TYPE_FP32 dims: [ 1, 25 ] },
{ name: "INPUT_LABELS_L4" data_type: TYPE_FP32 dims: [ 1, 49 ] }
]
output [
{ name: "TEXT_LABEL_L1" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_LABEL_L2" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_LABEL_L3" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_LABEL_L4" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "TEXT_CONF_L1" data_type: TYPE_FP32 dims: [ 1 ] },
{ name: "TEXT_CONF_L2" data_type: TYPE_FP32 dims: [ 1 ] },
{ name: "TEXT_CONF_L3" data_type: TYPE_FP32 dims: [ 1 ] },
{ name: "TEXT_CONF_L4" data_type: TYPE_FP32 dims: [ 1 ] }
]
instance_group [ { count: 1 kind: KIND_CPU } ]
{
"bot_id": "2",
"level": 1,
"labels": [
"nan",
"dien_nuoc",
"hoadon_dientu",
"tai_khoan",
"the_cao"
],
"parent": {}
}
\ No newline at end of file
{
"bot_id": "2",
"level": 2,
"labels": [
"nan",
"dang_ky_tu_dong",
"dang_nhap",
"dieu_chinh_hoadon",
"huy_hoadon",
"khoa_tai_khoan",
"loi_the",
"mo_tai_khoan",
"mua_the",
"nap_the",
"thanh_toan",
"tra_cuu_hoadon",
"tra_cuu_no"
],
"parent": {
"tra_cuu_hoadon": "hoadon_dientu",
"dieu_chinh_hoadon": "hoadon_dientu",
"huy_hoadon": "hoadon_dientu",
"nap_the": "the_cao",
"loi_the": "the_cao",
"mua_the": "the_cao",
"mo_tai_khoan": "tai_khoan",
"dang_nhap": "tai_khoan",
"khoa_tai_khoan": "tai_khoan",
"thanh_toan": "dien_nuoc",
"tra_cuu_no": "dien_nuoc",
"dang_ky_tu_dong": "dien_nuoc"
}
}
\ No newline at end of file
{
"bot_id": "2",
"level": 3,
"labels": [
"nan",
"dang_ky_tu_dong__huong_dan",
"dang_ky_tu_dong__khieu_nai",
"dang_nhap__huong_dan",
"dang_nhap__khieu_nai",
"dieu_chinh_hoadon__huong_dan",
"dieu_chinh_hoadon__khieu_nai",
"huy_hoadon__huong_dan",
"huy_hoadon__khieu_nai",
"khoa_tai_khoan__huong_dan",
"khoa_tai_khoan__khieu_nai",
"loi_the__huong_dan",
"loi_the__khieu_nai",
"mo_tai_khoan__huong_dan",
"mo_tai_khoan__khieu_nai",
"mua_the__huong_dan",
"mua_the__khieu_nai",
"nap_the__huong_dan",
"nap_the__khieu_nai",
"thanh_toan__huong_dan",
"thanh_toan__khieu_nai",
"tra_cuu_hoadon__huong_dan",
"tra_cuu_hoadon__khieu_nai",
"tra_cuu_no__huong_dan",
"tra_cuu_no__khieu_nai"
],
"parent": {
"tra_cuu_hoadon__huong_dan": "tra_cuu_hoadon",
"tra_cuu_hoadon__khieu_nai": "tra_cuu_hoadon",
"dieu_chinh_hoadon__huong_dan": "dieu_chinh_hoadon",
"dieu_chinh_hoadon__khieu_nai": "dieu_chinh_hoadon",
"huy_hoadon__huong_dan": "huy_hoadon",
"huy_hoadon__khieu_nai": "huy_hoadon",
"nap_the__huong_dan": "nap_the",
"nap_the__khieu_nai": "nap_the",
"loi_the__huong_dan": "loi_the",
"loi_the__khieu_nai": "loi_the",
"mua_the__huong_dan": "mua_the",
"mua_the__khieu_nai": "mua_the",
"mo_tai_khoan__huong_dan": "mo_tai_khoan",
"mo_tai_khoan__khieu_nai": "mo_tai_khoan",
"dang_nhap__huong_dan": "dang_nhap",
"dang_nhap__khieu_nai": "dang_nhap",
"khoa_tai_khoan__huong_dan": "khoa_tai_khoan",
"khoa_tai_khoan__khieu_nai": "khoa_tai_khoan",
"thanh_toan__huong_dan": "thanh_toan",
"thanh_toan__khieu_nai": "thanh_toan",
"tra_cuu_no__huong_dan": "tra_cuu_no",
"tra_cuu_no__khieu_nai": "tra_cuu_no",
"dang_ky_tu_dong__huong_dan": "dang_ky_tu_dong",
"dang_ky_tu_dong__khieu_nai": "dang_ky_tu_dong"
}
}
\ No newline at end of file
{
"bot_id": "2",
"level": 4,
"labels": [
"nan",
"dang_ky_tu_dong__huong_dan__online",
"dang_ky_tu_dong__huong_dan__tai_quay",
"dang_ky_tu_dong__khieu_nai__online",
"dang_ky_tu_dong__khieu_nai__tai_quay",
"dang_nhap__huong_dan__online",
"dang_nhap__huong_dan__tai_quay",
"dang_nhap__khieu_nai__online",
"dang_nhap__khieu_nai__tai_quay",
"dieu_chinh_hoadon__huong_dan__online",
"dieu_chinh_hoadon__huong_dan__tai_quay",
"dieu_chinh_hoadon__khieu_nai__online",
"dieu_chinh_hoadon__khieu_nai__tai_quay",
"huy_hoadon__huong_dan__online",
"huy_hoadon__huong_dan__tai_quay",
"huy_hoadon__khieu_nai__online",
"huy_hoadon__khieu_nai__tai_quay",
"khoa_tai_khoan__huong_dan__online",
"khoa_tai_khoan__huong_dan__tai_quay",
"khoa_tai_khoan__khieu_nai__online",
"khoa_tai_khoan__khieu_nai__tai_quay",
"loi_the__huong_dan__online",
"loi_the__huong_dan__tai_quay",
"loi_the__khieu_nai__online",
"loi_the__khieu_nai__tai_quay",
"mo_tai_khoan__huong_dan__online",
"mo_tai_khoan__huong_dan__tai_quay",
"mo_tai_khoan__khieu_nai__online",
"mo_tai_khoan__khieu_nai__tai_quay",
"mua_the__huong_dan__online",
"mua_the__huong_dan__tai_quay",
"mua_the__khieu_nai__online",
"mua_the__khieu_nai__tai_quay",
"nap_the__huong_dan__online",
"nap_the__huong_dan__tai_quay",
"nap_the__khieu_nai__online",
"nap_the__khieu_nai__tai_quay",
"thanh_toan__huong_dan__online",
"thanh_toan__huong_dan__tai_quay",
"thanh_toan__khieu_nai__online",
"thanh_toan__khieu_nai__tai_quay",
"tra_cuu_hoadon__huong_dan__online",
"tra_cuu_hoadon__huong_dan__tai_quay",
"tra_cuu_hoadon__khieu_nai__online",
"tra_cuu_hoadon__khieu_nai__tai_quay",
"tra_cuu_no__huong_dan__online",
"tra_cuu_no__huong_dan__tai_quay",
"tra_cuu_no__khieu_nai__online",
"tra_cuu_no__khieu_nai__tai_quay"
],
"parent": {
"tra_cuu_hoadon__huong_dan__online": "tra_cuu_hoadon__huong_dan",
"tra_cuu_hoadon__huong_dan__tai_quay": "tra_cuu_hoadon__huong_dan",
"tra_cuu_hoadon__khieu_nai__online": "tra_cuu_hoadon__khieu_nai",
"tra_cuu_hoadon__khieu_nai__tai_quay": "tra_cuu_hoadon__khieu_nai",
"dieu_chinh_hoadon__huong_dan__online": "dieu_chinh_hoadon__huong_dan",
"dieu_chinh_hoadon__huong_dan__tai_quay": "dieu_chinh_hoadon__huong_dan",
"dieu_chinh_hoadon__khieu_nai__online": "dieu_chinh_hoadon__khieu_nai",
"dieu_chinh_hoadon__khieu_nai__tai_quay": "dieu_chinh_hoadon__khieu_nai",
"huy_hoadon__huong_dan__online": "huy_hoadon__huong_dan",
"huy_hoadon__huong_dan__tai_quay": "huy_hoadon__huong_dan",
"huy_hoadon__khieu_nai__online": "huy_hoadon__khieu_nai",
"huy_hoadon__khieu_nai__tai_quay": "huy_hoadon__khieu_nai",
"nap_the__huong_dan__online": "nap_the__huong_dan",
"nap_the__huong_dan__tai_quay": "nap_the__huong_dan",
"nap_the__khieu_nai__online": "nap_the__khieu_nai",
"nap_the__khieu_nai__tai_quay": "nap_the__khieu_nai",
"loi_the__huong_dan__online": "loi_the__huong_dan",
"loi_the__huong_dan__tai_quay": "loi_the__huong_dan",
"loi_the__khieu_nai__online": "loi_the__khieu_nai",
"loi_the__khieu_nai__tai_quay": "loi_the__khieu_nai",
"mua_the__huong_dan__online": "mua_the__huong_dan",
"mua_the__huong_dan__tai_quay": "mua_the__huong_dan",
"mua_the__khieu_nai__online": "mua_the__khieu_nai",
"mua_the__khieu_nai__tai_quay": "mua_the__khieu_nai",
"mo_tai_khoan__huong_dan__online": "mo_tai_khoan__huong_dan",
"mo_tai_khoan__huong_dan__tai_quay": "mo_tai_khoan__huong_dan",
"mo_tai_khoan__khieu_nai__online": "mo_tai_khoan__khieu_nai",
"mo_tai_khoan__khieu_nai__tai_quay": "mo_tai_khoan__khieu_nai",
"dang_nhap__huong_dan__online": "dang_nhap__huong_dan",
"dang_nhap__huong_dan__tai_quay": "dang_nhap__huong_dan",
"dang_nhap__khieu_nai__online": "dang_nhap__khieu_nai",
"dang_nhap__khieu_nai__tai_quay": "dang_nhap__khieu_nai",
"khoa_tai_khoan__huong_dan__online": "khoa_tai_khoan__huong_dan",
"khoa_tai_khoan__huong_dan__tai_quay": "khoa_tai_khoan__huong_dan",
"khoa_tai_khoan__khieu_nai__online": "khoa_tai_khoan__khieu_nai",
"khoa_tai_khoan__khieu_nai__tai_quay": "khoa_tai_khoan__khieu_nai",
"thanh_toan__huong_dan__online": "thanh_toan__huong_dan",
"thanh_toan__huong_dan__tai_quay": "thanh_toan__huong_dan",
"thanh_toan__khieu_nai__online": "thanh_toan__khieu_nai",
"thanh_toan__khieu_nai__tai_quay": "thanh_toan__khieu_nai",
"tra_cuu_no__huong_dan__online": "tra_cuu_no__huong_dan",
"tra_cuu_no__huong_dan__tai_quay": "tra_cuu_no__huong_dan",
"tra_cuu_no__khieu_nai__online": "tra_cuu_no__khieu_nai",
"tra_cuu_no__khieu_nai__tai_quay": "tra_cuu_no__khieu_nai",
"dang_ky_tu_dong__huong_dan__online": "dang_ky_tu_dong__huong_dan",
"dang_ky_tu_dong__huong_dan__tai_quay": "dang_ky_tu_dong__huong_dan",
"dang_ky_tu_dong__khieu_nai__online": "dang_ky_tu_dong__khieu_nai",
"dang_ky_tu_dong__khieu_nai__tai_quay": "dang_ky_tu_dong__khieu_nai"
}
}
\ No newline at end of file
"""Khối preprocess của ensemble: câu -> input_ids. Dùng chung text_preprocess.py với lúc train."""
import os
import sys
import numpy as np
import triton_python_backend_utils as pb_utils
class TritonPythonModel:
def initialize(self, args):
sys.path.insert(0, os.path.join(args["model_repository"], args["model_version"]))
import text_preprocess
self.tp = text_preprocess
def execute(self, requests):
responses = []
for req in requests:
text = pb_utils.get_input_tensor_by_name(req, "INPUT_TEXT").as_numpy().reshape(-1)[0]
if isinstance(text, bytes):
text = text.decode("utf-8")
ids = np.array([self.tp.encode(text)], dtype=np.int64)
responses.append(pb_utils.InferenceResponse(output_tensors=[pb_utils.Tensor("INPUT_IDS", ids)]))
return responses
"""Tiền xử lý dùng chung cho train (máy 106) và serving (khối preprocess_PhoBERT trên Triton).
Viết tương thích Python 3.8 vì Python backend của Triton 23.03 chạy 3.8.
Bản mô phỏng: chuẩn hoá bỏ dấu, tách từ theo khoảng trắng, băm unigram + bigram ra id.
"""
import re
import unicodedata
import zlib
MAX_SEQ_LENGTH = 32
VOCAB_SIZE = 50000
_TOKEN_RE = re.compile(r"[a-z0-9]+")
def normalize(text):
"""Chữ thường, bỏ dấu tiếng Việt, chỉ giữ chữ và số."""
text = str(text).lower().replace("đ", "d")
text = unicodedata.normalize("NFD", text)
text = "".join(c for c in text if unicodedata.category(c) != "Mn")
return " ".join(_TOKEN_RE.findall(text))
def tokens(text):
words = normalize(text).split()
return words + [a + "_" + b for a, b in zip(words, words[1:])]
def _hash(tok):
return zlib.crc32(tok.encode("utf-8")) % (VOCAB_SIZE - 1) + 1
def encode(text, max_len=MAX_SEQ_LENGTH):
"""Trả list id độ dài max_len; 0 là đệm."""
ids = [_hash(t) for t in tokens(text)][:max_len]
return ids + [0] * (max_len - len(ids))
name: "preprocess_PhoBERT"
backend: "python"
max_batch_size: 0
input [
{ name: "INPUT_TEXT" data_type: TYPE_STRING dims: [ 1 ] },
{ name: "BOT_ID" data_type: TYPE_STRING dims: [ 1 ] }
]
output [
{ name: "INPUT_IDS" data_type: TYPE_INT64 dims: [ 1, 32 ] }
]
instance_group [ { count: 1 kind: KIND_CPU } ]
#!/usr/bin/env bash
# Ghi version đang chạy vào git: clone master mới nhất, bump overlay, commit [ci skip], push.
# Dùng: bash scripts/commit_version.sh uat v1.0.1
# Credentials lấy từ Secret git-push-creds (ns ci) của tài khoản dịch vụ.
set -euo pipefail
ENV_NAME=$1
VERSION=$2
USER_NAME=$(bash scripts/kretry.sh kubectl get secret git-push-creds -n ci -o jsonpath='{.data.username}' | base64 -d)
TOKEN=$(bash scripts/kretry.sh kubectl get secret git-push-creds -n ci -o jsonpath='{.data.token}' | base64 -d)
HOST=$(echo "$CI_PROJECT_URL" | sed -E 's#^https?://##')
URL="http://${USER_NAME}:${TOKEN}@${HOST}.git"
WORK=$(mktemp -d)
git clone -q --depth 1 --branch master "$URL" "$WORK"
python3 scripts/set_version.py "$WORK/deploy/classify-topic/overlays/$ENV_NAME" "$VERSION"
cd "$WORK"
if git diff --quiet; then echo "Overlay $ENV_NAME đã ở $VERSION"; exit 0; fi
git -c user.name="$USER_NAME" -c user.email="${USER_NAME}@users.noreply" \
commit -qam "[ci skip] deploy $ENV_NAME $VERSION (pipeline $CI_PIPELINE_ID)"
git push -q "$URL" HEAD:master
echo "Đã ghi $ENV_NAME=$VERSION vào master"
#!/usr/bin/env bash
# Bọc kubectl: thử lại tối đa 5 lần khi apiserver từ chối tạm thời (như cụm thật).
n=0
until "$@"; do
n=$((n + 1))
if [ "$n" -ge 5 ]; then echo "kretry: hết 5 lần thử: $*" >&2; exit 1; fi
echo "kretry: lỗi, thử lại lần $n..." >&2
sleep $((n * 3))
done
# Lấy credentials MinIO từ K8s Secret (GitLab 11.7 không có masked variables) và tạo alias mc 'myminio'.
# Dùng: . scripts/minio_env.sh
export MINIO_USER=$(bash scripts/kretry.sh kubectl get secret minio-creds -n ci -o jsonpath='{.data.username}' | base64 -d)
export MINIO_KEY=$(bash scripts/kretry.sh kubectl get secret minio-creds -n ci -o jsonpath='{.data.password}' | base64 -d)
export MINIO_ADDR=$(bash scripts/kretry.sh kubectl get svc minio-service -n storage -o jsonpath='{.spec.clusterIP}')
test -n "$MINIO_USER" -a -n "$MINIO_KEY" -a -n "$MINIO_ADDR"
echo "MinIO ClusterIP = $MINIO_ADDR"
mc alias set myminio "http://${MINIO_ADDR}:9000" "$MINIO_USER" "$MINIO_KEY" >/dev/null
"""Đóng gói runtime để đẩy MinIO: copy model_repository + ghi MANIFEST.json.
python3 scripts/package_runtime.py v1.0.1 staging
"""
import datetime
import hashlib
import json
import os
import shutil
import sys
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
REQUIRED = [
"model_repository/ensemble_PhoBERT/config.pbtxt",
"model_repository/preprocess_PhoBERT/config.pbtxt",
"model_repository/preprocess_PhoBERT/1/model.py",
"model_repository/preprocess_PhoBERT/1/text_preprocess.py",
"model_repository/PhoBERT/config.pbtxt",
"model_repository/PhoBERT/1/model.onnx",
"model_repository/postprocess_PhoBERT/config.pbtxt",
"model_repository/postprocess_PhoBERT/1/model.py",
]
def sha256(path):
h = hashlib.sha256()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(1 << 20), b""):
h.update(chunk)
return h.hexdigest()
def main():
version, out = sys.argv[1], sys.argv[2]
shutil.rmtree(out, ignore_errors=True)
shutil.copytree(os.path.join(ROOT, "model_repository"), os.path.join(out, "model_repository"),
ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
files, total = [], 0
for dirpath, _, names in os.walk(out):
for n in sorted(names):
p = os.path.join(dirpath, n)
rel = os.path.relpath(p, out)
size = os.path.getsize(p)
total += size
files.append({"path": rel, "size": size, "sha256": sha256(p)})
missing = [r for r in REQUIRED if not os.path.isfile(os.path.join(out, r))]
if missing:
sys.exit(f"Thiếu file bắt buộc: {missing}")
manifest = {"version": version,
"created_at": datetime.datetime.now(datetime.timezone.utc).isoformat(timespec="seconds"),
"source_commit": os.environ.get("CI_COMMIT_SHA", ""), "files": files}
with open(os.path.join(out, "MANIFEST.json"), "w", encoding="utf-8") as f:
json.dump(manifest, f, indent=2)
print(f"Đã đóng gói version {version} -> {out}")
print(f"{len(files)} file, {total / 1e6:.1f} MB")
print("MANIFEST.json + các file bắt buộc: đủ")
if __name__ == "__main__":
main()
"""Đọc config.pbtxt của Triton (protobuf text format) thành dict. Mỗi khoá -> list giá trị."""
import re
_TOKEN = re.compile(r'"(?:[^"\\]|\\.)*"|[{}\[\]:,;]|[^\s{}\[\]:,;"]+')
def _strip_comments(text):
"""Bỏ phần từ # tới cuối dòng, trừ # nằm trong chuỗi."""
out = []
for line in text.splitlines():
in_str = False
for i, c in enumerate(line):
if c == '"':
in_str = not in_str
elif c == "#" and not in_str:
line = line[:i]
break
out.append(line)
return "\n".join(out)
def _scalar(tok):
if tok.startswith('"'):
return tok[1:-1]
try:
return int(tok)
except ValueError:
return tok
class _Parser:
def __init__(self, text):
self.toks = _TOKEN.findall(_strip_comments(text))
self.i = 0
def peek(self):
return self.toks[self.i] if self.i < len(self.toks) else None
def take(self, expect=None):
tok = self.toks[self.i]
if expect and tok != expect:
raise ValueError(f"pbtxt: cần '{expect}', gặp '{tok}' ở vị trí {self.i}")
self.i += 1
return tok
def message(self, end=None):
out = {}
while self.peek() is not None and self.peek() != end:
key = self.take()
if self.peek() == ":":
self.take()
if self.peek() == "{":
self.take()
out.setdefault(key, []).append(self.message("}"))
self.take("}")
elif self.peek() == "[":
out.setdefault(key, []).extend(self.listing())
else:
out.setdefault(key, []).append(_scalar(self.take()))
while self.peek() in (",", ";"):
self.take()
return out
def listing(self):
self.take("[")
items = []
while self.peek() != "]":
if self.peek() == "{":
self.take()
items.append(self.message("}"))
self.take("}")
else:
items.append(_scalar(self.take()))
if self.peek() == ",":
self.take()
self.take("]")
return items
def parse(text):
return _Parser(text).message()
def load(path):
with open(path, encoding="utf-8") as f:
return parse(f.read())
def tensors(cfg, kind):
"""{tên tensor: dims} của input hoặc output."""
return {t["name"][0]: t.get("dims", []) for t in cfg.get(kind, [])}
"""Đổi MODEL_VERSION trong overlay: python3 scripts/set_version.py deploy/classify-topic/overlays/uat v1.0.1"""
import re
import sys
overlay, version = sys.argv[1], sys.argv[2]
path = f"{overlay}/kustomization.yaml"
text = open(path, encoding="utf-8").read()
new, n = re.subn(r"MODEL_VERSION=v[0-9A-Za-z.\-]+", f"MODEL_VERSION={version}", text)
if n != 1:
sys.exit(f"Không tìm thấy đúng một dòng MODEL_VERSION trong {path}")
open(path, "w", encoding="utf-8").write(new)
print(f"Đã bump MODEL_VERSION -> {version} trong {path}")
"""Smoke test như hiện tại: gọi ensemble, kiểm đủ 4 nhãn và độ tự tin trong (0, 1].
python3 scripts/smoke_test.py http://<svc>:8200/v2/models/ensemble_PhoBERT/infer 2
Lưu ý: chỉ kiểm hình thức, nhãn sai vẫn đạt (vấn đề đã ghi trong FLOW_HIEN_TAI.md).
"""
import json
import sys
import time
import urllib.request
SENTENCES = [
"cho em hỏi cách tra cứu hóa đơn điện tử online",
"nap the cao bi loi khieu nai tai quay",
"mình muốn mở tài khoản ngân hàng hướng dẫn qua mạng",
]
OUTPUTS = [f"TEXT_LABEL_L{k}" for k in range(1, 5)] + [f"TEXT_CONF_L{k}" for k in range(1, 5)]
def infer(url, text, bot):
body = {"inputs": [{"name": "INPUT_TEXT", "shape": [1], "datatype": "BYTES", "data": [text]},
{"name": "BOT_ID", "shape": [1], "datatype": "BYTES", "data": [bot]}],
"outputs": [{"name": n} for n in OUTPUTS]}
req = urllib.request.Request(url, data=json.dumps(body).encode(), headers={"Content-Type": "application/json"})
for attempt in range(10):
try:
with urllib.request.urlopen(req, timeout=30) as r:
return {o["name"]: o["data"][0] for o in json.loads(r.read())["outputs"]}
except Exception as e: # noqa: BLE001 — service vừa rollout có thể chưa nhận request
if attempt == 9:
raise
print(f"chờ service ({e})")
time.sleep(5)
def main():
url, bot = sys.argv[1], sys.argv[2]
failed = 0
for s in SENTENCES:
out = infer(url, s, bot)
good = all(out.get(f"TEXT_LABEL_L{k}") for k in range(1, 5)) and \
all(0 < float(out[f"TEXT_CONF_L{k}"]) <= 1 for k in range(1, 5))
failed += not good
print(f"[{'OK' if good else 'LỖI'}] '{s[:35]}' -> L1={out['TEXT_LABEL_L1']} | "
f"conf={float(out['TEXT_CONF_L1']):.3f} | L4={out['TEXT_LABEL_L4']}")
if failed:
sys.exit(f"SMOKE TEST: {failed} câu lỗi")
print(f"SMOKE TEST: TẤT CẢ ĐẠT ({len(SENTENCES)} câu x 4 level)")
if __name__ == "__main__":
main()
"""Kiểm repo trước khi đóng gói (stage validate). Thoát mã 1 nếu có lỗi.
1. Compile Python backend
2. Tensor ensemble khớp từng model
3. Tokenizer nhãn khớp dims đầu ra PhoBERT, cây nhãn đủ cành
4. Có model.onnx và tiền xử lý dùng chung
"""
import glob
import json
import os
import py_compile
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import pbtxt # noqa: E402
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MR = os.path.join(ROOT, "model_repository")
errors = []
def ok(msg):
print(f"[OK] {msg}")
def bad(msg):
print(f"[LỖI] {msg}")
errors.append(msg)
print("== 1. Compile Python backend ==")
for f in glob.glob(os.path.join(MR, "**", "*.py"), recursive=True) + glob.glob(os.path.join(ROOT, "scripts", "*.py")):
try:
py_compile.compile(f, doraise=True)
except py_compile.PyCompileError as e:
bad(f"compile {os.path.relpath(f, ROOT)}: {e.msg}")
ok("compile xong")
print("== 2. Đồng bộ tensor ensemble <-> từng model ==")
cfgs = {}
for p in glob.glob(os.path.join(MR, "*", "config.pbtxt")):
c = pbtxt.load(p)
cfgs[c["name"][0]] = c
if c.get("max_batch_size", [None])[0] != 0:
bad(f"{c['name'][0]}: max_batch_size phải là 0")
ens = cfgs.get("ensemble_PhoBERT")
if not ens:
bad("thiếu ensemble_PhoBERT")
else:
produced = set(pbtxt.tensors(ens, "input"))
for step in ens["ensemble_scheduling"][0]["step"]:
name = step["model_name"][0]
m = cfgs.get(name)
if not m:
bad(f"ensemble gọi model không có: {name}")
continue
ins, outs = pbtxt.tensors(m, "input"), pbtxt.tensors(m, "output")
for im in step.get("input_map", []):
if im["key"][0] not in ins:
bad(f"{name}: không có input {im['key'][0]}")
if im["value"][0] not in produced:
bad(f"{name}: input {im['key'][0]} lấy từ {im['value'][0]} chưa được tạo ở bước trước")
for om in step.get("output_map", []):
if om["key"][0] not in outs:
bad(f"{name}: không có output {om['key'][0]}")
produced.add(om["value"][0])
ok(f"{name}: in={sorted(ins)} out={len(outs)}")
missing = set(pbtxt.tensors(ens, "output")) - produced
if missing:
bad(f"ensemble: output chưa được tạo: {sorted(missing)}")
print("== 3. Tokenizer nhãn + cây nhãn ==")
pho_out = pbtxt.tensors(cfgs.get("PhoBERT", {}), "output")
bots = sorted(glob.glob(os.path.join(MR, "postprocess_PhoBERT", "tokenizer", "*")))
if not bots:
bad("chưa có tokenizer nhãn cho bot nào")
for bot_dir in bots:
bot = os.path.basename(bot_dir)
prev = None
for k in (1, 2, 3, 4):
p = os.path.join(bot_dir, f"label_level{k}_tokenizer.json")
if not os.path.isfile(p):
bad(f"bot {bot}: thiếu {os.path.basename(p)}")
break
t = json.load(open(p, encoding="utf-8"))
dims = pho_out.get(f"output_l{k}", [None])[-1]
n = len(t["labels"])
if n != dims:
bad(f"bot {bot} L{k}: {n - 1} nhãn (dims {n}) lệch PhoBERT output_l{k} dims {dims}")
else:
ok(f"bot {bot} L{k}: {n - 1} nhãn (dims {dims}) khớp")
if prev is not None:
orphan = [c for c, par in t["parent"].items() if par not in prev]
if orphan:
bad(f"bot {bot} L{k}: {len(orphan)} nhãn có cha không tồn tại, ví dụ {orphan[:3]}")
prev = set(t["labels"])
print("== 4. Model và tiền xử lý ==")
onnx_path = os.path.join(MR, "PhoBERT", "1", "model.onnx")
if not os.path.isfile(onnx_path) or os.path.getsize(onnx_path) == 0:
bad("thiếu model_repository/PhoBERT/1/model.onnx")
else:
ok(f"model.onnx {os.path.getsize(onnx_path) / 1e6:.1f} MB")
if not os.path.isfile(os.path.join(MR, "preprocess_PhoBERT", "1", "text_preprocess.py")):
bad("thiếu preprocess_PhoBERT/1/text_preprocess.py")
if errors:
print(f"VALIDATE: {len(errors)} LỖI")
sys.exit(1)
print("VALIDATE: TẤT CẢ ĐẠT")
import glob
import json
import os
TOK = os.path.join(os.path.dirname(__file__), "..", "model_repository", "postprocess_PhoBERT", "tokenizer")
def _bots():
return sorted(glob.glob(os.path.join(TOK, "*")))
def test_co_it_nhat_mot_bot():
assert _bots(), "chưa có tokenizer nhãn"
def test_nhan_con_thuoc_dung_cha():
for bot in _bots():
prev = None
for k in (1, 2, 3, 4):
t = json.load(open(os.path.join(bot, f"label_level{k}_tokenizer.json"), encoding="utf-8"))
assert t["labels"][0] == "nan"
assert len(set(t["labels"])) == len(t["labels"])
if prev is not None:
assert set(t["parent"]) == set(t["labels"][1:])
assert all(p in prev for p in t["parent"].values())
prev = set(t["labels"])
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "model_repository", "preprocess_PhoBERT", "1"))
import text_preprocess as tp # noqa: E402
def test_co_dau_va_khong_dau_cho_cung_id():
assert tp.encode("Hóa đơn điện tử") == tp.encode("hoa don dien tu")
def test_do_dai_co_dinh_va_dem_bang_0():
ids = tp.encode("nạp thẻ")
assert len(ids) == tp.MAX_SEQ_LENGTH
assert ids[3:] == [0] * (tp.MAX_SEQ_LENGTH - 3) # 2 từ + 1 bigram
def test_cat_cau_dai():
assert len(tp.encode("a " * 100)) == tp.MAX_SEQ_LENGTH
def test_id_trong_vocab():
assert all(0 < i < tp.VOCAB_SIZE for i in tp.encode("tài khoản bị khóa") if i)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment