training become slow?训练速度奇怪的变慢?tcmalloc在tensorflow中的使用!

本文主要是介绍training become slow?训练速度奇怪的变慢?tcmalloc在tensorflow中的使用!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

--------------------前言------------------------

在训练视频分类训练的时候,发现tensorflow莫名的变慢2~5 sec /batch, 之前一直是0.4 sec/batch, 联想到最早之前mxnet训练分类时候的类似情况,决定做排查(都是同一台训练服务器上):

(1)杀掉一些僵尸进程或多并行进程,eg. im2rec, 发现不见效,并且cpu利用率也不高,排除cpu性能的影响;

(2)杀掉一些系统进程,kworker, falcon-agent, gpu-monitor等,排除系统进程对训练性能的影响;

(3)iostat -x查看IO并不高,同时测试了image data loader,虽然性能有变化,但不是主要瓶颈占比,python cProfile热点分析瓶颈在session.run(),htop/top发现占用内存不多,但cache很大;

(4)top cpu利用率并不理想(与我设置的线程数不般配),猜测是多线程竞争引起,具体什么原因,不清楚;

(5)github搜索一些解决方法,tcmalloc有效(之前最简单的方法是重启,现在我没这么尝试^-^),总结如下:

一,----------------重载new与delete---------------------

首先,想明确一个c++中的概念,操作符的重载.直观印象中,都认为可重载的操作符是++, --, +, -, *, /,(),[]等,其实,操作符中有两个比较特殊的:operator new和operator delete,这两个都是可以重载的,重载的他们两个的主要目的是更好的内存管理(内存池);

其次,明确内存池(memory pool)的概念和应用.操作系统原生的memory management经常不满足要求,尤其在大型的客户端程序中.大型程序中我们经常new(malloc)与delete(free),会造成时间成本开销,同时造成内存碎片memory fragmentation(碎片的概念请自行google),另外由于程序复杂,出现内存泄露也不好定位和查找(),此时内存池便应运而生,内存池经常需要重载new delete操作符,对windows中MFC开发熟悉的想必对如下宏定义不陌生:

        #ifdef _DEBUG# define DEBUG_NEW new(THIS_FILE, __LINE__)#else# define DEBUG_NEW new#endif

MFC重载了new,从而方便的定位new的位置及文件路径,方便查看内存泄露.另一个例子如下:

#include <stdio.h>
#include<stdlib.h>//重载全局new
void *operator new(size_t size)
{printf("operator new: allocate %d bytes\n", size);void *ptr = malloc(sz);//在这里也可以方便的操作自己的内存池return ptr;
}//重载全局delete
void operator delete(void *ptr)
{puts("operator delete");free(ptr);
}int main( )
{int *p=new int(0);//使用重载的operator new,不再使用系统的operator newdelete p;return 0;
}

今天我们所说的tcmalloc或jemalloc就是类似的原理,如果选择了tcmalloc,则我们程序中会采用tcmalloc中的内存管理机制

二,---------------------tcmalloc是什么?-----------------

tcmalloc是一个内存分配器,管理堆内存,主要影响malloc和free,用于降低频繁分配、释放内存造成的性能损耗,并且有效地控制内存碎片。glibc中的内存分配器是ptmalloc2,tcmalloc要比它快。一次malloc和free操作,ptmalloc需要300ns,而tcmalloc只要50ns。同时,tcmalloc也优化了小对象的存储,需要更少的空间。tcmalloc特别对多线程做了优化,对于小对象的分配基本上是不存在锁竞争(lock contention),而大对象使用了细粒度、高效的自旋锁(spinlock)。分配给线程的本地缓存,在长时间空闲的情况下会被回收,供其他线程使用,这样提高了在多线程情况下的内存利用率,不会浪费内存,而这一点ptmalloc2是做不到的。

以下摘自gperftools文档,说出了tcmalloc的关键:

TCMalloc : Thread-Caching MallocMotivationTCMalloc is faster than the glibc 2.3 malloc (available as a separate library called ptmalloc2) and other mallocs that I have tested. ptmalloc2 takes approximately 300 nanoseconds to execute a malloc/free pair on a 2.8 GHz P4 (for small objects). The TCMalloc implementation takes approximately 50 nanoseconds for the same operation pair. Speed is important for a malloc implementation because if malloc is not fast enough, application writers are inclined to write their own custom free lists on top of malloc. This can lead to extra complexity, and more memory usage unless the application writer is very careful to appropriately size the free lists and scavenge idle objects out of the free listTCMalloc also reduces lock contention for multi-threaded programs. For small objects, there is virtually zero contention. For large objects, TCMalloc tries to use fine grained and efficient spinlocks. ptmalloc2 also reduces lock contention by using per-thread arenas but there is a big problem with ptmalloc2's use of per-thread arenas. In ptmalloc2 memory can never move from one arena to another. This can lead to huge amounts of wasted space. For example, in one Google application, the first phase would allocate approximately 300MB of memory for its data structures. When the first phase finished, a second phase would be started in the same address space. If this second phase was assigned a different arena than the one used by the first phase, this phase would not reuse any of the memory left after the first phase and would add another 300MB to the address space. Similar memory blowup problems were also noticed in other applications.Another benefit of TCMalloc is space-efficient representation of small objects. For example, N 8-byte objects can be allocated while using space approximately 8N * 1.01 bytes. I.e., a one-percent space overhead. ptmalloc2 uses a four-byte header for each object and (I think) rounds up the size to a multiple of 8 bytes and ends up using 16N bytes.


更详细的tcmalloc可以参见google perf tools文档

http://goog-perftools.sourceforge.net/doc/tcmalloc.html

三,---------------------training become slow----------------

曾几何时,无论是mxnet训练,还是tensorflow训练,你都会奇怪的发现如下现象:

trainning speed random slow down

or, period become slow at each batch,

or, once it has been being very fast for training, but this go today

while I have not change any training code before~~

or, the gpu has a low usage today(首先需要排除是否IO瓶颈引起)

遇到上述的情况,则有可能是cache的问题,正是因为使用系统自带的内存分配机制引起的,

There may be some memory pressure caused by virtual address space fragmentation and high system buffer cache churn (reading large training datasets from the file system)

那么尝试解决:

(1)htop:有效的查看cache的工具:

htop发现cache占用太多,其中Mem的yellow bars代表cache占用情况
htop说明
​​​​

(2)Clear RAM Memory Cache

Linux provides a way to flush or clear ram cache.

How to Clear Cache in Linux?

Every Linux System has three options to clear cache without interrupting any processes or services.

    a. Clear PageCache only.

  # sync; echo 1 > /proc/sys/vm/drop_caches​​​​b. Clear dentries and inodes.

   # sync; echo 2 > /proc/sys/vm/drop_caches

     c. Clear PageCache, dentries and inodes.

   # sync; echo 3 > /proc/sys/vm/drop_caches

    sync will flush the file system buffer. writing to drop_cache will clean cache without killing any application/service

    If you have to clear the disk cache, the first command is safest in enterprise and production as “...echo 1 > ….” will clear the PageCache only. It is not recommended to use third option above “...echo 3 >” in production until you know what you are doing, as it will clear PageCachedentries and inodes.

             更详细的说明请参见如下:

             https://www.tecmint.com/clear-ram-memory-cache-buffer-and-swap-space-on-linux/

   (3)使用tcmalloc when training:

             TCMalloc seems to improve training speed and avoids occasional slowdowns seen with the default allocator. You can enable it by installing it and setting LD_PRELOAD=/usr/lib/libtcmalloc.so. 

LD_PRELOAD=/usr/lib/libtcmalloc.so python train.py

             此时,训练中,分配buffer将使用tcmalloc中的内存池,提高效率,加速训练,恢复正常训练速度.

            备注:在部署c++程序中,如果遇到多线程频繁分配释放的情况,频繁操作大内存的情况,也可以通过观测cache,尝试利用tcmalloc改进.

   另外,tcmalloc不能在jni等形式下使用,因为jni有可能会导致先load系统运行时分配内存,后加载tcmalloc释放内存,造成内存冲突(切记在哪个库中分配的内存在哪个库中释放)

这篇关于training become slow?训练速度奇怪的变慢?tcmalloc在tensorflow中的使用!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/388681

相关文章

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Ubuntu如何分配​​未使用的空间

《Ubuntu如何分配​​未使用的空间》Ubuntu磁盘空间不足,实际未分配空间8.2G因LVM卷组名称格式差异(双破折号误写)导致无法扩展,确认正确卷组名后,使用lvextend和resize2fs... 目录1:原因2:操作3:报错5:解决问题:确认卷组名称​6:再次操作7:验证扩展是否成功8:问题已解

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构