Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enforce force reductions to add metrics in their own namespace #4506

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion vowpalwabbit/core/include/vw/core/learner.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,18 @@ class common_learner_builder
this->learner_ptr->_cleanup_example_f = [fn_ptr, data](polymorphic_ex ex) { fn_ptr(*data, ex); };
)

// Set the function pointer for persisting metrics for the learner.
// This function enforces that force reductions can only add metrics in their own namespace.
// The metrics output dictionary is a dictionary of dictionaries, where each sub-dictionary
// contains the metrics for a single learner. The key for each sub-dictionary is the learner's name.
LEARNER_BUILDER_DEFINE(set_persist_metrics(void (*fn_ptr)(DataT&, metric_sink&)),
assert(fn_ptr != nullptr);
DataT* data = this->learner_data.get();
this->learner_ptr->_persist_metrics_f = [fn_ptr, data](metric_sink& metrics) { fn_ptr(*data, metrics); };
std::string learner_name = this->name;
this->learner_ptr->_persist_metrics_f = [fn_ptr, data, learner_name](metric_sink& metrics) {
metrics[learner_name] = {};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here you should be able to tell metric_sink that it should push this new context called learner_name

something like

{
 metrics.push_context(learner_name);
 fn_ptr(*data, metrics);
 metrics.pop_context();
}

back inside any fn_ptr() you should be able to only operate inside this subcontext of the metrics object - you should not be able to access other contexts that might also exist.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. This makes sense.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lalo, I am having a question. I did not find any references for push_context and pop_context methods in the VW::metric_sink class. Am I missing something over here? Will we have to define it?

fn_ptr(*data, metrics[learner_name]);
};
)

LEARNER_BUILDER_DEFINE(set_pre_save_load(void (*fn_ptr)(VW::workspace& all, DataT&)),
Expand Down