-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathLoadWeights.cpp
87 lines (77 loc) · 2.47 KB
/
LoadWeights.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//--------------------------------------------------------------------------------------
// LoadWeights.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. Copyright (C) NVIDIA Corporation. All rights reserved.
// Licensed under the MIT License.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "LoadWeights.h"
#include <iostream>
#include <string>
#include <fstream>
namespace
{
const int c_bufferLength = 256;
}
// Loads weight values from a binary file.
bool LoadWeights(const std::string& fpath, WeightMapType& weightMap)
{
std::ifstream input(fpath, std::ifstream::binary);
if (!(input) || !(input.good()) || !(input.is_open()))
{
std::cerr << "Unable to open weight file: " << fpath << std::endl;
return false;
}
int32_t count;
try
{
input.read(reinterpret_cast<char*>(&count), 4);
}
catch (const std::ifstream::failure&)
{
std::cerr << "Invalid weight map file: " << fpath << std::endl;
return false;
}
if (count < 0)
{
std::cerr << "Invalid weight map file: " << fpath << std::endl;
return false;
}
std::cout << "Number of weight tensors: " + std::to_string(count) << std::endl;
uint32_t name_len;
uint32_t w_len;
char name_buf[c_bufferLength];
try
{
while (count--)
{
input.read(reinterpret_cast<char*>(&name_len), sizeof(uint32_t));
if (name_len > c_bufferLength - 1)
{
std::cerr << "name_len exceeds c_bufferLength: " << name_len
<< " vs " << c_bufferLength - 1 << std::endl;
return false;
}
input.read(name_buf, name_len);
name_buf[name_len] = '\0';
std::string name(name_buf);
input.read(reinterpret_cast<char*>(&w_len), sizeof(uint32_t));
weightMap[name] = WeightsType(w_len);
input.read(reinterpret_cast<char*>(weightMap[name].data()), sizeof(float) * w_len);
std::cout << "Loaded tensor: " + name + " -> " + std::to_string(w_len) << std::endl;
}
input.close();
}
catch (const std::ifstream::failure&)
{
std::cerr << "Invalid tensor data" << std::endl;
return false;
}
catch (const std::out_of_range&)
{
std::cerr << "Invalid tensor format" << std::endl;
return false;
}
return true;
}