Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#if !defined(ORT_MINIMAL_BUILD)
#include <numeric>
 
#include "core/framework/kernel_pilot_moe_expert_selection.h"
#include "gtest/gtest.h"
#include "test/util/include/asserts.h"
 
namespace onnxruntime::test {
namespace {
 
void ExpectSelectedExperts(const KernelPilotMoeExpertSelection& usage, const InlinedVector<int>& expected) {
  gsl::span<const int> selected;
  ASSERT_STATUS_OK(usage.GetSelectedExperts(selected));
  EXPECT_EQ((InlinedVector<int>{selected.begin(), selected.end()}), expected);
}
 
TEST(KernelPilotMoeExpertSelectionTest, RejectsUninitializedCollection) {
  KernelPilotMoeExpertSelection usage;
  EXPECT_FALSE(usage.IsInitialized());
  EXPECT_FALSE(usage.Collect({}).IsOK());
  const int sentinel[] = {7};
  gsl::span<const int> selected = sentinel;
  EXPECT_FALSE(usage.GetSelectedExperts(selected).IsOK());
  EXPECT_EQ(selected.data(), sentinel);
  EXPECT_FALSE(usage.BeginInvocation(0).IsOK());
  EXPECT_FALSE(usage.IsInitialized());
}
 
TEST(KernelPilotMoeExpertSelectionTest, UnionsSelectedExpertsAcrossBatches) {
  KernelPilotMoeExpertSelection usage;
  ASSERT_STATUS_OK(usage.BeginInvocation(4));
  EXPECT_TRUE(usage.IsInitialized());
  ExpectSelectedExperts(usage, {});
  const int first[] = {0, 2, 0, 2};
  const int second[] = {2, 3, 3};
  ASSERT_STATUS_OK(usage.Collect(first));
  ASSERT_STATUS_OK(usage.Collect(second));
  ASSERT_STATUS_OK(usage.Collect({}));
  ExpectSelectedExperts(usage, {0, 2, 3});
}
 
TEST(KernelPilotMoeExpertSelectionTest, ResetsSelectionAndReusesStorage) {
  KernelPilotMoeExpertSelection usage;
  ASSERT_STATUS_OK(usage.BeginInvocation(64));
  InlinedVector<int> all(64);
  std::iota(all.begin(), all.end(), 0);
  ASSERT_STATUS_OK(usage.Collect(all));
  gsl::span<const int> selected;
  ASSERT_STATUS_OK(usage.GetSelectedExperts(selected));
  const int* storage = selected.data();
  ExpectSelectedExperts(usage, all);
  for (size_t expert_count : {size_t{64}, size_t{32}, size_t{64}}) {
    ASSERT_STATUS_OK(usage.BeginInvocation(expert_count));
    ExpectSelectedExperts(usage, {});
    const int repeated[] = {1, 1};
    ASSERT_STATUS_OK(usage.Collect(repeated));
    ASSERT_STATUS_OK(usage.GetSelectedExperts(selected));
    EXPECT_EQ(selected.data(), storage);
    ExpectSelectedExperts(usage, {1});
  }
}
 
TEST(KernelPilotMoeExpertSelectionTest, InvalidBatchDoesNotPartiallyChangeSelection) {
  KernelPilotMoeExpertSelection usage;
  ASSERT_STATUS_OK(usage.BeginInvocation(4));
  const int first[] = {2};
  ASSERT_STATUS_OK(usage.Collect(first));
  for (const int invalid : {-1, 4}) {
    const int batch[] = {0, invalid};
    EXPECT_FALSE(usage.Collect(batch).IsOK());
    ExpectSelectedExperts(usage, {2});
  }
  EXPECT_FALSE(usage.BeginInvocation(0).IsOK());
  ExpectSelectedExperts(usage, {2});
  const int valid[] = {0, 1};
  ASSERT_STATUS_OK(usage.Collect(valid));
  ExpectSelectedExperts(usage, {2, 0, 1});
}
 
TEST(KernelPilotMoeExpertSelectionTest, KeepsInstancesIndependent) {
  KernelPilotMoeExpertSelection first, second;
  ASSERT_STATUS_OK(first.BeginInvocation(2));
  ASSERT_STATUS_OK(second.BeginInvocation(4));
  const int first_ids[] = {0, 0};
  const int second_ids[] = {3, 3};
  ASSERT_STATUS_OK(first.Collect(first_ids));
  ASSERT_STATUS_OK(second.Collect(second_ids));
  ExpectSelectedExperts(first, {0});
  ExpectSelectedExperts(second, {3});
  ASSERT_STATUS_OK(first.BeginInvocation(4));
  ASSERT_STATUS_OK(first.Collect(second_ids));
  ExpectSelectedExperts(first, {3});
  ExpectSelectedExperts(second, {3});
}
 
}  // namespace
}  // namespace onnxruntime::test
#endif