Skip to content

Commit 1c5fedf

Browse files
authored
fix: sparse weights iterator end->end (#4647)
* fix: sparse weights iterator end->end * fix sparse not_null and allow more bits for sparse * validate warning * lint * use fmt * clang * use warning interface * Update settings.json * use str
1 parent de52303 commit 1c5fedf

File tree

7 files changed

+92
-4
lines changed

7 files changed

+92
-4
lines changed

test/core.vwtest.json

+28
Original file line numberDiff line numberDiff line change
@@ -6017,5 +6017,33 @@
60176017
"input_files": [
60186018
"train-sets/single_empty_lines.txt"
60196019
]
6020+
},
6021+
{
6022+
"id": 465,
6023+
"desc": "cb_explore_adf with epsilon-greedy exploration using --sparse_weights and saving model",
6024+
"vw_command": "--cb_explore_adf --epsilon 0.1 -d train-sets/cb_test.ldf --noconstant --sparse_weights -f standard_sparse_model.vw",
6025+
"diff_files": {
6026+
"stderr": "train-sets/ref/sparse_save_check.stderr",
6027+
"stdout": "train-sets/ref/sparse_save_check.stdout"
6028+
},
6029+
"input_files": [
6030+
"train-sets/cb_test.ldf"
6031+
]
6032+
},
6033+
{
6034+
"id": 466,
6035+
"desc": "cb_explore_adf with epsilon-greedy exploration using --sparse_weights and loading model",
6036+
"vw_command": "--cb_explore_adf --epsilon 0.1 -d train-sets/cb_test.ldf --noconstant --sparse_weights -i standard_sparse_model.vw",
6037+
"diff_files": {
6038+
"stderr": "train-sets/ref/sparse_load_check.stderr",
6039+
"stdout": "train-sets/ref/sparse_load_check.stdout"
6040+
},
6041+
"input_files": [
6042+
"train-sets/cb_test.ldf",
6043+
"standard_sparse_model.vw"
6044+
],
6045+
"depends_on": [
6046+
465
6047+
]
60206048
}
60216049
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using no cache
2+
Reading datafile = train-sets/cb_test.ldf
3+
num sources = 1
4+
Num weight bits = 18
5+
learning rate = 0.5
6+
initial_t = 3
7+
power_t = 0.5
8+
cb_type = mtr
9+
Enabled learners: gd, scorer-identity, csoaa_ldf-rank, cb_adf, cb_explore_adf_greedy, shared_feature_merger
10+
Input label = CB
11+
Output pred = ACTION_PROBS
12+
average since example example current current current
13+
loss last counter weight label predict features
14+
0.066667 0.066667 1 1.0 0:1:0.5 1:0.48 15
15+
0.033333 0.000000 2 2.0 1:0:0.5 1:0.95 6
16+
17+
finished run
18+
number of examples = 3
19+
weighted example sum = 3.000000
20+
weighted label sum = 0.000000
21+
average loss = 0.033333
22+
total feature number = 27

test/train-sets/ref/sparse_load_check.stdout

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
final_regressor = standard_sparse_model.vw
2+
using no cache
3+
Reading datafile = train-sets/cb_test.ldf
4+
num sources = 1
5+
Num weight bits = 18
6+
learning rate = 0.5
7+
initial_t = 0
8+
power_t = 0.5
9+
cb_type = mtr
10+
Enabled learners: gd, scorer-identity, csoaa_ldf-rank, cb_adf, cb_explore_adf_greedy, shared_feature_merger
11+
Input label = CB
12+
Output pred = ACTION_PROBS
13+
average since example example current current current
14+
loss last counter weight label predict features
15+
0.666667 0.666667 1 1.0 0:1:0.5 0:0.33 15
16+
0.333333 0.000000 2 2.0 1:0:0.5 1:0.95 6
17+
18+
finished run
19+
number of examples = 3
20+
weighted example sum = 3.000000
21+
weighted label sum = 0.000000
22+
average loss = 0.333333
23+
total feature number = 27

test/train-sets/ref/sparse_save_check.stdout

Whitespace-only changes.

vowpalwabbit/core/include/vw/core/array_parameters_sparse.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@ class sparse_parameters
7070
sparse_parameters& operator=(sparse_parameters&&) noexcept = delete;
7171
sparse_parameters(sparse_parameters&&) noexcept = delete;
7272

73-
bool not_null() { return (_weight_mask > 0 && !_map.empty()); }
73+
bool not_null() { return (_weight_mask > 0); }
7474
VW::weight* first() { THROW_OR_RETURN("Allreduce currently not supported in sparse", nullptr); }
7575

7676
// iterator with stride
7777
iterator begin() { return iterator(_map.begin()); }
78-
iterator end() { return iterator(_map.begin()); }
78+
iterator end() { return iterator(_map.end()); }
7979

8080
// const iterator
8181
const_iterator cbegin() const { return const_iterator(_map.begin()); }
82-
const_iterator cend() const { return const_iterator(_map.begin()); }
82+
const_iterator cend() const { return const_iterator(_map.end()); }
8383

8484
inline VW::weight& operator[](size_t i) { return *(get_or_default_and_get(i)); }
8585

vowpalwabbit/core/src/vw_validate.cc

+16-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ void validate_default_bits(VW::workspace& all, uint32_t local_num_bits)
3535
void validate_num_bits(VW::workspace& all)
3636
{
3737
if (all.initial_weights_config.num_bits > sizeof(size_t) * 8 - 3)
38-
THROW("Only " << sizeof(size_t) * 8 - 3 << " or fewer bits allowed. If this is a serious limit, speak up.");
38+
{
39+
if (all.weights.sparse)
40+
{
41+
if (all.weights.sparse)
42+
{
43+
all.logger.err_warn(
44+
"Bit size is {}. While this is allowed for sparse weights, it may cause an overflow and is strongly "
45+
"recommended to use a smaller value.",
46+
all.initial_weights_config.num_bits);
47+
}
48+
}
49+
else
50+
{
51+
THROW("Only " << sizeof(size_t) * 8 - 3 << " or fewer bits allowed. If this is a serious limit, speak up.");
52+
}
53+
}
3954
}
4055
} // namespace VW

0 commit comments

Comments
 (0)