| 26 | namespace data { |
| 27 | |
| 28 | inline Maybe<void> InitDataSourceDistributedInfo(user_op::KernelInitContext* ctx, |
| 29 | size_t& world_size, int64_t& rank) { |
| 30 | auto nd_sbp_str_vec = ctx->Attr<std::vector<std::string>>("nd_sbp"); |
| 31 | if (nd_sbp_str_vec.empty()) { |
| 32 | world_size = GlobalProcessCtx::WorldSize(); |
| 33 | rank = GlobalProcessCtx::Rank(); |
| 34 | } else { |
| 35 | const Shape& hierarchy = *ctx->parallel_desc().hierarchy(); |
| 36 | CHECK_EQ_OR_RETURN(hierarchy.NumAxes(), nd_sbp_str_vec.size()); |
| 37 | rank = 0; |
| 38 | world_size = 1; |
| 39 | |
| 40 | using index_helper_t = NdIndexOffsetHelper<int64_t, SHAPE_MAX_AXIS_SIZE>; |
| 41 | index_helper_t index_helper(hierarchy.dim_vec().data(), hierarchy.NumAxes()); |
| 42 | int64_t nd_index[SHAPE_MAX_AXIS_SIZE] = {0}; |
| 43 | index_helper.OffsetToNdIndex(ctx->parallel_ctx().parallel_id(), nd_index); |
| 44 | |
| 45 | for (int i = hierarchy.NumAxes() - 1; i >= 0; --i) { |
| 46 | SbpParallel sbp; |
| 47 | CHECK_OR_RETURN(ParseSbpParallelFromString(nd_sbp_str_vec[i], &sbp)); |
| 48 | if (sbp.has_split_parallel()) { |
| 49 | rank += nd_index[i] * world_size; |
| 50 | world_size *= hierarchy.At(i); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | return Maybe<void>::Ok(); |
| 55 | } |
| 56 | |
| 57 | } // namespace data |
| 58 |
no test coverage detected