-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshow_dumped_data.cpp
More file actions
136 lines (119 loc) · 3.68 KB
/
show_dumped_data.cpp
File metadata and controls
136 lines (119 loc) · 3.68 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
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
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <stdexcept>
#include <cassert>
#include <type_traits>
#include "type_convert.hpp"
template <typename T>
bool readBufferFromFile(T* data, size_t dataNumItems, const std::string& fileName)
{
std::ifstream infile(fileName, std::ios::binary);
if(infile)
{
infile.read(reinterpret_cast<char*>(data), dataNumItems * sizeof(T));
infile.close();
}
else
{
throw std::runtime_error("could not open the file for reading");
}
return true;
}
template <typename T>
void show_integer_data_from_file(int numElements, const char* filename)
{
T* dataBuffer = new T[numElements];
if(readBufferFromFile(dataBuffer, numElements, filename))
{
for(int i = 0; i < numElements; i++)
{
if constexpr(sizeof(T) >= 2)
std::cout << i << " : " << dataBuffer[i] << std::endl;
else
std::cout << i << " : " << static_cast<int>(dataBuffer[i]) << std::endl;
};
};
delete[] dataBuffer;
};
template <typename T>
void show_floating_point_data_from_file(int numElements, const char* filename)
{
T* dataBuffer = new T[numElements];
if(readBufferFromFile(dataBuffer, numElements, filename))
{
std::cout << std::fixed << std::setprecision(11);
for(int i = 0; i < numElements; i++)
{
float value = type_convert<float>(dataBuffer[i]);
if(std::isinf(value) || std::isnan(value))
{
std::cout << i << " : "
<< "invalid value" << std::endl;
continue;
}
std::cout << i << ": " << value << std::endl;
};
};
delete[] dataBuffer;
};
int main(int argc, char* argv[])
{
if(argc != 4)
{
std::cerr << "Usage: " << argv[0]
<< " <fp64/fp32/fp16/bf16/int32/int16/int8/uint32/uint16/uint8> <file> <number "
"of elements> "
<< std::endl;
throw std::runtime_error("Invalid command argument!");
};
std::string precision(argv[1]);
if(precision != "fp64" && precision != "fp32" && precision != "fp16" && precision != "bf16" &&
precision != "int8" && precision != "int32" && precision != "int16" &&
precision != "uint32" && precision != "uint16" && precision != "uint8")
throw std::runtime_error("Invalid precison of input data specified!");
int numElements = atoi(argv[3]);
assert(numElements > 0);
if(precision == "fp64")
{
show_floating_point_data_from_file<double>(numElements, argv[2]);
}
else if(precision == "fp32")
{
show_floating_point_data_from_file<float>(numElements, argv[2]);
}
else if(precision == "fp16")
{
show_floating_point_data_from_file<half_t>(numElements, argv[2]);
}
else if(precision == "bf16")
{
show_floating_point_data_from_file<bhalf_t>(numElements, argv[2]);
}
else if(precision == "int32")
{
show_integer_data_from_file<int32_t>(numElements, argv[2]);
}
else if(precision == "uint32")
{
show_integer_data_from_file<uint32_t>(numElements, argv[2]);
}
else if(precision == "int16")
{
show_integer_data_from_file<int16_t>(numElements, argv[2]);
}
else if(precision == "uint16")
{
show_integer_data_from_file<uint16_t>(numElements, argv[2]);
}
else if(precision == "int8")
{
show_integer_data_from_file<int8_t>(numElements, argv[2]);
}
else if(precision == "uint8")
{
show_integer_data_from_file<uint8_t>(numElements, argv[2]);
}
};