Commit 1783c6b6 authored by root's avatar root
Browse files

add new file

parent 606c9edc
import numpy as np
import tritonclient.http as httpclient
# 1. 初始化客户端
client = httpclient.InferenceServerClient(url="localhost:8000")
# 2. 准备两个 2x2 的随机矩阵(为了显示清晰,用较小的尺寸)
A = np.random.randint(1, 10, size=(2, 2)).astype(np.int32)
B = np.random.randint(1, 10, size=(2, 2)).astype(np.int32)
# 3. 封装 Triton 输入
inputs = [
httpclient.InferInput("INPUT0", A.shape, "INT32"),
httpclient.InferInput("INPUT1", B.shape, "INT32")
]
inputs[0].set_data_from_numpy(A)
inputs[1].set_data_from_numpy(B)
# 4. 执行推理
response = client.infer(model_name="add", inputs=inputs)
C = response.as_numpy("OUTPUT0")
# 5. 格式化打印 A + B = C
print("--- 矩阵加法结果 ---")
# 使用 numpy 的 array2string 配合自定义格式
def fmt(mat): return np.array2string(mat, precision=1, separator=', ')
print(f"{fmt(A)}\n\n +\n\n{fmt(B)}\n\n =\n\n{fmt(C)}")
# 如果你想要更紧凑的同行对比 (仅限小矩阵)
print("\n--- 同行对比 (A + B = C) ---")
for i in range(len(A)):
plus = "+" if i == len(A)//2 else " "
equal = "=" if i == len(A)//2 else " "
print(f"{A[i]} {plus} {B[i]} {equal} {C[i]}")
import triton_python_backend_utils as pb_utils
class TritonPythonModel:
def initialize(self, args):
"""模型初始化时调用(可选)"""
print("initialized")
def execute(self, requests):
responses = []
for request in requests:
#获取输入参数
in_0 = pb_utils.get_input_tensor_by_name(request, "INPUT0").as_numpy()
in_1 = pb_utils.get_input_tensor_by_name(request, "INPUT1").as_numpy()
# 逻辑计算
out_tensor = pb_utils.Tensor("OUTPUT0", in_0 + in_1)
# 封装并返回
responses.append(pb_utils.InferenceResponse([out_tensor]))
return responses
def finalize(self):
"""模型卸载时调用(可选)"""
print('Cleaning up...')
name: "add"
backend: "python"
input [
{ name: "INPUT0", data_type: TYPE_INT32, dims: [ -1, -1 ] },
{ name: "INPUT1", data_type: TYPE_INT32, dims: [ -1, -1 ] }
]
output [
{ name: "OUTPUT0", data_type: TYPE_INT32, dims: [ -1, -1 ] }
]
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