@@ -163,6 +163,80 @@ TEST(LambdaRank, MakePair) {
163163 }
164164}
165165
166+ void TestMAPStat (Context const * ctx) {
167+ auto p_fmat = EmptyDMatrix ();
168+ MetaInfo& info = p_fmat->Info ();
169+ ltr::LambdaRankParam param;
170+ param.UpdateAllowUnknown (Args{});
171+
172+ {
173+ std::vector<float > h_data{1 .0f , 1 .0f , 0 .0f , 1 .0f , 1 .0f , 1 .0f };
174+ info.labels .Reshape (h_data.size (), 1 );
175+ info.labels .Data ()->HostVector () = h_data;
176+ info.num_row_ = h_data.size ();
177+
178+ HostDeviceVector<float > predt;
179+ auto & h_predt = predt.HostVector ();
180+ h_predt.resize (h_data.size ());
181+ std::iota (h_predt.rbegin (), h_predt.rend (), 0 .0f );
182+
183+ auto p_cache = std::make_shared<ltr::MAPCache>(ctx, info, param);
184+
185+ predt.SetDevice (ctx->gpu_id );
186+ auto rank_idx =
187+ p_cache->SortedIdx (ctx, ctx->IsCPU () ? predt.ConstHostSpan () : predt.ConstDeviceSpan ());
188+
189+ if (ctx->IsCPU ()) {
190+ obj::cpu_impl::MAPStat (ctx, info.labels .HostView ().Slice (linalg::All (), 0 ), rank_idx,
191+ p_cache);
192+ } else {
193+ obj::cuda_impl::MAPStat (ctx, info, rank_idx, p_cache);
194+ }
195+
196+ Context cpu_ctx;
197+ auto n_rel = p_cache->NumRelevant (&cpu_ctx);
198+ auto acc = p_cache->Acc (&cpu_ctx);
199+
200+ ASSERT_EQ (n_rel[0 ], 1.0 );
201+ ASSERT_EQ (acc[0 ], 1.0 );
202+
203+ ASSERT_EQ (n_rel.back (), h_data.size () - 1.0 );
204+ ASSERT_NEAR (acc.back (), 1.95 + (1.0 / h_data.size ()), kRtEps );
205+ }
206+ {
207+ info.labels .Reshape (16 );
208+ auto & h_label = info.labels .Data ()->HostVector ();
209+ info.group_ptr_ = {0 , 8 , 16 };
210+ info.num_row_ = info.labels .Shape (0 );
211+
212+ std::fill_n (h_label.begin (), 8 , 1 .0f );
213+ std::fill_n (h_label.begin () + 8 , 8 , 0 .0f );
214+ HostDeviceVector<float > predt;
215+ auto & h_predt = predt.HostVector ();
216+ h_predt.resize (h_label.size ());
217+ std::iota (h_predt.rbegin (), h_predt.rbegin () + 8 , 0 .0f );
218+ std::iota (h_predt.rbegin () + 8 , h_predt.rend (), 0 .0f );
219+
220+ auto p_cache = std::make_shared<ltr::MAPCache>(ctx, info, param);
221+
222+ predt.SetDevice (ctx->gpu_id );
223+ auto rank_idx =
224+ p_cache->SortedIdx (ctx, ctx->IsCPU () ? predt.ConstHostSpan () : predt.ConstDeviceSpan ());
225+
226+ if (ctx->IsCPU ()) {
227+ obj::cpu_impl::MAPStat (ctx, info.labels .HostView ().Slice (linalg::All (), 0 ), rank_idx,
228+ p_cache);
229+ } else {
230+ obj::cuda_impl::MAPStat (ctx, info, rank_idx, p_cache);
231+ }
232+
233+ Context cpu_ctx;
234+ auto n_rel = p_cache->NumRelevant (&cpu_ctx);
235+ ASSERT_EQ (n_rel[7 ], 8 ); // first group
236+ ASSERT_EQ (n_rel.back (), 0 ); // second group
237+ }
238+ }
239+
166240TEST (LambdaRank, MAPStat) {
167241 Context ctx;
168242 TestMAPStat (&ctx);
0 commit comments