|
| 1 | +# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +import unittest |
| 17 | + |
| 18 | +import paddle.distributed as dist |
| 19 | + |
| 20 | + |
| 21 | +class TestPlacementTypes(unittest.TestCase): |
| 22 | + def test_shard_eq_with_co_shard_order_zero(self): |
| 23 | + """ |
| 24 | + Tests that a Shard is equal to a CoShard with shard_order=0. |
| 25 | + This confirms the "semantic equality" philosophy. |
| 26 | + """ |
| 27 | + s1 = dist.Shard(0) |
| 28 | + s2 = dist.Shard(dim=0, shard_order=0) |
| 29 | + |
| 30 | + # 1. Test for symmetric equality |
| 31 | + self.assertEqual( |
| 32 | + s1, s2, "Shard(0) should be equal to Shard(dim=0, shard_order=0)" |
| 33 | + ) |
| 34 | + self.assertEqual(s2, s1, "Equality should be symmetric") |
| 35 | + |
| 36 | + # 2. Test hash consistency |
| 37 | + self.assertEqual( |
| 38 | + hash(s1), hash(s2), "Hashes must be equal for equal objects" |
| 39 | + ) |
| 40 | + |
| 41 | + # 3. Test behavior in a set |
| 42 | + placement_set = {s1, s2} |
| 43 | + self.assertEqual( |
| 44 | + len(placement_set), |
| 45 | + 1, |
| 46 | + "A set should only contain one of the two equal objects", |
| 47 | + ) |
| 48 | + |
| 49 | + # 4. Test behavior in a dict |
| 50 | + placement_dict = {s1: "value1"} |
| 51 | + self.assertIn( |
| 52 | + s2, placement_dict, "s2 should be found in a dict keyed by s1" |
| 53 | + ) |
| 54 | + self.assertEqual(placement_dict[s2], "value1") |
| 55 | + |
| 56 | + def test_shard_neq_with_co_shard_order_non_zero(self): |
| 57 | + """ |
| 58 | + Tests that a Shard is NOT equal to a CoShard with a non-zero shard_order. |
| 59 | + """ |
| 60 | + s1 = dist.Shard(0) |
| 61 | + s2 = dist.Shard(dim=0, shard_order=1) |
| 62 | + |
| 63 | + # 1. Test for symmetric inequality |
| 64 | + self.assertNotEqual( |
| 65 | + s1, |
| 66 | + s2, |
| 67 | + "Shard(0) should NOT be equal to Shard(dim=0, shard_order=1)", |
| 68 | + ) |
| 69 | + self.assertNotEqual(s2, s1, "Inequality should be symmetric") |
| 70 | + |
| 71 | + # 2. Test hash difference |
| 72 | + # Note: While not a strict requirement for non-equal objects to have different hashes, |
| 73 | + # a good hash function should minimize collisions. We test for non-collision here. |
| 74 | + self.assertNotEqual( |
| 75 | + hash(s1), hash(s2), "Hashes should be different for unequal objects" |
| 76 | + ) |
| 77 | + |
| 78 | + # 3. Test behavior in a set |
| 79 | + placement_set = {s1, s2} |
| 80 | + self.assertEqual( |
| 81 | + len(placement_set), 2, "A set should contain two distinct objects" |
| 82 | + ) |
| 83 | + |
| 84 | + def test_co_shard_eq(self): |
| 85 | + """ |
| 86 | + Tests equality for two CoShard objects. |
| 87 | + """ |
| 88 | + s1 = dist.Shard(dim=0, shard_order=1) |
| 89 | + s2 = dist.Shard(dim=0, shard_order=1) |
| 90 | + s3 = dist.Shard(dim=0, shard_order=2) |
| 91 | + |
| 92 | + self.assertEqual(s1, s2) |
| 93 | + self.assertNotEqual(s1, s3) |
| 94 | + |
| 95 | + def test_replicate_placement(self): |
| 96 | + """ |
| 97 | + Tests equality and hash for Replicate placement. |
| 98 | + """ |
| 99 | + r1 = dist.Replicate() |
| 100 | + r2 = dist.Replicate() |
| 101 | + s1 = dist.Shard(0) |
| 102 | + |
| 103 | + # 1. Test equality |
| 104 | + self.assertEqual(r1, r2, "Two Replicate objects should be equal") |
| 105 | + self.assertNotEqual(r1, s1, "Replicate should not be equal to Shard") |
| 106 | + |
| 107 | + # 2. Test hash consistency |
| 108 | + self.assertEqual( |
| 109 | + hash(r1), |
| 110 | + hash(r2), |
| 111 | + "Hashes of two Replicate objects should be equal", |
| 112 | + ) |
| 113 | + |
| 114 | + # 3. Test behavior in a set |
| 115 | + placement_set: set[dist.Placement] = {r1, r2} |
| 116 | + self.assertEqual( |
| 117 | + len(placement_set), |
| 118 | + 1, |
| 119 | + "A set should only contain one Replicate object", |
| 120 | + ) |
| 121 | + placement_set.add(s1) |
| 122 | + self.assertEqual( |
| 123 | + len(placement_set), |
| 124 | + 2, |
| 125 | + "The set should now contain two distinct objects", |
| 126 | + ) |
| 127 | + |
| 128 | + def test_partial_placement(self): |
| 129 | + """ |
| 130 | + Tests equality and hash for Partial placement. |
| 131 | + """ |
| 132 | + p_sum1 = dist.Partial(dist.ReduceType.kRedSum) |
| 133 | + p_sum2 = dist.Partial(dist.ReduceType.kRedSum) |
| 134 | + p_avg = dist.Partial(dist.ReduceType.kRedAvg) |
| 135 | + r1 = dist.Replicate() |
| 136 | + |
| 137 | + # 1. Test equality |
| 138 | + self.assertEqual( |
| 139 | + p_sum1, p_sum2, "Two Partial(kRedSum) objects should be equal" |
| 140 | + ) |
| 141 | + self.assertNotEqual( |
| 142 | + p_sum1, |
| 143 | + p_avg, |
| 144 | + "Partial(kRedSum) should not be equal to Partial(kRedAvg)", |
| 145 | + ) |
| 146 | + self.assertNotEqual( |
| 147 | + p_sum1, r1, "Partial should not be equal to Replicate" |
| 148 | + ) |
| 149 | + |
| 150 | + # 2. Test hash consistency |
| 151 | + self.assertEqual(hash(p_sum1), hash(p_sum2)) |
| 152 | + self.assertNotEqual(hash(p_sum1), hash(p_avg)) |
| 153 | + |
| 154 | + # 3. Test behavior in a set |
| 155 | + placement_set = {p_sum1, p_sum2} |
| 156 | + self.assertEqual(len(placement_set), 1) |
| 157 | + placement_set.add(p_avg) |
| 158 | + self.assertEqual(len(placement_set), 2) |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == '__main__': |
| 162 | + unittest.main() |
0 commit comments