class_docstr: 测试一些工具函数
class_docstr_tok: ["\u6d4b\u8bd5\u4e00\u4e9b\u5de5\u5177\u51fd\u6570"]
docstr: 测试 _tensor_to_object 和 _object_to_tensor 二者的结果能否互相转换
docstr_tok: ["\u6d4b\u8bd5","_tensor_to_object","\u548c","_object_to_tensor","\u4e8c\u8005\u7684\u7ed3\u679c\u80fd\u5426\u4e92\u76f8\u8f6c\u6362"]
code: def test_tensor_object_transfer_tensor(self, device):
      """
      测试 _tensor_to_object 和 _object_to_tensor 二者的结果能否互相转换
      """
      # 张量
      paddle_tensor = paddle.rand((3, 4, 5)).cpu()
      obj_tensor, size = _object_to_tensor(paddle_tensor, device=device)
      res = _tensor_to_object(obj_tensor, size)
      assert paddle.equal_all(res, paddle_tensor)
  
      # 列表
      paddle_list = [paddle.rand((6, 4, 2)) for i in range(10)]
      obj_tensor, size = _object_to_tensor(paddle_list, device=device)
      res = _tensor_to_object(obj_tensor, size)
      assert isinstance(res, list)
      for before, after in zip(paddle_list, res):
          assert paddle.equal_all(after, before)
  
      # 元组
      paddle_list = [paddle.rand((6, 4, 2)) for i in range(10)]
      paddle_tuple = tuple(paddle_list)
      obj_tensor, size = _object_to_tensor(paddle_tuple, device=device)
      res = _tensor_to_object(obj_tensor, size)
      assert isinstance(res, tuple)
      for before, after in zip(paddle_list, res):
          assert paddle.equal_all(after, before)
  
      # 字典
      paddle_dict = {
          "tensor": paddle.rand((3, 4)),
          "list": [paddle.rand((6, 4, 2)) for i in range(10)],
          "dict": {
              "list": [paddle.rand((6, 4, 2)) for i in range(10)],
              "tensor": paddle.rand((3, 4)),
          },
          "int": 2,
          "string": "test string",
      }
      obj_tensor, size = _object_to_tensor(paddle_dict, device=device)
      res = _tensor_to_object(obj_tensor, size)
      assert isinstance(res, dict)
      assert paddle.equal_all(res["tensor"], paddle_dict["tensor"])
      assert isinstance(res["list"], list)
      for before, after in zip(paddle_dict["list"], res["list"]):
          assert paddle.equal_all(after, before)
  
      assert isinstance(res["dict"], dict)
      assert paddle.equal_all(
          res["dict"]["tensor"], paddle_dict["dict"]["tensor"]
      )
      for before, after in zip(
          paddle_dict["dict"]["list"], res["dict"]["list"]
      ):
          assert paddle.equal_all(after, before)
      assert res["int"] == paddle_dict["int"]
      assert res["string"] == paddle_dict["string"]
  
file_path: fastNLP/tests/core/drivers/paddle_driver/test_dist_utils.py
imports: import os
  import sys
  import signal
  import pytest
  import traceback
  import numpy
  from fastNLP.core.drivers.paddle_driver.dist_utils import _tensor_to_object, _object_to_tensor, fastnlp_paddle_all_gather, fastnlp_paddle_broadcast_object
  from fastNLP.core.drivers.paddle_driver.fleet_launcher import FleetLauncher
  from tests.helpers.utils import magic_argv_env_context
  from fastNLP.envs.imports import _NEED_IMPORT_PADDLE
label_desc: Use image node_id 1 for calling the TestDistUtilsTools obj's underlying member method code with example usage: obj.test_tensor_object_transfer_tensor(device) without return types