-
Notifications
You must be signed in to change notification settings - Fork 20
/
tfg2p.cpp
65 lines (42 loc) · 1.32 KB
/
tfg2p.cpp
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
#include "tfg2p.h"
TFG2P::TFG2P()
{
G2P = nullptr;
}
TFG2P::TFG2P(const std::string &SavedModelFolder)
{
G2P = nullptr;
Initialize(SavedModelFolder);
}
bool TFG2P::Initialize(const std::string &SavedModelFolder)
{
try {
G2P = new cppflow::model(SavedModelFolder);
}
catch (...) {
G2P = nullptr;
return false;
}
return true;
}
TFTensor<int32_t> TFG2P::DoInference(const std::vector<int32_t> &InputIDs, float Temperature)
{
if (!G2P)
throw std::exception("Tried to do inference on unloaded or invalid model!");
// Convenience reference so that we don't have to constantly derefer pointers.
cppflow::model& Mdl = *G2P;
// Convenience reference so that we don't have to constantly derefer pointers.
cppflow::tensor input_ids{ InputIDs, std::vector<int64_t>{(int64_t)InputIDs.size()}};
cppflow::tensor input_len{(int32_t)InputIDs.size()};
cppflow::tensor input_temp{Temperature};
auto Outs = Mdl({{"serving_default_input_ids:0",input_ids},
{"serving_default_input_len:0",input_len},
{"serving_default_input_temperature:0",input_temp}},{"StatefulPartitionedCall:0"});
TFTensor<int32_t> RetTensor = VoxUtil::CopyTensor<int32_t>(Outs[0]);
return RetTensor;
}
TFG2P::~TFG2P()
{
if (G2P)
delete G2P;
}