Caffe——when a blob will copy data

本文详细解析了Caffe框架中Blob数据在CPU与GPU间交互的过程,阐述了何时需要进行数据拷贝及原因,并解释了.gpu_data()、.cpu_data()与.mutable_*()函数的使用场景。
部署运行你感兴趣的模型镜像

Caffe官网原文:

// Assuming that data are on the CPU initially, and we have a blob.
 const Dtype* foo;
 Dtype* bar;
 foo = blob.gpu_data(); // data copied cpu->gpu.
 foo = blob.cpu_data(); // no data copied since both have up-to-date contents.
 bar = blob.mutable_gpu_data(); // no data copied.
 // ... some operations ...
 bar = blob.mutable_gpu_data(); // no data copied when we are still on GPU.
 foo = blob.cpu_data(); // data copied gpu->cpu, since the gpu side has modified the data
 foo = blob.gpu_data(); // no data copied since both have up-to-date contents
//
 bar = blob.mutable_cpu_data(); // still no data copied.
 bar = blob.mutable_gpu_data(); // data copied cpu->gpu.
 bar = blob.mutable_cpu_data(); // data copied gpu->cpu
为什么有的地方需要data copy ,有点地方不需要??

首先需明确:

.gpu_data and .cpu_data are used in cases were the data is used only as input and will not be modified by the algorithm. .mutable_* is used when the data itself gets updated while running the algorithm.

其次,需要关注(1)对数据Blob的两次操作是否采用相同的处理器(processor),(2)之前的一次操作是否有可能更新数据Blob

Whenever a the data is called, it checks whether the previous statement was a mutable_* function call and that too using the same processor (gpu or cpu). If it is using the same processor, data need not be copied. If it is using the other processor, there is a chance that the data might have been updated in the previous .mutable_* call and hence a data copy is required.


您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值