-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.h
60 lines (54 loc) · 1.83 KB
/
utils.h
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
#include "tensor.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#ifdef FR_ENABLE_ANDROID_ASSET
#include <android/asset_manager.h>
#endif
#include <check.h>
#define LOG_INFO(msg) std::cout << "[INFO] " << msg << std::endl;
inline bool file_exists(const std::string &path) {
std::ifstream file(path);
return file.good();
}
inline std::string read_file(const std::string &_path,
void *_asset_manager = nullptr) {
if (_path.substr(0, 6) == "asset:") {
#ifdef FR_ENABLE_ANDROID_ASSET
AAssetManager *asset_manager = static_cast<AAssetManager *>(_asset_manager);
RV_CHECK(asset_manager != nullptr) << "Asset manager is not provided";
const std::string path = _path.substr(6);
AAsset *asset =
AAssetManager_open(asset_manager, path.c_str(), AASSET_MODE_STREAMING);
RV_CHECK(asset != nullptr) << "Asset \"" << path << "\" not found";
std::stringstream ss;
char buf[BUFSIZ];
int nb_read = 0;
while ((nb_read = AAsset_read(asset, buf, BUFSIZ)) > 0) {
ss.write(buf, nb_read);
}
AAsset_close(asset);
return ss.str();
#else
RV_UNIMPLEMENTED() << "An asset path is specified, but Android asset is "
"not enabled. Please check your build settings.";
#endif
} else {
const std::string &path = _path;
RV_CHECK(file_exists(path)) << "File \"" << path << "\" does not exist";
std::ifstream file(path);
std::stringstream ss;
ss << file.rdbuf();
return ss.str();
}
}
namespace rwkv {
namespace utils {
LengthType indices_to_offset(const Shape &shape,
const std::vector<LengthType> &indices);
void offset_to_indices(LengthType offset, const Shape &shape,
std::vector<LengthType> &indices);
} // namespace utils
} // namespace rwkv