MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / main

Function main

tests/test-rope.cpp:126–220  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

124}
125
126int main(int /*argc*/, const char ** /*argv*/) {
127 struct ggml_init_params params = {
128 /* .mem_size = */ 128*1024*1024,
129 /* .mem_buffer = */ NULL,
130 /* .no_alloc = */ false,
131 };
132
133 std::vector<uint8_t> work_buffer;
134
135 struct ggml_context * ctx0 = ggml_init(params);
136
137 struct ggml_tensor * x;
138
139 // rope f32
140 for (int m = 0; m < 3; ++m) {
141 const int ndims = 4;
142
143 const int64_t n_rot = 128;
144 const int64_t ne[4] = { 2*n_rot, 32, 73, 1 };
145
146 const int n_past_0 = 100;
147 const int n_past_2 = 33;
148
149 struct ggml_tensor * p0 = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ne[2]);
150 struct ggml_tensor * p1 = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ne[2]);
151 struct ggml_tensor * p2 = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ne[2]);
152
153 for (int i = 0; i < ne[2]; ++i) {
154 ((int32_t *) p0->data)[i] = n_past_0 + i;
155 ((int32_t *) p1->data)[i] = n_past_2 - n_past_0;
156 ((int32_t *) p2->data)[i] = n_past_2 + i;
157 }
158
159 // test mode 0, 2, 4 (standard, GPT-NeoX, GLM)
160 const int mode = m == 0 ? 0 : m == 1 ? 2 : 4;
161
162 x = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
163
164 // 100, 101, 102, ..., 172
165 struct ggml_tensor * r0 = ggml_rope(ctx0, x, p0, n_rot, mode, 1024);
166 // -67, -67, -67, ..., -67
167 struct ggml_tensor * r1 = ggml_rope(ctx0, r0, p1, n_rot, mode, 1024); // "context swap", i.e. forget n_past_0 - n_past_2 tokens
168
169 // 33, 34, 35, ..., 105
170 struct ggml_tensor * r2 = ggml_rope(ctx0, x, p2, n_rot, mode, 1024);
171
172 ggml_cgraph * gf = ggml_new_graph(ctx0);
173
174 ggml_build_forward_expand(gf, r0);
175 ggml_build_forward_expand(gf, r1);
176 ggml_build_forward_expand(gf, r2);
177
178 ggml_graph_compute_helper(work_buffer, gf, 4);
179
180 // check that r1 and r2 are the same
181 {
182 double sum0 = 0.0f;
183 double sum1 = 0.0f;

Callers

nothing calls this directly

Calls 10

printfFunction · 0.85
get_random_tensor_f32Function · 0.70
ggml_initFunction · 0.50
ggml_new_tensor_1dFunction · 0.50
ggml_ropeFunction · 0.50
ggml_new_graphFunction · 0.50
ggml_nelementsFunction · 0.50
ggml_freeFunction · 0.50

Tested by

no test coverage detected