|
| 1 | +#include "multi_parallel_linear.h" |
| 2 | + |
| 3 | +#include <c10/core/Device.h> |
| 4 | +#include <c10/core/ScalarType.h> |
| 5 | +#include <glog/logging.h> |
| 6 | +#include <gtest/gtest.h> |
| 7 | +#include <torch/torch.h> |
| 8 | + |
| 9 | +#include <cstddef> |
| 10 | +#include <torch/csrc/distributed/c10d/FileStore.hpp> |
| 11 | +#include <torch/csrc/distributed/c10d/HashStore.hpp> |
| 12 | +#include <torch/csrc/distributed/c10d/ProcessGroupNCCL.hpp> |
| 13 | + |
| 14 | +#include "model_loader/state_dict.h" |
| 15 | + |
| 16 | +namespace llm { |
| 17 | + |
| 18 | +TEST(MultiParallelLinearTest, FusedColumnParallelLinear) { |
| 19 | + // test load state dict for linear |
| 20 | + const int64_t in_features = 10; |
| 21 | + const int64_t out_features = 40; |
| 22 | + |
| 23 | + torch::Device device(torch::kCPU); |
| 24 | + torch::ScalarType dtype(torch::kFloat); |
| 25 | + const auto options = torch::dtype(dtype).device(device); |
| 26 | + |
| 27 | + std::vector<int64_t> out_features_vec = { |
| 28 | + out_features, out_features, out_features}; |
| 29 | + std::vector<std::string> prefixes = {"query.", "key.", "value."}; |
| 30 | + |
| 31 | + std::unordered_map<std::string, torch::Tensor> state_dict_data; |
| 32 | + // Allocate transposed weight matrix |
| 33 | + state_dict_data["query.weight"] = torch::randn({out_features, in_features}); |
| 34 | + state_dict_data["key.weight"] = torch::randn({out_features, in_features}); |
| 35 | + state_dict_data["value.weight"] = torch::randn({out_features, in_features}); |
| 36 | + |
| 37 | + // weight is not sharded |
| 38 | + StateDict state_dict(state_dict_data); |
| 39 | + |
| 40 | + // test load weight |
| 41 | + { |
| 42 | + ParallelArgs parallel_args(0, 1, nullptr); |
| 43 | + FusedColumnParallelLinearImpl linear(in_features, |
| 44 | + out_features_vec, |
| 45 | + prefixes, |
| 46 | + /*bias=*/false, |
| 47 | + /*gather_output=*/false, |
| 48 | + parallel_args, |
| 49 | + options); |
| 50 | + // test load fused weight |
| 51 | + EXPECT_EQ(linear.load(state_dict), 3); |
| 52 | + |
| 53 | + for (const auto& prefix : prefixes) { |
| 54 | + auto named_parameters = linear.named_parameters(/*recurse=*/false); |
| 55 | + const auto key = detail::join_name(prefix, "weight"); |
| 56 | + ASSERT_TRUE(named_parameters.contains(key)); |
| 57 | + |
| 58 | + const auto& loaded_weight = named_parameters[key]; |
| 59 | + EXPECT_EQ(loaded_weight.sizes(), |
| 60 | + torch::IntArrayRef({out_features, in_features})); |
| 61 | + EXPECT_TRUE(torch::equal(loaded_weight, state_dict_data[key])); |
| 62 | + } |
| 63 | + |
| 64 | + // verify the fused weight |
| 65 | + const auto loaded_fused_weight = linear.weight(); |
| 66 | + const auto desired_fused_weight = |
| 67 | + torch::cat({state_dict_data["query.weight"], |
| 68 | + state_dict_data["key.weight"], |
| 69 | + state_dict_data["value.weight"]}, |
| 70 | + /*dim=*/0); |
| 71 | + EXPECT_TRUE(torch::equal(loaded_fused_weight, desired_fused_weight)); |
| 72 | + } |
| 73 | + |
| 74 | + // test load weight with 4 shards |
| 75 | + const int32_t num_shards = 4; |
| 76 | + for (int32_t shard_id = 0; shard_id < num_shards; ++shard_id) { |
| 77 | + ParallelArgs parallel_args_0(shard_id, num_shards, nullptr); |
| 78 | + FusedColumnParallelLinearImpl linear(in_features, |
| 79 | + out_features_vec, |
| 80 | + prefixes, |
| 81 | + /*bias=*/false, |
| 82 | + /*gather_output=*/false, |
| 83 | + parallel_args_0, |
| 84 | + options); |
| 85 | + EXPECT_EQ(linear.load(state_dict), 3); |
| 86 | + |
| 87 | + auto named_parameters = linear.named_parameters(/*recurse=*/false); |
| 88 | + |
| 89 | + // check size for each prefix |
| 90 | + for (const auto& prefix : prefixes) { |
| 91 | + const auto key = detail::join_name(prefix, "weight"); |
| 92 | + ASSERT_TRUE(named_parameters.contains(key)); |
| 93 | + |
| 94 | + const auto& loaded_weight = named_parameters[key]; |
| 95 | + EXPECT_EQ(loaded_weight.sizes(), |
| 96 | + torch::IntArrayRef({out_features / num_shards, in_features})); |
| 97 | + EXPECT_TRUE(torch::equal( |
| 98 | + loaded_weight, state_dict_data[key].chunk(num_shards, 0)[shard_id])); |
| 99 | + } |
| 100 | + |
| 101 | + // shard weight then cat |
| 102 | + auto sharded_query_weight = |
| 103 | + state_dict_data["query.weight"].chunk(num_shards, 0)[shard_id]; |
| 104 | + auto sharded_key_weight = |
| 105 | + state_dict_data["key.weight"].chunk(num_shards, 0)[shard_id]; |
| 106 | + auto sharded_value_weight = |
| 107 | + state_dict_data["value.weight"].chunk(num_shards, 0)[shard_id]; |
| 108 | + |
| 109 | + // verify the fused weight |
| 110 | + const auto loaded_fused_weight = linear.weight(); |
| 111 | + auto desired_fused_weight = torch::cat( |
| 112 | + {sharded_query_weight, sharded_key_weight, sharded_value_weight}, |
| 113 | + /*dim=*/0); |
| 114 | + |
| 115 | + EXPECT_TRUE(torch::equal(loaded_fused_weight, desired_fused_weight)); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +TEST(MultiParallelLinearTest, GroupedColumnParallelLinear) { |
| 120 | + const int64_t in_features = 10; |
| 121 | + const int64_t out_features = 40; |
| 122 | + std::vector<int64_t> out_features_vec = { |
| 123 | + out_features, out_features, out_features}; |
| 124 | + std::vector<std::string> prefixes = {"query.", "key.", "value."}; |
| 125 | + |
| 126 | + torch::Device device(torch::kCPU); |
| 127 | + torch::ScalarType dtype(torch::kFloat); |
| 128 | + const auto options = torch::dtype(dtype).device(device); |
| 129 | + |
| 130 | + std::unordered_map<std::string, torch::Tensor> state_dict_data; |
| 131 | + // Allocate transposed weight matrix |
| 132 | + state_dict_data["query.weight"] = torch::randn({out_features, in_features}); |
| 133 | + state_dict_data["key.weight"] = torch::randn({out_features, in_features}); |
| 134 | + state_dict_data["value.weight"] = torch::randn({out_features, in_features}); |
| 135 | + // weight is not sharded |
| 136 | + StateDict state_dict(state_dict_data); |
| 137 | + |
| 138 | + // test load weight |
| 139 | + { |
| 140 | + ParallelArgs parallel_args(0, 1, nullptr); |
| 141 | + GroupedColumnParallelLinearImpl linear(in_features, |
| 142 | + out_features_vec, |
| 143 | + prefixes, |
| 144 | + /*bias=*/false, |
| 145 | + /*gather_output=*/false, |
| 146 | + parallel_args, |
| 147 | + options); |
| 148 | + // test load grouped weight |
| 149 | + EXPECT_EQ(linear.load(state_dict), 3); |
| 150 | + |
| 151 | + auto named_parameters = linear.named_parameters(/*recurse=*/true); |
| 152 | + for (size_t i = 0; i < prefixes.size(); ++i) { |
| 153 | + const auto prefix = "linear_" + std::to_string(i) + "." + prefixes[i]; |
| 154 | + const auto key = detail::join_name(prefix, "weight"); |
| 155 | + ASSERT_TRUE(named_parameters.contains(key)); |
| 156 | + |
| 157 | + const auto& loaded_weight = named_parameters[key]; |
| 158 | + |
| 159 | + const auto sd_key = detail::join_name(prefixes[i], "weight"); |
| 160 | + |
| 161 | + EXPECT_EQ(loaded_weight.sizes(), |
| 162 | + torch::IntArrayRef({out_features, in_features})); |
| 163 | + EXPECT_TRUE(torch::equal(loaded_weight, state_dict_data[sd_key])); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // test load weight with 4 shards |
| 168 | + const int32_t num_shards = 4; |
| 169 | + for (int32_t shard_id = 0; shard_id < num_shards; ++shard_id) { |
| 170 | + ParallelArgs parallel_args(shard_id, num_shards, nullptr); |
| 171 | + GroupedColumnParallelLinearImpl linear(in_features, |
| 172 | + out_features_vec, |
| 173 | + prefixes, |
| 174 | + /*bias=*/false, |
| 175 | + /*gather_output=*/false, |
| 176 | + parallel_args, |
| 177 | + options); |
| 178 | + EXPECT_EQ(linear.load(state_dict), 3); |
| 179 | + auto named_parameters = linear.named_parameters(/*recurse=*/true); |
| 180 | + // check size for each prefix |
| 181 | + for (size_t i = 0; i < prefixes.size(); ++i) { |
| 182 | + const auto prefix = "linear_" + std::to_string(i) + "." + prefixes[i]; |
| 183 | + const auto key = detail::join_name(prefix, "weight"); |
| 184 | + ASSERT_TRUE(named_parameters.contains(key)); |
| 185 | + |
| 186 | + const auto& loaded_weight = named_parameters[key]; |
| 187 | + EXPECT_EQ(loaded_weight.sizes(), |
| 188 | + torch::IntArrayRef({out_features / num_shards, in_features})); |
| 189 | + const auto sd_key = detail::join_name(prefixes[i], "weight"); |
| 190 | + EXPECT_TRUE( |
| 191 | + torch::equal(loaded_weight, |
| 192 | + state_dict_data[sd_key].chunk(num_shards, 0)[shard_id])); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +} // namespace llm |
0 commit comments