From 243e173007c2d95de9f85ce4824d8af415930dea Mon Sep 17 00:00:00 2001 From: brave-builds Date: Thu, 21 Dec 2023 01:58:48 +0000 Subject: [PATCH] Uplift of #21414 (squashed) to beta --- .../brave_news/browser/feed_v2_builder.cc | 53 +++++-------------- components/brave_news/common/features.cc | 3 -- components/brave_news/common/features.h | 5 -- 3 files changed, 14 insertions(+), 47 deletions(-) diff --git a/components/brave_news/browser/feed_v2_builder.cc b/components/brave_news/browser/feed_v2_builder.cc index d5d1ee563ed3..ab997707a2a1 100644 --- a/components/brave_news/browser/feed_v2_builder.cc +++ b/components/brave_news/browser/feed_v2_builder.cc @@ -8,7 +8,6 @@ #include #include -#include #include #include #include @@ -289,28 +288,6 @@ using GetWeighting = base::RepeatingCallback; -// Returns a probability distribution (sum to 1) of the weights. Temperature -// controls how "smooth" the distribution is. High temperature brings the -// distribution closer to a uniform distribution (more randomness). -// Low temperature brings the distribution closer to a delta function (less -// randomness). -void SoftmaxWithTemperature( - std::vector& weights, - double temperature = features::kBraveNewsTemperature.Get()) { - if (temperature == 0) { - return; - } - - double max = *base::ranges::max_element(weights.begin(), weights.end()); - base::ranges::transform(weights.begin(), weights.end(), weights.begin(), - [temperature, max](double weight) { - return std::exp((weight - max) / temperature); - }); - double sum = std::accumulate(weights.begin(), weights.end(), 0.0); - base::ranges::transform(weights.begin(), weights.end(), weights.begin(), - [sum](double weight) { return weight / sum; }); -} - // Sample across subscribed channels (direct and native) and publishers. ContentGroup SampleContentGroup( const std::vector& eligible_content_groups) { @@ -328,9 +305,11 @@ ContentGroup SampleContentGroup( // Picks an article with a probability article_weight/sum(article_weights). mojom::FeedItemMetadataPtr PickRouletteAndRemove( ArticleInfos& articles, - GetWeighting get_weighting = base::BindRepeating( - [](const mojom::FeedItemMetadataPtr& metadata, - const ArticleWeight& weight) { return weight.weighting; }), + GetWeighting get_weighting = + base::BindRepeating([](const mojom::FeedItemMetadataPtr& metadata, + const ArticleWeight& weight) { + return weight.subscribed ? weight.weighting : 0; + }), bool use_softmax = false) { std::vector weights; base::ranges::transform(articles, std::back_inserter(weights), @@ -340,15 +319,12 @@ mojom::FeedItemMetadataPtr PickRouletteAndRemove( }); // None of the items are eligible to be picked. - if (std::accumulate(weights.begin(), weights.end(), 0.0) == 0) { + const auto total_weight = + std::accumulate(weights.begin(), weights.end(), 0.0); + if (total_weight == 0) { return nullptr; } - if (use_softmax) { - SoftmaxWithTemperature(weights); - } - - double total_weight = std::accumulate(weights.begin(), weights.end(), 0.0); double picked_value = base::RandDouble() * total_weight; double current_weight = 0; @@ -407,7 +383,7 @@ std::vector GenerateBlock( auto image_url = metadata->image->is_padded_image_url() ? metadata->image->get_padded_image_url() : metadata->image->get_image_url(); - return image_url.is_valid() ? weight.weighting : 0; + return image_url.is_valid() && weight.subscribed ? weight.weighting : 0; })); // We might not be able to generate a hero card, if none of the articles in @@ -513,13 +489,12 @@ std::vector GenerateBlockFromContentGroups( auto hero_article = PickRouletteAndRemove(articles, get_weighting(/*is_hero*/ true)); - if (!hero_article) { - DVLOG(1) << "Failed to generate hero"; - return result; - } - result.push_back(mojom::FeedItemV2::NewHero( - mojom::HeroArticle::New(std::move(hero_article)))); + // This may fail if the the content group has no images. + if (hero_article) { + result.push_back(mojom::FeedItemV2::NewHero( + mojom::HeroArticle::New(std::move(hero_article)))); + } const int block_min_inline = features::kBraveNewsMinBlockCards.Get(); const int block_max_inline = features::kBraveNewsMaxBlockCards.Get(); diff --git a/components/brave_news/common/features.cc b/components/brave_news/common/features.cc index ccb9836b6798..ecd1a1f83b00 100644 --- a/components/brave_news/common/features.cc +++ b/components/brave_news/common/features.cc @@ -42,7 +42,4 @@ const base::FeatureParam kBraveNewsSourceVisitsMin{ const base::FeatureParam kBraveNewsCategoryTopicRatio{ &kBraveNewsFeedUpdate, "category-topic-ratio", 0.5}; -const base::FeatureParam kBraveNewsTemperature{&kBraveNewsFeedUpdate, - "temperature", 1}; - } // namespace brave_news::features diff --git a/components/brave_news/common/features.h b/components/brave_news/common/features.h index 24670134a718..1477e94e3d62 100644 --- a/components/brave_news/common/features.h +++ b/components/brave_news/common/features.h @@ -61,11 +61,6 @@ extern const base::FeatureParam kBraveNewsSourceVisitsMin; // 80% of the clusters should be categories and 20% topics. extern const base::FeatureParam kBraveNewsCategoryTopicRatio; -// The temperature of the softmax function used to compute the sampling -// probabilities of the articles in the feed. High temperature means the -// distribution is more uniform. -extern const base::FeatureParam kBraveNewsTemperature; - } // namespace brave_news::features #endif // BRAVE_COMPONENTS_BRAVE_NEWS_COMMON_FEATURES_H_