TensorTonic
Study Plans
Study Plan
Simulate Distributed Collectives
Distributed Computing
Medium

Simulate four distributed collectives locally over a rank-ordered list of one-dimensional PyTorch tensors. A rank is one participating worker. Let RRR be the number of ranks, LLL the input length at each rank, xrx_rxr​ rank rrr's input, and yry_ryr​ its output; sss indexes source ranks.

For all_gather, every rank receives the rank-ordered concatenation:

yr=concat⁡(x0,…,xR−1).y_r=\operatorname{concat}(x_0,\ldots,x_{R-1}).yr​=concat(x0​,…,xR−1​).

For all_reduce, every rank receives the elementwise sum:

yr=∑s=0R−1xs.y_r=\sum_{s=0}^{R-1}x_s.yr​=s=0∑R−1​xs​.

For reduce_scatter, rank r receives chunk r of the sum, using R equal contiguous chunks. For all_to_all, rank r receives chunk r from each source, concatenated in source-rank order. Return a Python list of R PyTorch tensors in rank order, preserving input dtype and device. Each output has length R times L for all_gather, L divided by R for reduce_scatter, and L otherwise.

Loading visualization...

Examples

Input: rank_tensors = [[1,2],[3,4]], collective = "all_gather"

Output: [[1,2,3,4],[1,2,3,4]]

Explanation: Rank-order concatenation produces one gathered tensor, and every rank receives a separate copy.

Input: rank_tensors = [[1,2,3,4],[10,20,30,40]], collective = "reduce_scatter"

Output: [[11,22],[33,44]]

Input: rank_tensors = [[1,2,3,4],[5,6,7,8]], collective = "all_to_all"

Output: [[1,2,5,6],[3,4,7,8]]

Hint 1

torch.cat joins tensors along an existing axis.

Hint 2

torch.chunk divides a tensor into ordered chunks.

Hint 3

Tensor.clone creates independent tensor storage.

Requirements

  • Use PyTorch to simulate the four supported operations without distributed communication.
  • Preserve rank order and do not modify any input tensor.
  • Returned tensors must not share memory with inputs.

Constraints

  • rank_tensors is a nonempty Python list of one-dimensional PyTorch tensors with equal length, dtype, and device.
  • There are 1 through 16 ranks; tensors contain finite float32 or bfloat16 values and may be noncontiguous.
  • collective is all_gather, all_reduce, reduce_scatter, or all_to_all.
  • For reduce_scatter and all_to_all, input length is divisible by the rank count.
Try Similar Problems
Synchronize Data-Parallel GradientsMediumReconstruct a Column-Parallel MLP LayerMediumCalculate Collective Effective BandwidthEasyMeasure Pipeline Bubble UtilizationMediumAccount for ZeRO Memory by StageEasyChoose a Topology-Aware Parallel PlanHardTrain a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumCalculate Tensor Parallel Communication CostHardSimulate Disaggregated Prefill and Decode ServingHardEstimate Model Memory and Minimum GPU CountEasyImplement Expert Parallel Routing Across GPUsHard

Accepts: array

Accepts: string

You must run your code first.

Simulate Distributed Collectives

Distributed Computing
Medium

Simulate four distributed collectives locally over a rank-ordered list of one-dimensional PyTorch tensors. A rank is one participating worker. Let RRR be the number of ranks, LLL the input length at each rank, xrx_rxr​ rank rrr's input, and yry_ryr​ its output; sss indexes source ranks.

For all_gather, every rank receives the rank-ordered concatenation:

yr=concat⁡(x0,…,xR−1).y_r=\operatorname{concat}(x_0,\ldots,x_{R-1}).yr​=concat(x0​,…,xR−1​).

For all_reduce, every rank receives the elementwise sum:

yr=∑s=0R−1xs.y_r=\sum_{s=0}^{R-1}x_s.yr​=s=0∑R−1​xs​.

For reduce_scatter, rank r receives chunk r of the sum, using R equal contiguous chunks. For all_to_all, rank r receives chunk r from each source, concatenated in source-rank order. Return a Python list of R PyTorch tensors in rank order, preserving input dtype and device. Each output has length R times L for all_gather, L divided by R for reduce_scatter, and L otherwise.

Loading visualization...

Examples

Input: rank_tensors = [[1,2],[3,4]], collective = "all_gather"

Output: [[1,2,3,4],[1,2,3,4]]

Explanation: Rank-order concatenation produces one gathered tensor, and every rank receives a separate copy.

Input: rank_tensors = [[1,2,3,4],[10,20,30,40]], collective = "reduce_scatter"

Output: [[11,22],[33,44]]

Input: rank_tensors = [[1,2,3,4],[5,6,7,8]], collective = "all_to_all"

Output: [[1,2,5,6],[3,4,7,8]]

Hint 1

torch.cat joins tensors along an existing axis.

Hint 2

torch.chunk divides a tensor into ordered chunks.

Hint 3

Tensor.clone creates independent tensor storage.

Requirements

  • Use PyTorch to simulate the four supported operations without distributed communication.
  • Preserve rank order and do not modify any input tensor.
  • Returned tensors must not share memory with inputs.

Constraints

  • rank_tensors is a nonempty Python list of one-dimensional PyTorch tensors with equal length, dtype, and device.
  • There are 1 through 16 ranks; tensors contain finite float32 or bfloat16 values and may be noncontiguous.
  • collective is all_gather, all_reduce, reduce_scatter, or all_to_all.
  • For reduce_scatter and all_to_all, input length is divisible by the rank count.
Try Similar Problems
Synchronize Data-Parallel GradientsMediumReconstruct a Column-Parallel MLP LayerMediumCalculate Collective Effective BandwidthEasyMeasure Pipeline Bubble UtilizationMediumAccount for ZeRO Memory by StageEasyChoose a Topology-Aware Parallel PlanHardTrain a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumCalculate Tensor Parallel Communication CostHardSimulate Disaggregated Prefill and Decode ServingHardEstimate Model Memory and Minimum GPU CountEasyImplement Expert Parallel Routing Across GPUsHard

Accepts: array

Accepts: string

You must run your code first.