Skip to content

Commit

Permalink
Uplift of #21414 (squashed) to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-builds committed Dec 21, 2023
1 parent 732ae2c commit 243e173
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 47 deletions.
53 changes: 14 additions & 39 deletions components/brave_news/browser/feed_v2_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <algorithm>
#include <iterator>
#include <locale>
#include <numeric>
#include <string>
#include <tuple>
Expand Down Expand Up @@ -289,28 +288,6 @@ using GetWeighting =
base::RepeatingCallback<double(const mojom::FeedItemMetadataPtr& metadata,
const ArticleWeight& weight)>;

// 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<double>& 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<ContentGroup>& eligible_content_groups) {
Expand All @@ -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<double> weights;
base::ranges::transform(articles, std::back_inserter(weights),
Expand All @@ -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;

Expand Down Expand Up @@ -407,7 +383,7 @@ std::vector<mojom::FeedItemV2Ptr> 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
Expand Down Expand Up @@ -513,13 +489,12 @@ std::vector<mojom::FeedItemV2Ptr> 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();
Expand Down
3 changes: 0 additions & 3 deletions components/brave_news/common/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,4 @@ const base::FeatureParam<double> kBraveNewsSourceVisitsMin{
const base::FeatureParam<double> kBraveNewsCategoryTopicRatio{
&kBraveNewsFeedUpdate, "category-topic-ratio", 0.5};

const base::FeatureParam<double> kBraveNewsTemperature{&kBraveNewsFeedUpdate,
"temperature", 1};

} // namespace brave_news::features
5 changes: 0 additions & 5 deletions components/brave_news/common/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ extern const base::FeatureParam<double> kBraveNewsSourceVisitsMin;
// 80% of the clusters should be categories and 20% topics.
extern const base::FeatureParam<double> 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<double> kBraveNewsTemperature;

} // namespace brave_news::features

#endif // BRAVE_COMPONENTS_BRAVE_NEWS_COMMON_FEATURES_H_

0 comments on commit 243e173

Please sign in to comment.