Questions on performance of to_tensor and from_tensor on GPU

最近用太极实现了P2G和G2P,并准备替代之前我自己的PyTorch实现的P2G和G2P。比较二者的性能的时候,发现太极的P2G和G2P本身性能远高于PyTorch版本的,但是使用to_torch和from_torch在PyTorch tensor和taichi field之间转移数据的运行时间却远高于P2G和G2P本身。由于需要和其他的PyTorch代码一同使用,每一次P2G和G2P的计算都要使用to_torch和from_torch,导致性能并没有显著提升。另外, 发现转移在CPU上的数据会快于转移GPU上的数据。例如:

import torch
import taichi as ti
import time

def main():
    device = "cuda"
    batch_size = 8
    n_particles = 450
    if device == "cpu":
        arch = ti.cpu
    else:
        arch = ti.cuda
    ti.init(arch=arch)
    tensor = torch.rand((100, batch_size, n_particles, 2), device=device)
    field = ti.Vector.field(2, dtype=ti.f32, shape=(batch_size, n_particles))

    start = time.perf_counter()
    for i in range(100):
        field.from_torch(tensor[i])
    end = time.perf_counter()
    print("Time per copy: {:.3f}[s]".format((end - start) / 100))


if __name__ == "__main__":
    main()

当device是“cpu”时,我的机器上输出的时间是0.0049s; 当device是“gpu”时,是0.0113s。请问这是在GPU上转移数据本身就存在的问题还是我使用to_tensor和from_tensor不当?

以下是我机器的一些相关信息:
taichi: 0.7.10
Pytorch: 1.7.0+cu101
GPU: NVidia GeForce RTX 2080Ti
CPU: Intel Core i9-9900K
OS: Windows 10