FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
GLiNER.cpp/src/model.cpp at main · Knowledgator/GLiNER.cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Knowledgator
/
GLiNER.cpp
Public
Notifications
You must be signed in to change notification settings
Fork
8
Star
50
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
GLiNER.cpp
/
src
/
model.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
143 lines (123 loc) · 4.62 KB
Breadcrumbs
GLiNER.cpp
/
src
/
model.cpp
Copy path
File metadata and controls
143 lines (123 loc) · 4.62 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#
include
<
iostream
>
#
include
"
GLiNER/model.hpp
"
#
if
defined(_WIN32)
#
include
<
string
>
inline
std::wstring
toWstring
(
const
std::string& path) {
return
std::wstring
(path.
begin
(), path.
end
());
}
#
endif
using
namespace
gliner
;
Model::Model
(
const
std::string& model_path,
const
std::string& tokenizer_path,
const
Config& config
) : modelPath(model_path), config(config)
{
env =
new
Ort::Env
(
ORT_LOGGING_LEVEL_WARNING
,
"
gliner
"
);
sessionOptions =
new
Ort::SessionOptions
();
#
if
defined(_WIN32)
session =
new
Ort::Session
(*env,
toWstring
(modelPath).
c_str
(), *sessionOptions);
#
else
session =
new
Ort::Session
(*env, modelPath.
data
(), *sessionOptions);
#
endif
initialize
(tokenizer_path);
}
Model::Model
(
const
std::string& path,
const
std::string& tokenizer_path,
const
Config& config,
const
int
device_id
) : modelPath(path), config(config)
{
env =
new
Ort::Env
(
ORT_LOGGING_LEVEL_WARNING
,
"
gliner
"
);
sessionOptions =
new
Ort::SessionOptions
();
useDevice
(sessionOptions, device_id);
#
if
defined(_WIN32)
session =
new
Ort::Session
(*env,
toWstring
(modelPath).
c_str
(), *sessionOptions);
#
else
session =
new
Ort::Session
(*env, modelPath.
data
(), *sessionOptions);
#
endif
initialize
(tokenizer_path);
}
Model::Model
(
const
std::string& path,
const
std::string& tokenizer_path,
const
Config& config,
const
Ort::Env& env,
const
Ort::SessionOptions& session_options
) : modelPath(path), config(config)
{
#
if
defined(_WIN32)
session =
new
Ort::Session
(env,
toWstring
(modelPath).
c_str
(), session_options);
#
else
session =
new
Ort::Session
(env, modelPath.
data
(), session_options);
#
endif
initialize
(tokenizer_path);
}
Model::~Model
() {
if
(env !=
nullptr
) {
delete
env;
}
if
(sessionOptions !=
nullptr
) {
delete
sessionOptions;
}
delete
session;
delete
processor;
delete
decoder;
}
bool
Model::checkInputs
(
const
std::vector<std::string>& texts,
const
std::vector<std::string>& entities) {
return
!texts.
empty
() && !entities.
empty
();
}
void
Model::initialize
(
const
std::string& tokenizer_path) {
switch
(config.
modelType
){
case
TOKEN_LEVEL
:
processor =
new
TokenProcessor
(config, tokenizer_path);
decoder =
new
TokenDecoder
();
inputNames = {
"
input_ids
"
,
"
attention_mask
"
,
"
words_mask
"
,
"
text_lengths
"
};
outputNames = {
"
logits
"
};
break
;
case
SPAN_LEVEL
:
processor =
new
SpanProcessor
(config, tokenizer_path);
decoder =
new
SpanDecoder
();
inputNames = {
"
input_ids
"
,
"
attention_mask
"
,
"
words_mask
"
,
"
text_lengths
"
,
"
span_idx
"
,
"
span_mask
"
};
outputNames = {
"
logits
"
};
break
;
}
}
void
Model::useDevice
(Ort::SessionOptions* session_options,
const
int
device_id) {
if
(device_id >=
0
) {
OrtCUDAProviderOptions cuda_options;
cuda_options.
device_id
=
0
;
session_options->
AppendExecutionProvider_CUDA
(cuda_options);
}
}
int64_t
Model::count_total_elements
(std::vector<
int64_t
>& output_shape) {
int64_t
total_elements =
1
;
for
(
int64_t
i : output_shape) {
total_elements *= i;
}
return
total_elements;
}
void
Model::run
(
const
std::vector<Ort::Value>& input_tensors, std::vector<
float
>& output) {
std::vector<Ort::Value> modelOutputs = session->
Run
(
Ort::RunOptions
(), inputNames.
data
(),
input_tensors.
data
(), inputNames.
size
(),
outputNames.
data
(), outputNames.
size
()
);
Ort::Value& output_tensor = modelOutputs[
0
];
Ort::TensorTypeAndShapeInfo output_info = output_tensor.
GetTensorTypeAndShapeInfo
();
std::vector<
int64_t
> output_shape = output_info.
GetShape
();
const
float
* output_data = output_tensor.
GetTensorData
<
float
>();
output = std::vector<
float
>(output_data, output_data +
count_total_elements
(output_shape));
}
std::vector<std::vector<Span>>
Model::inference
(
const
std::vector<std::string>& texts,
const
std::vector<std::string>& entities,
bool
flatNer,
float
threshold,
bool
multiLabel
) {
if
(!
checkInputs
(texts, entities)) {
std::cerr <<
"
WARNING! Empty texts or entities.
"
<< std::endl;
return
{};
}
Ort::MemoryInfo memory_info =
Ort::MemoryInfo::CreateCpu
(OrtArenaAllocator, OrtMemTypeDefault);
std::vector<
float
> output;
//
std::vector<Prompt> prompts; // TODO: delete
Batch* batch = processor->
prepareBatch
(texts, entities);
std::vector<Ort::Value> input_tensors;
batch->
tensors
(input_tensors, memory_info);
run
(input_tensors, output);
auto
decoded = decoder->
decode
(
batch, texts, entities, output, flatNer, threshold, multiLabel
);
delete
batch;
return
decoded;
}
Back
|
FazBrowse Home
|
New Git URL