forked from VowpalWabbit/vowpal_wabbit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_example_json.h
More file actions
1956 lines (1647 loc) · 56.8 KB
/
parse_example_json.h
File metadata and controls
1956 lines (1647 loc) · 56.8 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) by respective owners including Yahoo!, Microsoft, and
// individual contributors. All rights reserved. Released under a BSD (revised)
// license as described in the file LICENSE.
#pragma once
#include "v_array.h"
#include "future_compat.h"
#include <cstring>
#include <cfloat>
// seems to help with skipping spaces
//#define RAPIDJSON_SIMD
//#define RAPIDJSON_SSE42
// Let MSVC know that it should not even try to compile RapidJSON as managed
// - pragma documentation: https://docs.microsoft.com/en-us/cpp/preprocessor/managed-unmanaged?view=vs-2017
// - /clr compilation detection: https://docs.microsoft.com/en-us/cpp/dotnet/how-to-detect-clr-compilation?view=vs-2017
#if (_MANAGED == 1) || (_M_CEE == 1)
# pragma managed(push, off)
#endif
// RapidJson triggers this warning by memcpying non-trivially copyable type. Ignore it so that our warnings are not
// polluted by it.
// https://github.com/Tencent/rapidjson/issues/1700
VW_WARNING_STATE_PUSH
VW_WARNING_DISABLE_CLASS_MEMACCESS
#include <rapidjson/reader.h>
#include <rapidjson/error/en.h>
VW_WARNING_STATE_POP
#include "io/logger.h"
#if (_MANAGED == 1) || (_M_CEE == 1)
# pragma managed(pop)
#endif
#include "cb.h"
#include "conditional_contextual_bandit.h"
#include "cb_continuous_label.h"
#include "best_constant.h"
#include "json_utils.h"
#include "parse_slates_example_json.h"
#include "vw_string_view.h"
#include <algorithm>
#include <vector>
#include <limits>
#include <sstream>
// portability fun
#ifndef _WIN32
# define _stricmp strcasecmp
#endif
namespace logger = VW::io::logger;
using namespace rapidjson;
struct vw;
template <bool audit>
struct BaseState;
template <bool audit>
struct Context;
template <bool audit>
struct BaseState
{
const char* name;
BaseState(const char* pname) : name(pname) {}
virtual BaseState<audit>* Null(Context<audit>& ctx)
{
// ignore Null by default and stay in the current state
return ctx.previous_state == nullptr ? this : ctx.previous_state;
}
virtual BaseState<audit>* Bool(Context<audit>& ctx, bool b)
{
ctx.error() << "Unexpected token: bool (" << (b ? "true" : "false") << ")";
return nullptr;
}
virtual BaseState<audit>* Float(Context<audit>& ctx, float v)
{
ctx.error() << "Unexpected token: float (" << v << ")";
return nullptr;
}
virtual BaseState<audit>* Uint(Context<audit>& ctx, unsigned v)
{
ctx.error() << "Unexpected token: uint (" << v << ")";
return nullptr;
}
virtual BaseState<audit>* String(Context<audit>& ctx, const char* str, rapidjson::SizeType len, bool)
{
ctx.error() << "Unexpected token: std::string('" << str << "' len: " << len << ")";
return nullptr;
}
virtual BaseState<audit>* StartObject(Context<audit>& ctx)
{
ctx.error() << "Unexpected token: {";
return nullptr;
}
virtual BaseState<audit>* Key(Context<audit>& ctx, const char* str, rapidjson::SizeType len, bool /* copy */)
{
ctx.error() << "Unexpected token: key('" << str << "' len: " << len << ")";
return nullptr;
}
virtual BaseState<audit>* EndObject(Context<audit>& ctx, rapidjson::SizeType)
{
ctx.error() << "Unexpected token: }";
return nullptr;
}
virtual BaseState<audit>* StartArray(Context<audit>& ctx)
{
ctx.error() << "Unexpected token: [";
return nullptr;
}
virtual BaseState<audit>* EndArray(Context<audit>& ctx, rapidjson::SizeType)
{
ctx.error() << "Unexpected token: ]";
return nullptr;
}
};
template <bool audit>
class ArrayToPdfState : public BaseState<audit>
{
private:
BaseState<audit>* obj_return_state;
public:
VW::continuous_actions::pdf_segment segment;
BaseState<audit>* return_state;
ArrayToPdfState() : BaseState<audit>("ArrayToPdfObject") {}
BaseState<audit>* StartObject(Context<audit>& ctx) override
{
obj_return_state = ctx.previous_state;
return this;
}
BaseState<audit>* Key(Context<audit>& ctx, const char* str, rapidjson::SizeType len, bool /* copy */) override
{
ctx.key = str;
ctx.key_length = len;
return this;
}
BaseState<audit>* String(Context<audit>& ctx, const char* str, rapidjson::SizeType /* len */, bool) override
{
if (_stricmp(str, "NaN") != 0)
{
ctx.error() << "The only supported string in the array is 'NaN'";
return nullptr;
}
return this;
}
BaseState<audit>* StartArray(Context<audit>&) override
{
segment = {0., 0., 0.};
return this;
}
BaseState<audit>* EndArray(Context<audit>& ctx, rapidjson::SizeType) override
{
// check valid pdf else remove
auto& red_fts = ctx.ex->_reduction_features.template get<VW::continuous_actions::reduction_features>();
if (!VW::continuous_actions::is_valid_pdf(red_fts.pdf)) { red_fts.pdf.clear(); }
return return_state;
}
BaseState<audit>* Float(Context<audit>& ctx, float v) override
{
if (!_stricmp(ctx.key, "left")) { segment.left = v; }
else if (!_stricmp(ctx.key, "right"))
{
segment.right = v;
}
else if (!_stricmp(ctx.key, "pdf_value"))
{
segment.pdf_value = v;
}
else if (!_stricmp(ctx.key, "chosen_action"))
{
ctx.ex->_reduction_features.template get<VW::continuous_actions::reduction_features>().chosen_action = v;
}
else
{
ctx.error() << "Unsupported label property: '" << ctx.key << "' len: " << ctx.key_length;
return nullptr;
}
return this;
}
BaseState<audit>* Uint(Context<audit>& ctx, unsigned v) override { return Float(ctx, static_cast<float>(v)); }
BaseState<audit>* EndObject(Context<audit>& ctx, rapidjson::SizeType) override
{
ctx.ex->_reduction_features.template get<VW::continuous_actions::reduction_features>().pdf.push_back(segment);
segment = {0., 0., 0.};
return obj_return_state;
}
};
template <bool audit>
class LabelObjectState : public BaseState<audit>
{
private:
BaseState<audit>* return_state;
public:
CB::cb_class cb_label;
VW::cb_continuous::continuous_label_elm cont_label_element;
bool found;
bool found_cb;
bool found_cb_continuous;
std::vector<unsigned int> actions;
std::vector<float> probs;
std::vector<unsigned int> inc;
LabelObjectState() : BaseState<audit>("LabelObject") {}
void init(vw* /* all */)
{
found = found_cb = found_cb_continuous = false;
cb_label = CB::cb_class{};
cont_label_element = {0., 0., 0.};
}
BaseState<audit>* StartObject(Context<audit>& ctx) override
{
ctx.all->example_parser->lbl_parser.default_label(&ctx.ex->l);
// don't allow { { { } } }
if (ctx.previous_state == this)
{
ctx.error() << "invalid label object. nested objected.";
return nullptr;
}
// keep previous state
return_state = ctx.previous_state;
return this;
}
BaseState<audit>* Key(Context<audit>& ctx, const char* str, rapidjson::SizeType len, bool /* copy */) override
{
ctx.key = str;
ctx.key_length = len;
return this;
}
BaseState<audit>* String(Context<audit>& ctx, const char* str, rapidjson::SizeType /* len */, bool) override
{
if (_stricmp(str, "NaN") != 0)
{
ctx.error() << "Unsupported label property: '" << ctx.key << "' len: " << ctx.key_length
<< ". The only string value supported in this context is NaN.";
return nullptr;
}
// simple
if (!_stricmp(ctx.key, "Label"))
{
ctx.ex->l.simple.label = std::numeric_limits<float>::quiet_NaN();
found = true;
}
else if (!_stricmp(ctx.key, "Initial"))
{
auto& simple_red_features = ctx.ex->_reduction_features.template get<simple_label_reduction_features>();
simple_red_features.initial = std::numeric_limits<float>::quiet_NaN();
found = true;
}
else if (!_stricmp(ctx.key, "Weight"))
{
auto& simple_red_features = ctx.ex->_reduction_features.template get<simple_label_reduction_features>();
simple_red_features.weight = std::numeric_limits<float>::quiet_NaN();
found = true;
}
// CB/CA
else if (!_stricmp(ctx.key, "Cost"))
{
if (found_cb_continuous) { cont_label_element.cost = std::numeric_limits<float>::quiet_NaN(); }
else
{
cb_label.cost = std::numeric_limits<float>::quiet_NaN();
found_cb = true;
}
}
else if (!_stricmp(ctx.key, "Probability"))
{
cb_label.probability = std::numeric_limits<float>::quiet_NaN();
found_cb = true;
}
// CA
else if (!_stricmp(ctx.key, "Pdf_value") && found_cb_continuous)
{
cont_label_element.pdf_value = std::numeric_limits<float>::quiet_NaN();
}
else
{
ctx.error() << "Unsupported label property: '" << ctx.key << "' len: " << ctx.key_length;
return nullptr;
}
return this;
}
BaseState<audit>* Float(Context<audit>& ctx, float v) override
{
// simple
if (!_stricmp(ctx.key, "Label"))
{
ctx.ex->l.simple.label = v;
found = true;
}
else if (!_stricmp(ctx.key, "Initial"))
{
auto& simple_red_features = ctx.ex->_reduction_features.template get<simple_label_reduction_features>();
simple_red_features.initial = v;
found = true;
}
else if (!_stricmp(ctx.key, "Weight"))
{
ctx.ex->weight = v;
found = true;
}
// CB/CA
else if (!_stricmp(ctx.key, "Action"))
{
if (found_cb_continuous) { cont_label_element.action = v; }
else
{
cb_label.action = static_cast<uint32_t>(v);
found_cb = true;
}
}
else if (!_stricmp(ctx.key, "Cost"))
{
if (found_cb_continuous) { cont_label_element.cost = v; }
else
{
cb_label.cost = v;
found_cb = true;
}
}
else if (!_stricmp(ctx.key, "Probability"))
{
cb_label.probability = v;
found_cb = true;
}
// CA
else if (!_stricmp(ctx.key, "Pdf_value") && found_cb_continuous)
{
cont_label_element.pdf_value = v;
}
else
{
ctx.error() << "Unsupported label property: '" << ctx.key << "' len: " << ctx.key_length;
return nullptr;
}
return this;
}
BaseState<audit>* Uint(Context<audit>& ctx, unsigned v) override { return Float(ctx, static_cast<float>(v)); }
BaseState<audit>* EndObject(Context<audit>& ctx, rapidjson::SizeType) override
{
if (ctx.all->example_parser->lbl_parser.label_type == label_type_t::ccb)
{
auto& ld = ctx.ex->l.conditional_contextual_bandit;
for (auto id : inc) { ld.explicit_included_actions.push_back(id); }
inc.clear();
if ((actions.size() != 0) && (probs.size() != 0))
{
auto outcome = new CCB::conditional_contextual_bandit_outcome();
outcome->cost = cb_label.cost;
if (actions.size() != probs.size()) { THROW("Actions and probabilities must be the same length."); }
for (size_t i = 0; i < this->actions.size(); i++) { outcome->probabilities.push_back({actions[i], probs[i]}); }
actions.clear();
probs.clear();
ld.outcome = outcome;
cb_label = CB::cb_class{};
}
}
else if (ctx.all->example_parser->lbl_parser.label_type == label_type_t::slates)
{
auto& ld = ctx.ex->l.slates;
if ((actions.size() != 0) && (probs.size() != 0))
{
if (actions.size() != probs.size()) { THROW("Actions and probabilities must be the same length."); }
ld.labeled = true;
for (size_t i = 0; i < this->actions.size(); i++) { ld.probabilities.push_back({actions[i], probs[i]}); }
actions.clear();
probs.clear();
cb_label = CB::cb_class{};
}
}
else if (found_cb)
{
auto& ld = ctx.ex->l.cb;
ld.costs.push_back(cb_label);
found_cb = false;
cb_label = CB::cb_class{};
}
else if (found_cb_continuous)
{
auto* ld = &ctx.ex->l.cb_cont;
ld->costs.push_back(cont_label_element);
found_cb_continuous = false;
cont_label_element = {0., 0., 0.};
}
else if (found)
{
count_label(ctx.all->sd, ctx.ex->l.simple.label);
found = false;
}
return return_state;
}
};
// "_label_*":
template <bool audit>
struct LabelSinglePropertyState : BaseState<audit>
{
LabelSinglePropertyState() : BaseState<audit>("LabelSingleProperty") {}
BaseState<audit>* StartObject(Context<audit>& ctx) override { return ctx.label_object_state.StartObject(ctx); }
// forward _label
BaseState<audit>* Float(Context<audit>& ctx, float v) override
{
// skip "_label_"
ctx.key += 7;
ctx.key_length -= 7;
if (ctx.label_object_state.Float(ctx, v) == nullptr) return nullptr;
return ctx.previous_state;
}
BaseState<audit>* String(Context<audit>& ctx, const char* str, rapidjson::SizeType len, bool copy) override
{
// skip "_label_"
ctx.key += 7;
ctx.key_length -= 7;
if (ctx.label_object_state.String(ctx, str, len, copy) == nullptr) return nullptr;
return ctx.previous_state;
}
BaseState<audit>* Uint(Context<audit>& ctx, unsigned v) override
{
// skip "_label_"
ctx.key += 7;
ctx.key_length -= 7;
if (ctx.label_object_state.Uint(ctx, v) == nullptr) return nullptr;
return ctx.previous_state;
}
};
template <bool audit>
struct LabelIndexState : BaseState<audit>
{
int index;
LabelIndexState() : BaseState<audit>("LabelIndex"), index(-1) {}
BaseState<audit>* Uint(Context<audit>& ctx, unsigned int v) override
{
index = v;
return ctx.previous_state;
}
};
// "_label":"1"
// Note: doesn't support labelIndex
template <bool audit>
struct LabelState : BaseState<audit>
{
LabelState() : BaseState<audit>("Label") {}
BaseState<audit>* StartObject(Context<audit>& ctx) override { return ctx.label_object_state.StartObject(ctx); }
BaseState<audit>* String(Context<audit>& ctx, const char* str, rapidjson::SizeType /* len */, bool) override
{
VW::parse_example_label(*ctx.all, *ctx.ex, str);
return ctx.previous_state;
}
BaseState<audit>* Float(Context<audit>& ctx, float v) override
{
// TODO: once we introduce label types, check here
ctx.ex->l.simple.label = v;
return ctx.previous_state;
}
BaseState<audit>* Uint(Context<audit>& ctx, unsigned v) override
{
// TODO: once we introduce label types, check here
ctx.ex->l.simple.label = static_cast<float>(v);
return ctx.previous_state;
}
};
// "_text":"a b c"
template <bool audit>
struct TextState : BaseState<audit>
{
TextState() : BaseState<audit>("text") {}
BaseState<audit>* String(Context<audit>& ctx, const char* str, rapidjson::SizeType length, bool)
{
auto& ns = ctx.CurrentNamespace();
// split into individual features
const char* start = str;
const char* end = str + length;
for (char* p = const_cast<char*>(str); p != end; p++)
{
switch (*p)
{
// split on space and tab
case ' ':
case '\t':
*p = '\0';
if (p - start > 0) ns.AddFeature(ctx.all, start);
start = p + 1;
break;
// escape chars
case ':':
case '|':
*p = '_';
break;
}
}
if (start < end) ns.AddFeature(ctx.all, start);
return ctx.previous_state;
}
};
template <bool audit>
struct TagState : BaseState<audit>
{
// "_tag":"abc"
TagState() : BaseState<audit>("tag") {}
BaseState<audit>* String(Context<audit>& ctx, const char* str, SizeType length, bool)
{
ctx.ex->tag.insert(ctx.ex->tag.end(), str, str + length);
return ctx.previous_state;
}
};
template <bool audit>
struct MultiState : BaseState<audit>
{
MultiState() : BaseState<audit>("Multi") {}
BaseState<audit>* StartArray(Context<audit>& ctx) override
{
// mark shared example
if (ctx.all->example_parser->lbl_parser.label_type == label_type_t::cb)
{
CB::label* ld = &ctx.ex->l.cb;
CB::cb_class f;
f.partial_prediction = 0.;
f.action = static_cast<uint32_t>(uniform_hash("shared", 6, 0));
f.cost = FLT_MAX;
f.probability = -1.f;
ld->costs.push_back(f);
}
else if (ctx.all->example_parser->lbl_parser.label_type == label_type_t::ccb)
{
CCB::label* ld = &ctx.ex->l.conditional_contextual_bandit;
ld->type = CCB::example_type::shared;
}
else if (ctx.all->example_parser->lbl_parser.label_type == label_type_t::slates)
{
auto& ld = ctx.ex->l.slates;
ld.type = VW::slates::example_type::shared;
}
else
THROW("label type is not CB, CCB or slates")
return this;
}
BaseState<audit>* StartObject(Context<audit>& ctx) override
{
// allocate new example
ctx.ex = &(*ctx.example_factory)(ctx.example_factory_context);
ctx.all->example_parser->lbl_parser.default_label(&ctx.ex->l);
if (ctx.all->example_parser->lbl_parser.label_type == label_type_t::ccb)
{ ctx.ex->l.conditional_contextual_bandit.type = CCB::example_type::action; }
else if (ctx.all->example_parser->lbl_parser.label_type == label_type_t::slates)
{
ctx.ex->l.slates.type = VW::slates::example_type::action;
}
ctx.examples->push_back(ctx.ex);
// setup default namespace
ctx.PushNamespace(" ", this);
return &ctx.default_state;
}
BaseState<audit>* EndArray(Context<audit>& ctx, rapidjson::SizeType) override
{
// return to shared example
ctx.ex = (*ctx.examples)[0];
return &ctx.default_state;
}
};
// This state makes the assumption we are in CCB
template <bool audit>
struct SlotsState : BaseState<audit>
{
SlotsState() : BaseState<audit>("Slots") {}
BaseState<audit>* saved;
BaseState<audit>* saved_root_state;
BaseState<audit>* StartArray(Context<audit>& ctx) override
{
// drain existing added namespace
// todo check bounds
saved = ctx.PopNamespace();
saved_root_state = ctx.root_state;
ctx.root_state = this;
return this;
}
BaseState<audit>* StartObject(Context<audit>& ctx) override
{
// allocate new example
ctx.ex = &(*ctx.example_factory)(ctx.example_factory_context);
ctx.all->example_parser->lbl_parser.default_label(&ctx.ex->l);
if (ctx.all->example_parser->lbl_parser.label_type == label_type_t::ccb)
{ ctx.ex->l.conditional_contextual_bandit.type = CCB::example_type::slot; }
else if (ctx.all->example_parser->lbl_parser.label_type == label_type_t::slates)
{
ctx.ex->l.slates.type = VW::slates::example_type::slot;
}
ctx.examples->push_back(ctx.ex);
// The end object logic assumes shared example so we need to take an extra one here.
ctx.label_index_state.index = static_cast<int>(ctx.examples->size()) - 2;
// setup default namespace
ctx.PushNamespace(" ", this);
return &ctx.default_state;
}
BaseState<audit>* EndArray(Context<audit>& ctx, rapidjson::SizeType) override
{
// return to shared example
ctx.ex = (*ctx.examples)[0];
ctx.PushNamespace(" ", saved);
ctx.root_state = saved_root_state;
return &ctx.default_state;
}
};
// "...":[Numbers only]
template <bool audit>
class ArrayState : public BaseState<audit>
{
feature_index array_hash;
public:
ArrayState() : BaseState<audit>("Array") {}
BaseState<audit>* StartArray(Context<audit>& ctx) override
{
if (ctx.previous_state == this)
{
ctx.error() << "Nested arrays are not supported";
return nullptr;
}
ctx.PushNamespace(ctx.key, ctx.previous_state);
array_hash = ctx.CurrentNamespace().namespace_hash;
return this;
}
BaseState<audit>* Float(Context<audit>& ctx, float f) override
{
if (audit)
{
std::stringstream str;
str << '[' << (array_hash - ctx.CurrentNamespace().namespace_hash) << ']';
ctx.CurrentNamespace().AddFeature(f, array_hash, str.str().c_str());
}
else
ctx.CurrentNamespace().AddFeature(f, array_hash, nullptr);
array_hash++;
return this;
}
BaseState<audit>* Uint(Context<audit>& ctx, unsigned f) override { return Float(ctx, static_cast<float>(f)); }
BaseState<audit>* Null(Context<audit>& /* ctx */) override
{
// ignore null values and stay in current state
return this;
}
BaseState<audit>* StartObject(Context<audit>& ctx) override
{
// parse properties
ctx.PushNamespace(ctx.namespace_path.size() > 0 ? ctx.CurrentNamespace().name : " ", this);
return &ctx.default_state;
}
BaseState<audit>* EndArray(Context<audit>& ctx, rapidjson::SizeType /* elementCount */) override
{
return ctx.PopNamespace();
}
};
// only 0 is valid as DefaultState::Ignore injected that into the source stream
template <bool audit>
struct IgnoreState : BaseState<audit>
{
IgnoreState() : BaseState<audit>("Ignore") {}
BaseState<audit>* Uint(Context<audit>& ctx, unsigned) override { return ctx.previous_state; }
};
template <bool audit>
class DefaultState : public BaseState<audit>
{
public:
DefaultState() : BaseState<audit>("Default") {}
BaseState<audit>* Ignore(Context<audit>& ctx, rapidjson::SizeType length)
{
// fast ignore
// skip key + \0 + "
char* head = ctx.stream->src_ + length + 2;
if (head >= ctx.stream_end || *head != ':')
{
ctx.error() << "Expected ':' found '" << *head << "'";
return nullptr;
}
head++;
// scan for ,}
// support { { ... } }
int depth = 0, sq_depth = 0;
bool stop = false;
while (!stop)
{
switch (*head)
{
case '\0':
ctx.error() << "Found EOF";
return nullptr;
case '"':
{
// skip strings
bool stopInner = false;
while (!stopInner)
{
head++;
switch (*head)
{
case '\0':
ctx.error() << "Found EOF";
return nullptr;
case '\\':
head++;
break;
case '"':
stopInner = true;
break;
}
}
break;
}
case '{':
depth++;
break;
case '}':
if (depth == 0 && sq_depth == 0)
stop = true;
else
depth--;
break;
case '[':
sq_depth++;
break;
case ']':
if (depth == 0 && sq_depth == 0)
stop = true;
else
sq_depth--;
break;
case ',':
if (depth == 0 && sq_depth == 0) stop = true;
break;
}
head++;
}
// skip key + \0 + ":
char* value = ctx.stream->src_ + length + 3;
if (value >= ctx.stream_end)
{
ctx.error() << "Found EOF";
return nullptr;
}
*value = '0';
value++;
memset(value, ' ', head - value - 1);
return &ctx.ignore_state;
}
BaseState<audit>* Key(Context<audit>& ctx, const char* str, rapidjson::SizeType length, bool) override
{
ctx.key = str;
ctx.key_length = length;
if (length > 0 && str[0] == '_')
{
// match _label*
if (ctx.key_length >= 6 && !strncmp(ctx.key, "_label", 6))
{
if (ctx.key_length >= 7 && ctx.key[6] == '_')
{
if (length >= 9 && !strncmp(&ctx.key[7], "ca", 2)) { ctx.label_object_state.found_cb_continuous = true; }
return &ctx.label_single_property_state;
}
else if (ctx.key_length == 6)
return &ctx.label_state;
else if (ctx.key_length == 11 && !_stricmp(ctx.key, "_labelIndex"))
return &ctx.label_index_state;
else
{
ctx.error() << "Unsupported key '" << ctx.key << "' len: " << length;
return nullptr;
}
}
if (ctx.key_length == 5 && !strcmp(ctx.key, "_text")) return &ctx.text_state;
// TODO: _multi in _multi...
if (ctx.key_length == 6 && !strcmp(ctx.key, "_multi")) { return &ctx.multi_state; }
if (ctx.key_length == 6 && !strcmp(ctx.key, "_slots")) return &ctx.slots_state;
if (ctx.key_length == 4 && !_stricmp(ctx.key, "_tag")) return &ctx.tag_state;
if (ctx.key_length == 4 && !_stricmp(ctx.key, "_inc"))
{
ctx.array_uint_state.output_array = &ctx.label_object_state.inc;
ctx.array_uint_state.return_state = this;
return &ctx.array_uint_state;
}
if (ctx.key_length == 2 && ctx.key[1] == 'a')
{
ctx.array_uint_state.output_array = &ctx.label_object_state.actions;
ctx.array_uint_state.return_state = this;
return &ctx.array_uint_state;
}
if (ctx.key_length == 2 && ctx.key[1] == 'p')
{
// Ignore "_p" when it is inside the "c" key in decision service state.
if (ctx.root_state == &ctx.decision_service_state) { Ignore(ctx, length); }
ctx.array_float_state.output_array = &ctx.label_object_state.probs;
ctx.array_float_state.return_state = this;
return &ctx.array_float_state;
}
else if (length == 8 && !strncmp(str, "_slot_id", 8))
{
if (ctx.all->example_parser->lbl_parser.label_type != label_type_t::slates)
{ THROW("Can only use _slot_id with slates examples"); } ctx.uint_state.output_uint = &ctx.ex->l.slates.slot_id;
ctx.array_float_state.return_state = this;
return &ctx.array_float_state;
}
else if (ctx.key_length == 20 && !strncmp(str, "_original_label_cost", 20))
{
if(!ctx.decision_service_data) {
THROW("_original_label_cost is only valid in DSJson");
}
ctx.original_label_cost_state.aggr_float = &ctx.decision_service_data->originalLabelCost;
ctx.original_label_cost_state.first_slot_float = &ctx.decision_service_data->originalLabelCostFirstSlot;
ctx.original_label_cost_state.return_state = this;
return &ctx.original_label_cost_state;
}
else if (ctx.key_length == 5 && !_stricmp(ctx.key, "__aid"))
{
ctx.uint_dedup_state.return_state = this;
return &ctx.uint_dedup_state;
}
return Ignore(ctx, length);
}
return this;
}
BaseState<audit>* String(Context<audit>& ctx, const char* str, rapidjson::SizeType length, bool) override
{
// string escape
const char* end = str + length;
for (char* p = const_cast<char*>(str); p != end; p++)
{
switch (*p)
{
case ' ':
case '\t':
case '|':
case ':':
*p = '_';
}
}
if (ctx.all->chain_hash_json) { ctx.CurrentNamespace().AddFeature(ctx.all, ctx.key, str); }
else
{
char* prepend = const_cast<char*>(str) - ctx.key_length;
memmove(prepend, ctx.key, ctx.key_length);
ctx.CurrentNamespace().AddFeature(ctx.all, prepend);
}
return this;
}
BaseState<audit>* Bool(Context<audit>& ctx, bool b) override
{
if (b) ctx.CurrentNamespace().AddFeature(ctx.all, ctx.key);
return this;
}
BaseState<audit>* StartObject(Context<audit>& ctx) override
{
ctx.PushNamespace(ctx.key, this);
return this;
}