Skip to content

Commit

Permalink
refactor: migrate to new initialize function (VowpalWabbit#4444)
Browse files Browse the repository at this point in the history
* refactor: migrate to new initialize function

* fix parser throughput

* fix test

* update python

* python fixes

* fixes

* fixes

* fixes

* deprecated usage

* rename finalize_driver to finsih, doxygen ignore _*, try fix python

* integrate rename

* python build

* undo rename

* remove redundant var

* fix test_search
  • Loading branch information
jackgerrits authored Jan 13, 2023
1 parent cbbd1db commit 24f66f4
Show file tree
Hide file tree
Showing 53 changed files with 908 additions and 946 deletions.
2 changes: 1 addition & 1 deletion doc/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ EXCLUDE_PATTERNS =
# Note that the wildcards are matched against the file with absolute path, so to
# exclude all test directories use the pattern */test/*

EXCLUDE_SYMBOLS =
EXCLUDE_SYMBOLS = _*

# The EXAMPLE_PATH tag can be used to specify one or more files or directories
# that contain example code fragments that are included (see the \include
Expand Down
3 changes: 2 additions & 1 deletion java/src/main/c++/jni_spark_vw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ JNIEXPORT void JNICALL Java_org_vowpalwabbit_spark_VowpalWabbitNative_finish(JNI
try
{
VW::sync_stats(*all);
VW::finish(*all);
all->finish();
delete all;
}
catch (...)
{
Expand Down
3 changes: 2 additions & 1 deletion java/src/main/c++/vowpalWabbit_learner_VWLearners.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ JNIEXPORT void JNICALL Java_vowpalWabbit_learner_VWLearners_closeInstance(JNIEnv
try
{
VW::workspace* vwInstance = (VW::workspace*)vwPtr;
VW::finish(*vwInstance);
vwInstance->finish();
delete vwInstance;
}
catch (...)
{
Expand Down
5 changes: 3 additions & 2 deletions library/gd_mf_weights.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "vw/config/option_group_definition.h"
#include "vw/config/options_cli.h"
#include "vw/core/crossplat_compat.h"
#include "vw/core/parse_primitives.h"
#include "vw/core/parser.h"
#include "vw/core/vw.h"

Expand Down Expand Up @@ -52,7 +53,7 @@ int main(int argc, char* argv[])
}

// initialize model
VW::workspace* model = VW::initialize(vwparams);
auto model = VW::initialize(VW::make_unique<VW::config::options_cli>(VW::split_command_line(vwparams)));
model->audit = true;

string target("--rank ");
Expand Down Expand Up @@ -131,6 +132,6 @@ int main(int argc, char* argv[])
constant << weights[ec->feature_space[VW::details::CONSTANT_NAMESPACE].indices[0]] << std::endl;

// clean up
VW::finish(*model);
model->finish();
fclose(file);
}
14 changes: 9 additions & 5 deletions library/library_example.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "vw/config/options_cli.h"
#include "vw/core/parser.h"
#include "vw/core/vw.h"

Expand All @@ -12,7 +13,8 @@ inline VW::feature vw_feature_from_string(VW::workspace& v, const std::string& f

int main(int argc, char* argv[])
{
VW::workspace* model = VW::initialize("--hash all -q st --noconstant -f train2.vw --no_stdin");
auto model = VW::initialize(VW::make_unique<VW::config::options_cli>(
std::vector<std::string>{"--hash", "all", "-q", "st", "--noconstant", "-f", "train2.vw", "--no_stdin"}));

VW::example* vec2 = VW::read_example(*model, (char*)"|s p^the_man w^the w^man |t p^un_homme w^un w^homme");
model->learn(*vec2);
Expand Down Expand Up @@ -43,10 +45,11 @@ int main(int argc, char* argv[])
std::cerr << "p3 = " << vec3->pred.scalar << std::endl;
// TODO: this does not invoke m_vw->l->finish_example()
VW::finish_example(*model, *vec3);
model->finish();
model.reset();

VW::finish(*model);

VW::workspace* model2 = VW::initialize("--hash all -q st --noconstant -i train2.vw --no_stdin");
auto model2 = VW::initialize(VW::make_unique<VW::config::options_cli>(
std::vector<std::string>{"--hash", "all", "-q", "st", "--noconstant", "-i", "train2.vw", "--no_stdin"}));
vec2 = VW::read_example(*model2, (char*)" |s p^the_man w^the w^man |t p^un_homme w^un w^homme");
model2->learn(*vec2);
std::cerr << "p4 = " << vec2->pred.scalar << std::endl;
Expand All @@ -65,5 +68,6 @@ int main(int argc, char* argv[])
}

VW::finish_example(*model2, *vec2);
VW::finish(*model2);
model2->finish();
model2.reset();
}
6 changes: 4 additions & 2 deletions library/recommend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "vw/config/option_group_definition.h"
#include "vw/config/options_cli.h"
#include "vw/core/crossplat_compat.h"
#include "vw/core/memory.h"
#include "vw/core/parse_primitives.h"
#include "vw/core/vw.h"
#include "vw/io/errno_handling.h"

Expand Down Expand Up @@ -199,7 +201,7 @@ int main(int argc, char* argv[])

// INITIALIZE WITH WHATEVER YOU WOULD PUT ON THE VW COMMAND LINE
if (verbose > 0) { fprintf(stderr, "initializing vw...\n"); }
VW::workspace* model = VW::initialize(vwparams);
auto model = VW::initialize(VW::make_unique<VW::config::options_cli>(VW::split_command_line(vwparams)));

char* estr = NULL;

Expand Down Expand Up @@ -262,7 +264,7 @@ int main(int argc, char* argv[])

if (verbose > 0) { progress(); }

VW::finish(*model);
model->finish();
fclose(fI);
fclose(fU);
return 0;
Expand Down
20 changes: 12 additions & 8 deletions library/search_generate.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "libsearch.h"
#include "vw/config/options_cli.h"
#include "vw/core/parse_primitives.h"
#include "vw/core/vw.h"

#include <cstdio>
Expand Down Expand Up @@ -425,9 +427,10 @@ class Generator : public SearchTask<input, output> // NOLINT

void run_easy()
{
VW::workspace& vw_obj = *VW::initialize(
"--search 29 --quiet --search_task hook --example_queue_limit 1024 --search_rollin learn --search_rollout none");
Generator task(vw_obj);
auto vw_obj = VW::initialize(
VW::make_unique<VW::config::options_cli>(std::vector<std::string>{"--search", "29", "--quiet", "--search_task",
"hook", "--example_queue_limit", "1024", "--search_rollin", "learn", "--search_rollout", "none"}));
Generator task(*vw_obj);
output out("");

std::vector<input> training_data = {input("maison", "house"), input("lune", "moon"),
Expand Down Expand Up @@ -522,7 +525,8 @@ void train()
"none -q i: --ngram i15 --skips i5 --ngram c15 --ngram w6 --skips c3 --skips w3"); // --search_use_passthrough_repr");
// // -q si -q wi -q ci -q di
// -f my_model
VW::workspace* vw_obj = VW::initialize(init_str);
auto vw_obj = VW::initialize(VW::make_unique<VW::config::options_cli>(VW::split_command_line(init_str)));

cerr << init_str << endl;
// Generator gen(*vw_obj, nullptr); // &dict);
for (size_t pass = 1; pass <= 20; pass++)
Expand All @@ -533,14 +537,14 @@ void train()
// run_istream(gen, "phrase-table.te", false, 100000);
run_easy();
}
VW::finish(*vw_obj);
vw_obj->finish();
}

void predict()
{
VW::workspace& vw_obj = *VW::initialize("--quiet -t --example_queue_limit 1024 -i my_model");
// run(vw_obj);
VW::finish(vw_obj);
auto vw_obj = VW::initialize(VW::make_unique<VW::config::options_cli>(
std::vector<std::string>{"--quiet", "-t", "--example_queue_limit", "1024", "-i", "my_model"}));
vw_obj->finish();
}

int main(int argc, char* argv[])
Expand Down
37 changes: 21 additions & 16 deletions library/test_search.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "libsearch.h"
#include "vw/config/options_cli.h"
#include "vw/core/memory.h"
#include "vw/core/reductions/search/search_sequencetask.h"
#include "vw/core/vw.h"

Expand Down Expand Up @@ -96,18 +98,19 @@ void train()
{
// initialize VW as usual, but use 'hook' as the search_task
cerr << endl << endl << "##### train() #####" << endl << endl;
VW::workspace& vw_obj =
*VW::initialize("--search 4 --quiet --search_task hook --example_queue_limit 1024 -f my_model");
run(vw_obj);
VW::finish(vw_obj, false);
auto vw_obj = VW::initialize(VW::make_unique<VW::config::options_cli>(std::vector<std::string>{
"--search", "4", "--quiet", "--search_task", "hook", "--example_queue_limit", "1024", "-f", "my_model"}));
run(*vw_obj);
vw_obj->finish();
}

void predict()
{
cerr << endl << endl << "##### predict() #####" << endl << endl;
VW::workspace& vw_obj = *VW::initialize("--quiet -t --example_queue_limit 1024 -i my_model");
run(vw_obj);
VW::finish(vw_obj, false);
auto vw_obj = VW::initialize(VW::make_unique<VW::config::options_cli>(
std::vector<std::string>{"--quiet", "-t", "--example_queue_limit", "1024", "-i", "my_model"}));
run(*vw_obj);
vw_obj->finish();
}

void test_buildin_task()
Expand All @@ -122,24 +125,26 @@ void test_buildin_task()

// now, load that model using the BuiltInTask library
cerr << endl << endl << "##### test BuiltInTask #####" << endl << endl;
VW::workspace& vw_obj = *VW::initialize("-t --search_task hook");

auto vw_obj =
VW::initialize(VW::make_unique<VW::config::options_cli>(std::vector<std::string>{"-t", "--search_task", "hook"}));
{ // create a new scope for the task object
BuiltInTask task(vw_obj, &SequenceTask::task);
BuiltInTask task(*vw_obj, &SequenceTask::task);
VW::multi_ex mult_ex;
mult_ex.push_back(VW::read_example(vw_obj, (char*)"1 | a"));
mult_ex.push_back(VW::read_example(vw_obj, (char*)"1 | a"));
mult_ex.push_back(VW::read_example(vw_obj, (char*)"1 | a"));
mult_ex.push_back(VW::read_example(vw_obj, (char*)"1 | a"));
mult_ex.push_back(VW::read_example(vw_obj, (char*)"1 | a"));
mult_ex.push_back(VW::read_example(*vw_obj, (char*)"1 | a"));
mult_ex.push_back(VW::read_example(*vw_obj, (char*)"1 | a"));
mult_ex.push_back(VW::read_example(*vw_obj, (char*)"1 | a"));
mult_ex.push_back(VW::read_example(*vw_obj, (char*)"1 | a"));
mult_ex.push_back(VW::read_example(*vw_obj, (char*)"1 | a"));
std::vector<action> out;
task.predict(mult_ex, out);
cerr << "out (should be 1 2 3 4 3) =";
for (size_t i = 0; i < out.size(); i++) { cerr << " " << out[i]; }
cerr << endl;
for (size_t i = 0; i < mult_ex.size(); i++) { VW::finish_example(vw_obj, *mult_ex[i]); }
for (size_t i = 0; i < mult_ex.size(); i++) { VW::finish_example(*vw_obj, *mult_ex[i]); }
}

VW::finish(vw_obj, false);
vw_obj->finish();
}

int main(int argc, char* argv[])
Expand Down
6 changes: 4 additions & 2 deletions nuget/native/test/main.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "vw/config/options_cli.h"
#include "vw/core/memory.h"
#include "vw/core/vw.h"

int main()
{
auto* workspace = VW::initialize("--quiet");
VW::finish(*workspace);
auto workspace = VW::initialize(VW::make_unique<VW::config::options_cli>(std::vector<std::string>{"--quiet"}));
workspace->finish();
}
36 changes: 27 additions & 9 deletions python/pylibvw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "vw/core/global_data.h"
#include "vw/core/kskip_ngram_transformer.h"
#include "vw/core/learner.h"
#include "vw/core/memory.h"
#include "vw/core/merge.h"
#include "vw/core/multiclass.h"
#include "vw/core/multilabel.h"
Expand Down Expand Up @@ -268,21 +269,38 @@ vw_ptr my_initialize_with_log(py::list args, py_log_wrapper_ptr py_log)

if (std::find(args_vec.begin(), args_vec.end(), "--no_stdin") == args_vec.end()) { args_vec.push_back("--no_stdin"); }

trace_message_t trace_listener = nullptr;
VW::driver_output_func_t trace_listener = nullptr;
void* trace_context = nullptr;
std::unique_ptr<VW::io::logger> logger_ptr = nullptr;

if (py_log)
{
trace_listener = (py_log_wrapper::trace_listener_py);
trace_listener = py_log_wrapper::trace_listener_py;
trace_context = py_log.get();
}

std::unique_ptr<VW::config::options_i, options_deleter_type> options(
new VW::config::options_cli(args_vec), [](VW::config::options_i* ptr) { delete ptr; });
const auto log_function = [](void* context, VW::io::log_level level, const std::string& message)
{
_UNUSED(level);
try
{
auto inst = static_cast<py_log_wrapper*>(context);
inst->py_log.attr("log")(message);
}
catch (...)
{
// TODO: Properly translate and return Python exception. #2169
PyErr_Print();
PyErr_Clear();
std::cerr << "error using python logging. ignoring." << std::endl;
}
};

logger_ptr = VW::make_unique<VW::io::logger>(VW::io::create_custom_sink_logger(py_log.get(), log_function));
}

VW::workspace* foo = VW::initialize(std::move(options), nullptr, false, trace_listener, trace_context);
// return boost::shared_ptr<VW::workspace>(foo, [](vw *all){VW::finish(*all);});
return boost::shared_ptr<VW::workspace>(foo);
auto options = VW::make_unique<VW::config::options_cli>(args_vec);
auto foo = VW::initialize(std::move(options), nullptr, trace_listener, trace_context, logger_ptr.get());
return boost::shared_ptr<VW::workspace>(foo.release());
}

vw_ptr my_initialize(py::list args) { return my_initialize_with_log(args, nullptr); }
Expand Down Expand Up @@ -340,7 +358,7 @@ py::dict get_learner_metrics(vw_ptr all)

void my_finish(vw_ptr all)
{
VW::finish(*all, false); // don't delete all because python will do that for us!
all->finish(); // don't delete all because python will do that for us!
}

void my_save(vw_ptr all, std::string name) { VW::save_predictor(*all, name); }
Expand Down
21 changes: 10 additions & 11 deletions test/benchmarks/benchmark_funcs.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "vw/config/options_cli.h"
#include "vw/core/parser.h"
#include "vw/core/vw.h"
#include "vw/io/io_adapter.h"
Expand All @@ -12,13 +13,14 @@ static void benchmark_sum_ft_squared_char(benchmark::State& state)
"1 1.0 zebra|MetricFeatures:3.28 height:1.5 length:2.0 |Says black with white stripes |OtherFeatures "
"NumberOfLegs:4.0 HasStripes";

auto vw = VW::initialize("--quiet -q MS --cubic MOS", nullptr, false, nullptr, nullptr);
auto vw = VW::initialize(
VW::make_unique<VW::config::options_cli>(std::vector<std::string>{"--quiet", "-q", "MS", "--cubic", "MOS"}));

VW::multi_ex examples;
io_buf buffer;
buffer.add_file(VW::io::create_buffer_view(example_string.data(), example_string.size()));
examples.push_back(&VW::get_unused_example(vw));
vw->example_parser->reader(vw, buffer, examples);
examples.push_back(&VW::get_unused_example(vw.get()));
vw->example_parser->reader(vw.get(), buffer, examples);
example* ex = examples[0];
VW::setup_example(*vw, ex);
for (auto _ : state)
Expand All @@ -28,7 +30,6 @@ static void benchmark_sum_ft_squared_char(benchmark::State& state)
benchmark::DoNotOptimize(result);
}
VW::finish_example(*vw, *ex);
VW::finish(*vw);
}

static void benchmark_sum_ft_squared_extent(benchmark::State& state)
Expand All @@ -37,16 +38,15 @@ static void benchmark_sum_ft_squared_extent(benchmark::State& state)
"1 1.0 zebra|MetricFeatures:3.28 height:1.5 length:2.0 |Says black with white stripes |OtherFeatures "
"NumberOfLegs:4.0 HasStripes";

auto vw = VW::initialize(
"--quiet --experimental_full_name_interactions MetricFeatures|Says --experimental_full_name_interactions "
"MetricFeatures|OtherFeatures|Says",
nullptr, false, nullptr, nullptr);
auto vw = VW::initialize(VW::make_unique<VW::config::options_cli>(
std::vector<std::string>{"--quiet", "--experimental_full_name_interactions", "MetricFeatures|Says",
"--experimental_full_name_interactions", "MetricFeatures|OtherFeatures|Says"}));

VW::multi_ex examples;
io_buf buffer;
buffer.add_file(VW::io::create_buffer_view(example_string.data(), example_string.size()));
examples.push_back(&VW::get_unused_example(vw));
vw->example_parser->reader(vw, buffer, examples);
examples.push_back(&VW::get_unused_example(vw.get()));
vw->example_parser->reader(vw.get(), buffer, examples);
example* ex = examples[0];
VW::setup_example(*vw, ex);
for (auto _ : state)
Expand All @@ -56,7 +56,6 @@ static void benchmark_sum_ft_squared_extent(benchmark::State& state)
benchmark::DoNotOptimize(result);
}
VW::finish_example(*vw, *ex);
VW::finish(*vw);
}

BENCHMARK(benchmark_sum_ft_squared_char);
Expand Down
Loading

0 comments on commit 24f66f4

Please sign in to comment.