Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/dttest/api/python/llm_datadist_v1/status.py: 95%
64 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-27 10:03 +0800
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-27 10:03 +0800
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# -----------------------------------------------------------------------------------------------------------
4# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6# CANN Open Software License Agreement Version 2.0 (the "License").
7# Please refer to the License for details. You may not use this file except in compliance with the License.
8# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10# See LICENSE in the root of the software repository for the full text of the License.
11# -----------------------------------------------------------------------------------------------------------
13from enum import Enum
15from llm_datadist_v1 import llm_wrapper
17from .utils import log
20class LLMStatusCode(Enum):
21 LLM_SUCCESS = llm_wrapper.kSuccess
22 LLM_FAILED = llm_wrapper.kFailed
23 LLM_PARAM_INVALID = llm_wrapper.kParamInvalid
24 LLM_WAIT_PROCESS_TIMEOUT = llm_wrapper.kLLMWaitProcessTimeOut
25 LLM_KV_CACHE_NOT_EXIST = llm_wrapper.kLLMKVCacheNotExist
26 LLM_REPEAT_REQUEST = llm_wrapper.kLLMRepeatRequest
27 LLM_REQUEST_ALREADY_COMPLETED = llm_wrapper.kLLMRequestAlreadyCompleted
28 LLM_ENGINE_FINALIZED = llm_wrapper.kLLMEngineFinalized
29 LLM_NOT_YET_LINK = llm_wrapper.kLLMNotYetLink
30 LLM_ALREADY_LINK = llm_wrapper.kAlreadyLink
31 LLM_LINK_FAILED = llm_wrapper.kLinKFailed
32 LLM_UNLINK_FAILED = llm_wrapper.kUnlinkFailed
33 LLM_NOTIFY_PROMPT_UNLINK_FAILED = llm_wrapper.kNotifyPromptUnlinkFailed
34 LLM_CLUSTER_NUM_EXCEED_LIMIT = llm_wrapper.kLLMClusterNumExceedLimit
35 LLM_PROCESSING_LINK = llm_wrapper.kProcessingLink
36 LLM_DEVICE_OUT_OF_MEMORY = llm_wrapper.kDeviceOutOfMemory
37 LLM_PREFIX_ALREADY_EXIST = llm_wrapper.kPrefixAlreadyExist
38 LLM_PREFIX_NOT_EXIST = llm_wrapper.kPrefixNotExist
39 LLM_SEQ_LENOVERLIMIT = llm_wrapper.kLLMSeqLenOverLimit
40 LLM_NO_FREE_BLOCK = llm_wrapper.kLLMNoFreeBlock
41 LLM_BLOCKS_OUT_OF_MEMORY = llm_wrapper.kLLMBlocksOutOfMemory
42 LLM_EXIST_LINK = llm_wrapper.kLLMExistLink
43 LLM_FEATURE_NOT_ENABLED = llm_wrapper.kLLMFeatureNotEnabled
44 LLM_TIMEOUT = llm_wrapper.kLLMTimeout
45 LLM_LINK_BUSY = llm_wrapper.kLLMLinkBusy
46 LLM_OUT_OF_MEMORY = llm_wrapper.kLLMOutOfMemory
47 LLM_DEVICE_MEM_ERROR = llm_wrapper.kLLMDeviceMemError
48 LLM_SUSPECT_REMOTE_ERROR = llm_wrapper.kLLMSuspectRemoteError
49 LLM_UNKNOWN_ERROR = -1
52class LLMException(RuntimeError):
53 def __init__(self, *args, status_code=LLMStatusCode.LLM_SUCCESS):
54 super().__init__(*args)
55 self._status_code = status_code
57 @property
58 def status_code(self):
59 """
60 获取异常的错误码
61 Returns:
62 异常的错误码
63 """
64 return self._status_code
67_code_2_status = {
68 llm_wrapper.kSuccess: LLMStatusCode.LLM_SUCCESS,
69 llm_wrapper.kFailed: LLMStatusCode.LLM_FAILED,
70 llm_wrapper.kParamInvalid: LLMStatusCode.LLM_PARAM_INVALID,
71 llm_wrapper.kLLMWaitProcessTimeOut: LLMStatusCode.LLM_WAIT_PROCESS_TIMEOUT,
72 llm_wrapper.kLLMKVCacheNotExist: LLMStatusCode.LLM_KV_CACHE_NOT_EXIST,
73 llm_wrapper.kLLMRepeatRequest: LLMStatusCode.LLM_REPEAT_REQUEST,
74 llm_wrapper.kLLMRequestAlreadyCompleted: LLMStatusCode.LLM_REQUEST_ALREADY_COMPLETED,
75 llm_wrapper.kLLMEngineFinalized: LLMStatusCode.LLM_ENGINE_FINALIZED,
76 llm_wrapper.kLLMNotYetLink: LLMStatusCode.LLM_NOT_YET_LINK,
77 llm_wrapper.kAlreadyLink: LLMStatusCode.LLM_ALREADY_LINK,
78 llm_wrapper.kLinKFailed: LLMStatusCode.LLM_LINK_FAILED,
79 llm_wrapper.kUnlinkFailed: LLMStatusCode.LLM_UNLINK_FAILED,
80 llm_wrapper.kNotifyPromptUnlinkFailed: LLMStatusCode.LLM_NOTIFY_PROMPT_UNLINK_FAILED,
81 llm_wrapper.kLLMClusterNumExceedLimit: LLMStatusCode.LLM_CLUSTER_NUM_EXCEED_LIMIT,
82 llm_wrapper.kProcessingLink: LLMStatusCode.LLM_PROCESSING_LINK,
83 llm_wrapper.kDeviceOutOfMemory: LLMStatusCode.LLM_DEVICE_OUT_OF_MEMORY,
84 llm_wrapper.kPrefixAlreadyExist: LLMStatusCode.LLM_PREFIX_ALREADY_EXIST,
85 llm_wrapper.kPrefixNotExist: LLMStatusCode.LLM_PREFIX_NOT_EXIST,
86 llm_wrapper.kLLMSeqLenOverLimit: LLMStatusCode.LLM_SEQ_LENOVERLIMIT,
87 llm_wrapper.kLLMNoFreeBlock: LLMStatusCode.LLM_NO_FREE_BLOCK,
88 llm_wrapper.kLLMBlocksOutOfMemory: LLMStatusCode.LLM_BLOCKS_OUT_OF_MEMORY,
89 llm_wrapper.kLLMExistLink: LLMStatusCode.LLM_EXIST_LINK,
90 llm_wrapper.kLLMFeatureNotEnabled: LLMStatusCode.LLM_FEATURE_NOT_ENABLED,
91 llm_wrapper.kLLMTimeout: LLMStatusCode.LLM_TIMEOUT,
92 llm_wrapper.kLLMLinkBusy: LLMStatusCode.LLM_LINK_BUSY,
93 llm_wrapper.kLLMOutOfMemory: LLMStatusCode.LLM_OUT_OF_MEMORY,
94 llm_wrapper.kLLMDeviceMemError: LLMStatusCode.LLM_DEVICE_MEM_ERROR,
95 llm_wrapper.kLLMSuspectRemoteError: LLMStatusCode.LLM_SUSPECT_REMOTE_ERROR,
96}
99class Status:
100 def __init__(self, status):
101 self._status_code = status
103 def is_ok(self):
104 """
105 是否成功
106 Returns:
107 是否成功
108 """
109 return self._status_code == LLMStatusCode.LLM_SUCCESS
111 @property
112 def status_code(self):
113 """
114 获取错误码
115 Returns:
116 错误码
117 """
118 return self._status_code
121def code_2_status(status) -> LLMStatusCode:
122 return _code_2_status[status] if status in _code_2_status else LLMStatusCode.LLM_FAILED
125def handle_llm_status(status, func_name, other_info):
126 if status != int(llm_wrapper.kSuccess):
127 raise LLMException(
128 f"{func_name} failed, error code is {code_2_status(status)}, {other_info}.",
129 status_code=code_2_status(status),
130 )
133def raise_if_false(pred, fmt, *args, status_code=LLMStatusCode.LLM_PARAM_INVALID, **kwargs):
134 if not pred:
135 error_msg = fmt.format(*args, **kwargs)
136 log.error(error_msg)
137 raise LLMException(error_msg, status_code=status_code)
140def raise_if_true(pred, fmt, *args, status_code=LLMStatusCode.LLM_PARAM_INVALID, **kwargs):
141 if pred:
142 error_msg = fmt.format(*args, **kwargs)
143 log.error(error_msg)
144 raise LLMException(error_msg, status_code=status_code)