-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_gpu_support.py
More file actions
91 lines (82 loc) · 3.1 KB
/
example_gpu_support.py
File metadata and controls
91 lines (82 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Example demonstrating GPU support in LEKCut
This example shows how to use different execution providers
including GPU acceleration through CUDA and TensorRT.
"""
import onnxruntime as ort
from lekcut import word_tokenize
def main():
# Display available execution providers
print("Available ONNX Runtime Execution Providers:")
providers = ort.get_available_providers()
for i, provider in enumerate(providers, 1):
print(f" {i}. {provider}")
print()
# Example 1: Default execution (CPU)
print("Example 1: Default execution (uses CPU)")
text = "ทดสอบการตัดคำภาษาไทยด้วย LEKCut"
result = word_tokenize(text)
print(f"Input: {text}")
print(f"Output: {result}")
print()
# Example 2: Explicit CPU execution
print("Example 2: Explicit CPU execution")
result = word_tokenize(text, providers=['CPUExecutionProvider'])
print(f"Input: {text}")
print(f"Output: {result}")
print()
# Example 3: GPU execution with CPU fallback
# This will use CUDA if available, otherwise fall back to CPU
print("Example 3: GPU execution (CUDA) with CPU fallback")
result = word_tokenize(text, providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
print(f"Input: {text}")
print(f"Output: {result}")
if 'CUDAExecutionProvider' in providers:
print("Note: Using CUDA GPU acceleration")
else:
print("Note: CUDA not available, using CPU fallback")
print()
# Example 4: TensorRT optimization (for NVIDIA GPUs)
print("Example 4: TensorRT optimization with CUDA and CPU fallback")
result = word_tokenize(
text,
providers=[
'TensorrtExecutionProvider',
'CUDAExecutionProvider',
'CPUExecutionProvider'
]
)
print(f"Input: {text}")
print(f"Output: {result}")
if 'TensorrtExecutionProvider' in providers:
print("Note: Using TensorRT optimization")
elif 'CUDAExecutionProvider' in providers:
print("Note: TensorRT not available, using CUDA")
else:
print("Note: GPU providers not available, using CPU")
print()
# Example 5: Multiple sentences
print("Example 5: Processing multiple sentences")
sentences = [
"สวัสดีครับ",
"ยินดีต้อนรับสู่ LEKCut",
"ห้องสมุดสำหรับตัดคำภาษาไทย"
]
print("Using GPU acceleration with CPU fallback:")
for sentence in sentences:
result = word_tokenize(sentence, providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
print(f" {sentence:40s} -> {result}")
print()
if __name__ == "__main__":
print("=" * 70)
print("LEKCut GPU Support Examples")
print("=" * 70)
print()
main()
print("=" * 70)
print("For more information, see:")
print(" - ONNX Runtime docs: https://onnxruntime.ai/docs/execution-providers/")
print(" - To enable GPU: pip install onnxruntime-gpu")
print("=" * 70)