-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathphoenix_scorer.rs
More file actions
131 lines (115 loc) · 4.68 KB
/
Copy pathphoenix_scorer.rs
File metadata and controls
131 lines (115 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use crate::models::candidate::CandidateHelpers;
use crate::models::candidate::PostCandidate;
use crate::models::query::ScoredPostsQuery;
use crate::params::{
PhoenixInferenceClusterId, PhoenixRankerNewUserHistoryThreshold,
PhoenixRankerNewUserInferenceClusterId, RerankerHeadTag,
};
use crate::util::egress::PredictionDispatch;
use crate::util::phoenix_request::build_prediction_request;
use tonic::async_trait;
use xai_candidate_pipeline::component_library::clients::phoenix_prediction_client::PhoenixCluster;
use xai_candidate_pipeline::component_library::utils::current_timestamp_millis;
use xai_candidate_pipeline::scorer::Scorer;
use xai_recsys_proto::ProductSurface;
pub const PHOENIX_RANKER_KILL_SWITCH_DECIDER: &str = "disable_home_mixer_phoenix_ranker";
pub struct PhoenixScorer {
pub dispatch: PredictionDispatch,
}
impl PhoenixScorer {
fn resolve_cluster(query: &ScoredPostsQuery) -> PhoenixCluster {
let configured_cluster =
PhoenixCluster::parse(&query.params.get(PhoenixInferenceClusterId));
let threshold: u64 = query.params.get(PhoenixRankerNewUserHistoryThreshold);
if threshold > 0 {
let action_count = query
.scoring_sequence
.as_ref()
.and_then(|s| s.metadata.as_ref())
.map(|m| m.length)
.unwrap_or(0);
if action_count < threshold {
return PhoenixCluster::parse(
&query.params.get(PhoenixRankerNewUserInferenceClusterId),
);
}
}
if let Some(decider) = &query.decider {
let is_prod = matches!(
configured_cluster,
PhoenixCluster::Experiment1Fou | PhoenixCluster::Experiment2Fou
);
if is_prod {
if decider.enabled("override_qf_use_experiment2_fou") {
return PhoenixCluster::Experiment2Fou;
}
if decider.enabled("override_qf_use_experiment1_fou") {
return PhoenixCluster::Experiment1Fou;
}
}
}
configured_cluster
}
}
#[async_trait]
impl Scorer<ScoredPostsQuery, PostCandidate> for PhoenixScorer {
fn enable(&self, query: &ScoredPostsQuery) -> bool {
if query.has_cached_posts {
return false;
}
let killed = query
.decider
.as_ref()
.is_some_and(|d| d.enabled(PHOENIX_RANKER_KILL_SWITCH_DECIDER));
!killed
}
async fn score(
&self,
query: &ScoredPostsQuery,
candidates: &[PostCandidate],
) -> Vec<Result<PostCandidate, String>> {
let last_scored_at_ms = current_timestamp_millis();
let product_surface = if query.in_network_only {
ProductSurface::HomeTimelineRankedFollowing
} else {
ProductSurface::HomeTimelineRanking
};
if query.scoring_sequence.is_none() {
return vec![Ok(PostCandidate::default()); candidates.len()];
};
let cluster = Self::resolve_cluster(query);
let request = build_prediction_request(query, candidates, product_surface);
let predictions = self
.dispatch
.predict_with_fallback(query, cluster, request)
.await
.map_err(|e| format!("Phoenix prediction failed: {}", e));
let predictions = match predictions {
Ok(predictions) => predictions,
Err(err) => return vec![Err(err); candidates.len()],
};
candidates
.iter()
.map(|c| PostCandidate {
phoenix_scores: predictions.candidate_scores(&c.get_original_tweet_id()),
backbone_scores: predictions.candidate_backbone_scores(&c.get_original_tweet_id()),
served_slate_context: predictions
.candidate_slate_context(&c.get_original_tweet_id())
.map(Into::into),
prediction_request_id: Some(query.prediction_id),
last_scored_at_ms,
reranker_head_tag: Some(query.params.get(RerankerHeadTag) as u32),
..Default::default()
})
.map(Ok)
.collect()
}
fn update(&self, candidate: &mut PostCandidate, scored: PostCandidate) {
candidate.phoenix_scores = scored.phoenix_scores;
candidate.backbone_scores = scored.backbone_scores;
candidate.served_slate_context = scored.served_slate_context;
candidate.prediction_request_id = scored.prediction_request_id;
candidate.last_scored_at_ms = scored.last_scored_at_ms;
candidate.reranker_head_tag = scored.reranker_head_tag;
}
}