Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wjwever
triton-server-dcu-example
Commits
1783c6b6
Commit
1783c6b6
authored
Apr 15, 2026
by
root
Browse files
add new file
parent
606c9edc
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
0 deletions
+70
-0
python-repo/client.py
python-repo/client.py
+35
-0
python-repo/model-repo/add/1/model.py
python-repo/model-repo/add/1/model.py
+25
-0
python-repo/model-repo/add/config.pbtxt
python-repo/model-repo/add/config.pbtxt
+10
-0
No files found.
python-repo/client.py
0 → 100644
View file @
1783c6b6
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
]
}
"
)
python-repo/model-repo/add/1/model.py
0 → 100644
View file @
1783c6b6
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...'
)
python-repo/model-repo/add/config.pbtxt
0 → 100644
View file @
1783c6b6
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 ] }
]
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment