Skip to content

Commit 9f33593

Browse files
authored
human readable memory warns (#14361)
* human readable memory warns test=develop * update test=develop * refine test=develop * fix build test=develop
1 parent 9eb0ab1 commit 9f33593

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

paddle/fluid/memory/malloc.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15+
#include <string>
1516
#include <vector>
1617

1718
#include "paddle/fluid/memory/malloc.h"
@@ -21,6 +22,7 @@ limitations under the License. */
2122
#include "paddle/fluid/memory/detail/buddy_allocator.h"
2223
#include "paddle/fluid/memory/detail/system_allocator.h"
2324
#include "paddle/fluid/platform/gpu_info.h"
25+
#include "paddle/fluid/string/printf.h"
2426

2527
DEFINE_bool(init_allocated_mem, false,
2628
"It is a mistake that the values of the memory allocated by "
@@ -137,12 +139,18 @@ void* Alloc<platform::CUDAPlace>(platform::CUDAPlace place, size_t size) {
137139
platform::SetDeviceId(place.device);
138140
size_t avail, total;
139141
platform::GpuMemoryUsage(&avail, &total);
140-
LOG(WARNING) << "Cannot allocate " << size << " bytes in GPU "
141-
<< place.device << ", available " << avail << " bytes";
142+
LOG(WARNING) << "Cannot allocate " << string::HumanReadableSize(size)
143+
<< " in GPU " << place.device << ", available "
144+
<< string::HumanReadableSize(avail);
142145
LOG(WARNING) << "total " << total;
143-
LOG(WARNING) << "GpuMinChunkSize " << buddy_allocator->GetMinChunkSize();
144-
LOG(WARNING) << "GpuMaxChunkSize " << buddy_allocator->GetMaxChunkSize();
145-
LOG(WARNING) << "GPU memory used: " << Used<platform::CUDAPlace>(place);
146+
LOG(WARNING) << "GpuMinChunkSize "
147+
<< string::HumanReadableSize(
148+
buddy_allocator->GetMinChunkSize());
149+
LOG(WARNING) << "GpuMaxChunkSize "
150+
<< string::HumanReadableSize(
151+
buddy_allocator->GetMaxChunkSize());
152+
LOG(WARNING) << "GPU memory used: "
153+
<< string::HumanReadableSize(Used<platform::CUDAPlace>(place));
146154
platform::SetDeviceId(cur_dev);
147155
}
148156
if (FLAGS_init_allocated_mem) {

paddle/fluid/string/printf.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include <iostream>
7373
#include <sstream>
7474
#include <string>
75+
#include <vector>
7576

7677
#include "tinyformat/tinyformat.h" // https://github.com/c42f/tinyformat
7778

@@ -102,5 +103,22 @@ void Printf(const char* fmt, const Args&... args) {
102103
Fprintf(std::cout, fmt, args...);
103104
}
104105

106+
template <typename T>
107+
std::string HumanReadableSize(T size) {
108+
size_t i = 0;
109+
double f_size = static_cast<double>(size);
110+
double orig = f_size;
111+
const std::vector<std::string> units(
112+
{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"});
113+
while (f_size > 1024) {
114+
f_size /= 1024;
115+
i++;
116+
}
117+
if (i >= units.size()) {
118+
return Sprintf("%fB", orig);
119+
}
120+
return Sprintf("%f%s", f_size, units[i]);
121+
}
122+
105123
} // namespace string
106124
} // namespace paddle

0 commit comments

Comments
 (0)