Vowpal Wabbit
Public Member Functions | Private Attributes | List of all members
vw_slim::model_parser Class Reference

#include <model_parser.h>

Public Member Functions

 model_parser (const char *model, size_t length)
 
int read (const char *field_name, size_t field_length, const char **ret)
 
int skip (size_t bytes)
 
const char * position ()
 
uint32_t checksum ()
 
template<bool compute_checksum>
int read_string (const char *field_name, std::string &s)
 
template<typename T , bool compute_checksum>
int read (const char *field_name, T &val)
 
template<typename T >
int read (const char *field_name, T &val)
 
template<typename T , typename W >
int read_weights (std::unique_ptr< W > &weights, uint64_t weight_length)
 
template<typename W >
int read_weights (std::unique_ptr< W > &weights, uint32_t num_bits, uint32_t stride_shift)
 

Private Attributes

const char * _model_begin
 
const char * _model
 
const char * _model_end
 
uint32_t _checksum
 

Detailed Description

Definition at line 20 of file model_parser.h.

Constructor & Destructor Documentation

◆ model_parser()

vw_slim::model_parser::model_parser ( const char *  model,
size_t  length 
)

Definition at line 5 of file model_parser.cc.

References _model_begin, and _model_end.

6  : _model_begin(model), _model(model), _model_end(model + length), _checksum(0)
7 {
8 #ifdef MODEL_PARSER_DEBUG
9  std::cout << "moder_parser("
10  << "0x" << std::hex << std::setw(8) << (uint64_t)_model_begin << "-" << std::hex << std::setw(8)
11  << (uint64_t)_model_end << ")" << std::dec;
12 #endif
13 }
const char * _model
Definition: model_parser.h:23
const char * _model_begin
Definition: model_parser.h:22
const char * _model_end
Definition: model_parser.h:24

Member Function Documentation

◆ checksum()

uint32_t vw_slim::model_parser::checksum ( )

Definition at line 17 of file model_parser.cc.

References _checksum.

Referenced by vw_slim::vw_predict< W >::load().

17 { return _checksum; }

◆ position()

const char * vw_slim::model_parser::position ( )

Definition at line 15 of file model_parser.cc.

References _model.

15 { return _model; }
const char * _model
Definition: model_parser.h:23

◆ read() [1/3]

int vw_slim::model_parser::read ( const char *  field_name,
size_t  field_length,
const char **  ret 
)

Definition at line 19 of file model_parser.cc.

References _model, _model_end, E_VW_PREDICT_ERR_INVALID_MODEL, and S_VW_PREDICT_OK.

Referenced by vw_slim::vw_predict< W >::load(), read(), and read_string().

20 { // check if we're inside the buffer
21  const char* new_model = _model + field_length;
22  if (new_model > _model_end)
24 
25 #ifdef MODEL_PARSER_DEBUG
26  std::fstream log("vwslim-debug.log", std::fstream::app);
27  log << "reading " << field_name << std::endl;
28 #endif
29 
30  *ret = _model;
31  // advance begin
32  _model = new_model;
33 
34  return S_VW_PREDICT_OK;
35 }
#define S_VW_PREDICT_OK
const char * _model
Definition: model_parser.h:23
#define E_VW_PREDICT_ERR_INVALID_MODEL
const char * _model_end
Definition: model_parser.h:24

◆ read() [2/3]

template<typename T , bool compute_checksum>
int vw_slim::model_parser::read ( const char *  field_name,
T &  val 
)
inline

Definition at line 73 of file model_parser.h.

References _checksum, _model_begin, read(), RETURN_ON_FAIL, S_VW_PREDICT_OK, and uniform_hash().

74  {
75 #ifdef MODEL_PARSER_DEBUG
76  std::fstream log("vwslim-debug.log", std::fstream::app);
77  log << std::setw(18) << field_name << " 0x" << std::hex << std::setw(8) << (uint64_t)_model << "-" << std::hex
78  << std::setw(8) << (uint64_t)_model_end << " " << std::setw(4) << (_model - _model_begin)
79  << " field: " << std::setw(8) << (uint64_t)&val << std::dec;
80 #endif
81 
82  const char* data;
83  RETURN_ON_FAIL(read(field_name, sizeof(T), &data));
84 
85  // avoid alignment issues for 32/64bit types on e.g. Android/ARM
86  memcpy(&val, data, sizeof(T));
87 
88  if (compute_checksum)
89  _checksum = (uint32_t)uniform_hash(&val, sizeof(T), _checksum);
90 
91 #ifdef MODEL_PARSER_DEBUG
92  log << " '" << val << '\'' << std::endl;
93 #endif
94 
95  return S_VW_PREDICT_OK;
96  }
#define RETURN_ON_FAIL(stmt)
#define S_VW_PREDICT_OK
const char * _model
Definition: model_parser.h:23
VW_STD14_CONSTEXPR uint64_t uniform_hash(const void *key, size_t len, uint64_t seed)
Definition: hash.h:67
const char * _model_begin
Definition: model_parser.h:22
int read(const char *field_name, size_t field_length, const char **ret)
Definition: model_parser.cc:19
const char * _model_end
Definition: model_parser.h:24

◆ read() [3/3]

template<typename T >
int vw_slim::model_parser::read ( const char *  field_name,
T &  val 
)
inline

Definition at line 100 of file model_parser.h.

101  {
102  return read<T, true>(field_name, val);
103  }

◆ read_string()

template<bool compute_checksum>
int vw_slim::model_parser::read_string ( const char *  field_name,
std::string &  s 
)
inline

Definition at line 39 of file model_parser.h.

References E_VW_PREDICT_ERR_INVALID_MODEL, read(), RETURN_ON_FAIL, S_VW_PREDICT_OK, and uniform_hash().

Referenced by vw_slim::vw_predict< W >::load().

40  {
41  uint32_t str_len;
42  RETURN_ON_FAIL((read<uint32_t, compute_checksum>("string.len", str_len)));
43 #ifdef MODEL_PARSER_DEBUG
44  {
45  std::fstream log("vwslim-debug.log", std::fstream::app);
46  log << std::setw(18) << field_name << " length " << str_len << std::endl;
47  }
48 #endif
49 
50  // 0 length strings are not valid, need to contain at least \0
51  if (str_len == 0)
53 
54  const char* data;
55  RETURN_ON_FAIL(read(field_name, str_len, &data));
56 
57  s = std::string(data, str_len - 1);
58 #ifdef MODEL_PARSER_DEBUG
59  {
60  std::fstream log("vwslim-debug.log", std::fstream::app);
61  log << std::setw(18) << field_name << " '" << s << '\'' << std::endl;
62  }
63 #endif
64 
65  // calculate checksum
66  if (compute_checksum && str_len > 0)
67  _checksum = (uint32_t)uniform_hash(data, str_len, _checksum);
68 
69  return S_VW_PREDICT_OK;
70  }
#define RETURN_ON_FAIL(stmt)
#define S_VW_PREDICT_OK
VW_STD14_CONSTEXPR uint64_t uniform_hash(const void *key, size_t len, uint64_t seed)
Definition: hash.h:67
int read(const char *field_name, size_t field_length, const char **ret)
Definition: model_parser.cc:19
#define E_VW_PREDICT_ERR_INVALID_MODEL

◆ read_weights() [1/2]

template<typename T , typename W >
int vw_slim::model_parser::read_weights ( std::unique_ptr< W > &  weights,
uint64_t  weight_length 
)
inline

Definition at line 106 of file model_parser.h.

References E_VW_PREDICT_ERR_WEIGHT_INDEX_OUT_OF_RANGE, RETURN_ON_FAIL, and S_VW_PREDICT_OK.

Referenced by vw_slim::vw_predict< W >::load().

107  {
108  // weights are excluded from checksum calculation
109  while (_model < _model_end)
110  {
111  T idx;
112  RETURN_ON_FAIL((read<T, false>("gd.weight.index", idx)));
113  if (idx > weight_length)
115 
116  float& w = (*weights)[idx];
117  RETURN_ON_FAIL((read<float, false>("gd.weight.value", w)));
118 
119 #ifdef MODEL_PARSER_DEBUG
120  std::cout << "weight. idx: " << idx << ":" << (*weights)[idx] << std::endl;
121 #endif
122  }
123 
124  return S_VW_PREDICT_OK;
125  }
#define RETURN_ON_FAIL(stmt)
#define S_VW_PREDICT_OK
const char * _model
Definition: model_parser.h:23
const char * _model_end
Definition: model_parser.h:24
#define E_VW_PREDICT_ERR_WEIGHT_INDEX_OUT_OF_RANGE

◆ read_weights() [2/2]

template<typename W >
int vw_slim::model_parser::read_weights ( std::unique_ptr< W > &  weights,
uint32_t  num_bits,
uint32_t  stride_shift 
)
inline

Definition at line 128 of file model_parser.h.

References RETURN_ON_FAIL, and S_VW_PREDICT_OK.

129  {
130  uint64_t weight_length = (uint64_t)1 << num_bits;
131 
132  weights = std::unique_ptr<W>(new W(weight_length));
133  weights->stride_shift(stride_shift);
134 
135  if (num_bits < 31)
136  {
137  RETURN_ON_FAIL((read_weights<uint32_t, W>(weights, weight_length)));
138  }
139  else
140  {
141  RETURN_ON_FAIL((read_weights<uint64_t, W>(weights, weight_length)));
142  }
143 
144  return S_VW_PREDICT_OK;
145  }
#define RETURN_ON_FAIL(stmt)
#define S_VW_PREDICT_OK
uint64_t stride_shift(const stagewise_poly &poly, uint64_t idx)

◆ skip()

int vw_slim::model_parser::skip ( size_t  bytes)

Definition at line 37 of file model_parser.cc.

References _checksum, _model, _model_end, E_VW_PREDICT_ERR_INVALID_MODEL, S_VW_PREDICT_OK, and uniform_hash().

Referenced by vw_slim::vw_predict< W >::load().

38 {
39  const char* new_model = _model + bytes;
40  if (new_model > _model_end)
42 
43  if (bytes > 0)
44  _checksum = (uint32_t)uniform_hash(_model, bytes, _checksum);
45 
46  _model = new_model;
47 
48  return S_VW_PREDICT_OK;
49 }
#define S_VW_PREDICT_OK
const char * _model
Definition: model_parser.h:23
VW_STD14_CONSTEXPR uint64_t uniform_hash(const void *key, size_t len, uint64_t seed)
Definition: hash.h:67
#define E_VW_PREDICT_ERR_INVALID_MODEL
const char * _model_end
Definition: model_parser.h:24

Member Data Documentation

◆ _checksum

uint32_t vw_slim::model_parser::_checksum
private

Definition at line 25 of file model_parser.h.

Referenced by checksum(), read(), and skip().

◆ _model

const char* vw_slim::model_parser::_model
private

Definition at line 23 of file model_parser.h.

Referenced by position(), read(), and skip().

◆ _model_begin

const char* vw_slim::model_parser::_model_begin
private

Definition at line 22 of file model_parser.h.

Referenced by model_parser(), and read().

◆ _model_end

const char* vw_slim::model_parser::_model_end
private

Definition at line 24 of file model_parser.h.

Referenced by model_parser(), read(), and skip().


The documentation for this class was generated from the following files: