| 3094 | } |
| 3095 | |
| 3096 | Maybe<Tensor> GlobalTensorTo(const std::shared_ptr<Tensor>& x, const std::string& device_type, |
| 3097 | const Symbol<DType>& dtype, const bool& copy) { |
| 3098 | std::shared_ptr<Tensor> tensor; |
| 3099 | auto input_placement = JUST(x->parallel_desc()); |
| 3100 | std::string input_device_tag = input_placement->device_tag(); |
| 3101 | if (input_device_tag == "gpu") { input_device_tag = "cuda"; } |
| 3102 | if (device_type == input_device_tag) { |
| 3103 | if (dtype == x->dtype()) { |
| 3104 | return (copy ? JUST(x->clone()) : x); |
| 3105 | } else { |
| 3106 | return JUST(Cast(x, dtype, /*pin_memory=*/false)); |
| 3107 | } |
| 3108 | } |
| 3109 | if (LazyMode::is_enabled()) { |
| 3110 | if (dtype != x->dtype()) { tensor = JUST(Cast(x, dtype, /*pin_memory=*/false)); } |
| 3111 | if (device_type != JUST(x->parallel_desc())->device_tag()) { |
| 3112 | tensor = JUST(Copy(tensor ? tensor : x, device_type, 0, /*pin_memory=*/false)); |
| 3113 | } |
| 3114 | return tensor; |
| 3115 | } else { |
| 3116 | CheckMetaConsistency(x).GetOrThrow(); |
| 3117 | auto placement = JUST(ReplacePlacementDeviceTag(input_placement, device_type)); |
| 3118 | auto nd_sbp = JUST(x->nd_sbp()); |
| 3119 | std::vector<Symbol<SbpParallel>> sbp_tuple(nd_sbp->sbp_parallel().size()); |
| 3120 | for (int i = 0; i < sbp_tuple.size(); ++i) { sbp_tuple[i] = nd_sbp->sbp_parallel().Get(i); } |
| 3121 | tensor = JUST(GlobalToLocal(x, /*copy=*/false)); |
| 3122 | Symbol<Device> device = JUST(Device::New(device_type)); |
| 3123 | tensor = JUST(LocalTensorTo(tensor, device, dtype, copy)); |
| 3124 | JUST(tensor->set_requires_grad(x->requires_grad())); |
| 3125 | return JUST(LocalToGlobal(tensor, placement, sbp_tuple, *(x->shape()), dtype, |
| 3126 | /* sync_data */ true, /*copy=*/false)); |
| 3127 | } |
| 3128 | } |
| 3129 | |
| 3130 | } // namespace |
| 3131 |
no test coverage detected