MCPcopy Create free account
hub / github.com/Oneflow-Inc/oneflow / GlobalTensorTo

Function GlobalTensorTo

oneflow/core/functional/impl/array_functor.cpp:3096–3128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3094}
3095
3096Maybe<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

Callers 4

operator()Method · 0.85
operator()Method · 0.85
operator()Method · 0.85
operator()Method · 0.85

Calls 15

CopyClass · 0.85
GlobalToLocalClass · 0.85
LocalTensorToFunction · 0.85
NewFunction · 0.70
LocalToGlobalFunction · 0.70
CastClass · 0.50
is_enabledFunction · 0.50
parallel_descMethod · 0.45
dtypeMethod · 0.45
cloneMethod · 0.45
GetOrThrowMethod · 0.45

Tested by

no test coverage detected