Skip to content

Commit 3c30821

Browse files
committed
removed parallelization of group-wise metrics
1 parent 34f1103 commit 3c30821

1 file changed

Lines changed: 14 additions & 74 deletions

File tree

include/LightGBM/objective_function.h

Lines changed: 14 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -623,25 +623,19 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
623623
*/
624624
void ComputeFPR(const double *score, double probabilities_threshold, std::unordered_map<constraint_group_t, double> &group_fpr) const
625625
{
626-
std::mutex fp_mutex, ln_mutex;
627626
std::unordered_map<int, int> false_positives;
628627
std::unordered_map<int, int> label_negatives;
629628

630-
#pragma omp parallel for schedule(static)
631629
for (data_size_t i = 0; i < num_data_; ++i)
632630
{
633631
constraint_group_t group = group_[i];
634632

635633
if (label_[i] == 0)
636634
{
637-
{
638-
std::lock_guard<std::mutex> lock(ln_mutex);
639-
label_negatives[group] += 1;
640-
}
635+
label_negatives[group] += 1;
641636

642637
const double z = 1.0f / (1.0f + std::exp(-score[i]));
643638
if (z >= probabilities_threshold) {
644-
std::lock_guard<std::mutex> lock(fp_mutex);
645639
false_positives[group] += 1;
646640
}
647641
}
@@ -693,29 +687,21 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
693687
*/
694688
void ComputeHingeFPR(const double *score, std::unordered_map<constraint_group_t, double> &group_fpr) const
695689
{
696-
std::mutex fp_mutex, ln_mutex;
697690
std::unordered_map<constraint_group_t, double> false_positives; // map of group index to the respective hinge-proxy FPs
698691
std::unordered_map<constraint_group_t, int> label_negatives; // map of group index to the respective number of LNs
699692

700-
#pragma omp parallel for schedule(static)
701693
for (data_size_t i = 0; i < num_data_; ++i)
702694
{
703695
constraint_group_t group = group_[i];
704696

705697
// HingeFPR uses only label negatives
706698
if (label_[i] == 0)
707699
{
708-
{
709-
std::lock_guard<std::mutex> lock(ln_mutex);
710-
label_negatives[group] += 1;
711-
}
700+
label_negatives[group] += 1;
712701

713702
// proxy_margin_ is the line intercept value
714703
const double hinge_score = proxy_margin_ + score[i];
715-
{
716-
std::lock_guard<std::mutex> lock(fp_mutex);
717-
false_positives[group] += std::max(0.0, hinge_score);
718-
}
704+
false_positives[group] += std::max(0.0, hinge_score);
719705
}
720706
}
721707

@@ -741,30 +727,22 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
741727
*/
742728
void ComputeQuadraticLossFPR(const double *score, std::unordered_map<constraint_group_t, double> &group_fpr) const
743729
{
744-
std::mutex fp_mutex, ln_mutex;
745730
std::unordered_map<constraint_group_t, double> false_positives; // map of group index to the respective proxy FPs
746731
std::unordered_map<constraint_group_t, int> label_negatives; // map of group index to the respective number of LNs
747732

748-
#pragma omp parallel for schedule(static)
749733
for (data_size_t i = 0; i < num_data_; ++i)
750734
{
751735
constraint_group_t group = group_[i];
752736

753737
// FPR uses only label NEGATIVES
754738
if (label_[i] == 0 and score[i] > -proxy_margin_)
755739
{ // Conditions for non-zero proxy-FPR value
756-
{
757-
std::lock_guard<std::mutex> lock(ln_mutex);
758-
label_negatives[group] += 1;
759-
}
740+
label_negatives[group] += 1;
760741

761742
// proxy_margin_ corresponds to the symmetric of the function's zero point; f(-proxy_margin_)=0
762743
const double quadratic_score = (1. / 2.) * std::pow(score[i] + proxy_margin_, 2);
763744
assert(quadratic_score >= 0.);
764-
{
765-
std::lock_guard<std::mutex> lock(fp_mutex);
766-
false_positives[group] += quadratic_score;
767-
}
745+
false_positives[group] += quadratic_score;
768746
}
769747
}
770748

@@ -790,31 +768,23 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
790768
*/
791769
void ComputeXEntropyLossFPR(const double *score, std::unordered_map<constraint_group_t, double> &group_fpr) const
792770
{
793-
std::mutex fp_mutex, ln_mutex;
794771
std::unordered_map<constraint_group_t, double> false_positives; // map of group index to the respective proxy FPs
795772
std::unordered_map<constraint_group_t, int> label_negatives; // map of group index to the respective number of LNs
796773
const double xent_horizontal_shift = log(exp(proxy_margin_) - 1);
797774

798-
#pragma omp parallel for schedule(static)
799775
for (data_size_t i = 0; i < num_data_; ++i)
800776
{
801777
constraint_group_t group = group_[i];
802778

803779
// FPR uses only label NEGATIVES
804780
if (label_[i] == 0)
805781
{
806-
{
807-
std::lock_guard<std::mutex> lock(ln_mutex);
808-
label_negatives[group] += 1;
809-
}
782+
label_negatives[group] += 1;
810783

811784
// proxy_margin_ corresponds to the vertical margin at x=0; l(0) = proxy_margin_
812785
const double xent_score = log(1 + exp(score[i] + xent_horizontal_shift));
813786
assert(xent_score >= 0.);
814-
{
815-
std::lock_guard<std::mutex> lock(fp_mutex);
816-
false_positives[group] += xent_score;
817-
}
787+
false_positives[group] += xent_score;
818788
}
819789
}
820790

@@ -838,25 +808,19 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
838808
*/
839809
void ComputeFNR(const double *score, double probabilities_threshold, std::unordered_map<constraint_group_t, double> &group_fnr) const
840810
{
841-
std::mutex fn_mutex, lp_mutex;
842811
std::unordered_map<constraint_group_t, int> false_negatives;
843812
std::unordered_map<constraint_group_t, int> label_positives;
844813

845-
#pragma omp parallel for schedule(static)
846814
for (data_size_t i = 0; i < num_data_; ++i)
847815
{
848816
constraint_group_t group = group_[i];
849817

850818
if (label_[i] == 1)
851819
{
852-
{
853-
std::lock_guard<std::mutex> lock(lp_mutex);
854-
label_positives[group] += 1;
855-
}
820+
label_positives[group] += 1;
856821

857822
const double z = 1.0f / (1.0f + std::exp(-score[i]));
858823
if (z < probabilities_threshold) {
859-
std::lock_guard<std::mutex> lock(fn_mutex);
860824
false_negatives[group] += 1;
861825
}
862826
}
@@ -907,27 +871,19 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
907871
*/
908872
void ComputeHingeLossFNR(const double *score, std::unordered_map<constraint_group_t, double> &group_fnr) const
909873
{
910-
std::mutex fn_mutex, lp_mutex;
911874
std::unordered_map<constraint_group_t, double> false_negatives; // map of group index to the respective hinge-proxy FNs
912875
std::unordered_map<constraint_group_t, int> label_positives;
913876

914-
#pragma omp parallel for schedule(static)
915877
for (data_size_t i = 0; i < num_data_; ++i)
916878
{
917879
constraint_group_t group = group_[i];
918880

919881
if (label_[i] == 1)
920882
{
921-
{
922-
std::lock_guard<std::mutex> lock(lp_mutex);
923-
label_positives[group] += 1;
924-
}
883+
label_positives[group] += 1;
925884

926885
const double hinge_score = proxy_margin_ - score[i];
927-
{
928-
std::lock_guard<std::mutex> lock(fn_mutex);
929-
false_negatives[group] += std::max(0.0, hinge_score);
930-
}
886+
false_negatives[group] += std::max(0.0, hinge_score);
931887
}
932888
}
933889

@@ -952,30 +908,22 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
952908
*/
953909
void ComputeQuadraticLossFNR(const double *score, std::unordered_map<constraint_group_t, double> &group_fnr) const
954910
{
955-
std::mutex fn_mutex, lp_mutex;
956911
std::unordered_map<constraint_group_t, double> false_negatives; // map of group index to the respective proxy FPs
957912
std::unordered_map<constraint_group_t, int> label_positives; // map of group index to the respective number of LNs
958913

959-
#pragma omp parallel for schedule(static)
960914
for (data_size_t i = 0; i < num_data_; ++i)
961915
{
962916
constraint_group_t group = group_[i];
963917

964918
// FNR uses only label POSITIVES
965919
if (label_[i] == 1 and score[i] < proxy_margin_)
966920
{ // Conditions for non-zero proxy-FNR value
967-
{
968-
std::lock_guard<std::mutex> lock(lp_mutex);
969-
label_positives[group] += 1;
970-
}
921+
label_positives[group] += 1;
971922

972923
// proxy_margin_ corresponds to the function's zero point; f(proxy_margin_)=0
973924
const double quadratic_score = (1. / 2.) * std::pow(score[i] - proxy_margin_, 2);
974925
assert(quadratic_score >= 0.);
975-
{
976-
std::lock_guard<std::mutex> lock(fn_mutex);
977-
false_negatives[group] += quadratic_score;
978-
}
926+
false_negatives[group] += quadratic_score;
979927
}
980928
}
981929

@@ -1001,31 +949,23 @@ class ConstrainedObjectiveFunction : public ObjectiveFunction
1001949
*/
1002950
void ComputeXEntropyLossFNR(const double *score, std::unordered_map<constraint_group_t, double> &group_fnr) const
1003951
{
1004-
std::mutex fn_mutex, lp_mutex;
1005952
std::unordered_map<constraint_group_t, double> false_negatives; // map of group index to the respective proxy FPs
1006953
std::unordered_map<constraint_group_t, int> label_positives; // map of group index to the respective number of LNs
1007954
const double xent_horizontal_shift = log(exp(proxy_margin_) - 1);
1008955

1009-
#pragma omp parallel for schedule(static)
1010956
for (data_size_t i = 0; i < num_data_; ++i)
1011957
{
1012958
constraint_group_t group = group_[i];
1013959

1014960
// FNR uses only label POSITIVES
1015961
if (label_[i] == 1)
1016962
{
1017-
{
1018-
std::lock_guard<std::mutex> lock(lp_mutex);
1019-
label_positives[group] += 1;
1020-
}
963+
label_positives[group] += 1;
1021964

1022965
// proxy_margin_ corresponds to the vertical margin at x=0; l(0) = proxy_margin_
1023966
const double xent_score = log(1 + exp(xent_horizontal_shift - score[i]));
1024967
assert(xent_score >= 0.);
1025-
{
1026-
std::lock_guard<std::mutex> lock(fn_mutex);
1027-
false_negatives[group] += xent_score;
1028-
}
968+
false_negatives[group] += xent_score;
1029969
}
1030970
}
1031971

0 commit comments

Comments
 (0)