HEBench
hebench_logreg_o.cpp
Go to the documentation of this file.
1 
2 // Copyright (C) 2021 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include <bitset>
6 #include <cassert>
7 #include <cstring>
8 #include <iomanip>
9 #include <iostream>
10 #include <sstream>
11 #include <stdexcept>
12 #include <utility>
13 
14 #include "hebench/modules/timer/include/timer.h"
15 
16 #include "hebench/api_bridge/api.h"
17 #include "include/hebench_engine.h"
18 
19 #include "../include/hebench_logreg_o.h"
20 
21 namespace hebench {
22 namespace TestHarness {
23 namespace LogisticRegression {
24 namespace Offline {
25 
26 //----------------------------
27 // class BenchmarkDescription
28 //----------------------------
29 
30 bool BenchmarkDescriptor::m_b_registered = // register the benchmark with the factory
31  hebench::TestHarness::BenchmarkFactory::registerSupportedBenchmark(std::make_shared<BenchmarkDescriptor>());
32 
34  const std::vector<hebench::APIBridge::WorkloadParam> &w_params) const
35 {
36  assert(m_b_registered);
37 
38  // return true if benchmark is supported
39 
40  bool retval =
43 
44  return retval;
45 }
46 
48  const Engine &engine,
49  const BenchmarkDescription::Backend &backend_desc,
50  const BenchmarkDescription::Configuration &config) const
51 {
52  // finish describing workload
53  assert(OpParameterCount == 3);
54  assert(DefaultBatchSize == 100);
55 
56  BenchmarkDescriptorCategory::completeWorkloadDescription(output, engine, backend_desc, config);
57 
58  assert(OpParameterCount == output.operation_params_count);
59 
60  std::stringstream ss;
61  std::uint64_t *batch_sizes = output.concrete_descriptor.cat_params.offline.data_count;
62  std::uint64_t vector_size = fetchVectorSize(config.w_params);
63  hebench::APIBridge::BenchmarkDescriptor bench_desc = backend_desc.descriptor;
64 
65  // weights and bias can only have 1 sample
66  bench_desc.cat_params.offline.data_count[DataLoader::Index_W] = 1;
67  bench_desc.cat_params.offline.data_count[DataLoader::Index_b] = 1;
68 
69  std::uint64_t sample_size_fallback =
70  config.fallback_default_sample_size > 0 ?
73  std::uint64_t result_batch_size =
76  config.default_sample_sizes,
77  bench_desc,
78  sample_size_fallback,
80  if (batch_sizes[DataLoader::Index_W] != 1)
81  {
82  ss = std::stringstream();
83  ss << "Batch size for feature vector 'W' must be 1, but " << batch_sizes[DataLoader::Index_W] << " received.";
84  throw std::invalid_argument(ss.str());
85  } // end if
86  if (batch_sizes[DataLoader::Index_b] != 1)
87  {
88  ss = std::stringstream();
89  ss << "Batch size for bias 'b' must be 1, but " << batch_sizes[DataLoader::Index_b] << " received.";
90  throw std::invalid_argument(ss.str());
91  } // end if
92 
93  ss = std::stringstream();
94 
95  // complete header with workload specifics
96  ss << ", , P(X = X') = sigmoid";
97  switch (bench_desc.workload)
98  {
100  ss << "_pd3";
101  break;
103  ss << "_pd5";
104  break;
106  ss << "_pd7";
107  break;
108  default:
109  // standard sigmoid
110  break;
111  } // end switch
112  ss << "(W . X + b)" << std::endl
113  << ", , , Elements, Batch size" << std::endl;
114  ss << ", , W, " << vector_size << ", " << batch_sizes[DataLoader::Index_W] << std::endl;
115  ss << ", , b, 1, " << batch_sizes[DataLoader::Index_b] << std::endl;
116  ss << ", , X, " << vector_size << ", " << batch_sizes[DataLoader::Index_X] << std::endl;
117  ss << ", , P(X), 1, " << result_batch_size << std::endl;
118 
119  output.workload_header = ss.str();
120 }
121 
123  const DescriptionToken &description_token)
124 {
125  assert(m_b_registered);
126  Benchmark *retval = nullptr;
127 
128  try
129  {
130  retval = new Benchmark(p_engine, description_token);
131  }
132  catch (...)
133  {
134  if (retval)
135  delete retval;
136  throw;
137  }
138 
139  return retval;
140 }
141 
143 {
144  assert(m_b_registered);
145  if (p_bench)
146  delete p_bench;
147 }
148 
149 //-----------------
150 // class Benchmark
151 //-----------------
152 
153 Benchmark::Benchmark(std::shared_ptr<Engine> p_engine,
154  const IBenchmarkDescriptor::DescriptionToken &description_token) :
155  BenchmarkOffline(p_engine, description_token)
156 {
157 }
158 
160 {
161  hebench::Common::EventTimer timer;
162  hebench::Common::TimingReportEvent::Ptr p_timing_event;
163  std::uint64_t vector_size = BenchmarkDescriptor::fetchVectorSize(this->getBenchmarkConfiguration().w_params);
164  std::uint64_t batch_sizes[BenchmarkDescriptor::OpParameterCount];
165  std::stringstream ss;
166 
168  bench_desc.cat_params.offline.data_count[DataLoader::Index_W] = 1;
169  bench_desc.cat_params.offline.data_count[DataLoader::Index_b] = 1;
170 
171  std::uint64_t sample_size_fallback =
177  this->getBenchmarkConfiguration().default_sample_sizes,
178  bench_desc,
179  sample_size_fallback,
181 
182  assert(batch_sizes[DataLoader::Index_W] == 1 && batch_sizes[DataLoader::Index_b] == 1);
183 
184  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log("Preparing workload.") << std::endl;
185 
187  switch (this->getBackendDescription().descriptor.workload)
188  {
191  break;
194  break;
197  break;
198  default:
200  break;
201  } // end switch
202 
203  timer.start();
204  if (this->getBenchmarkConfiguration().dataset_filename.empty())
205  {
206  // generates random values for input and generates (computes) ground truth
207  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log("Generating data...") << std::endl;
208  m_data = DataLoader::create(pd,
209  vector_size,
210  batch_sizes[DataLoader::Index_X],
211  this->getBackendDescription().descriptor.data_type);
212  } // end if
213  else
214  {
215  std::stringstream ss;
216  ss << "Loading data from external dataset: " << std::endl
217  << "\"" << this->getBenchmarkConfiguration().dataset_filename << "\"";
218  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log(ss.str()) << std::endl;
219  // load values for input and ground truth from file
220  m_data = DataLoader::create(pd,
221  vector_size,
222  batch_sizes[DataLoader::Index_X],
223  this->getBackendDescription().descriptor.data_type,
224  this->getBenchmarkConfiguration().dataset_filename);
225  } // end else
226  p_timing_event = timer.stop<std::milli>();
227 
228  ss = std::stringstream();
229  ss << "Total data loaded: " << m_data->getTotalDataLoaded() << " bytes";
230  std::cout << IOS_MSG_DONE << hebench::Logging::GlobalLogger::log(ss.str()) << std::endl;
231  ss = std::stringstream();
232  ss << "Elapsed wall time: " << p_timing_event->elapsedWallTime<std::milli>() << " ms";
233  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log(ss.str()) << std::endl;
234  ss = std::stringstream();
235  ss << "Elapsed CPU time: " << p_timing_event->elapsedCPUTime<std::milli>() << " ms";
236  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log(ss.str()) << std::endl;
237 }
238 
240  const std::uint64_t *param_data_pack_indices,
241  const std::vector<hebench::APIBridge::NativeDataBuffer *> &outputs,
242  hebench::APIBridge::DataType data_type) const
243 {
244  assert(dataset->getParameterCount() == BenchmarkDescriptorCategory::OpParameterCount
245  && dataset->getResultCount() == BenchmarkDescriptorCategory::OpResultCount);
246 
247  return BenchmarkOffline::validateResult(dataset, param_data_pack_indices, outputs, data_type);
248 }
249 
250 } // namespace Offline
251 } // namespace LogisticRegression
252 } // namespace TestHarness
253 } // namespace hebench
const hebench::APIBridge::BenchmarkDescriptor & descriptor
Benchmark backend descriptor, as retrieved by backend, corresponding to the registration handle h_des...
std::uint64_t fallback_default_sample_size
Default sample size to be used if a specific size is not specified in the default_sample_sizes collec...
std::vector< std::uint64_t > default_sample_sizes
Default sample size for each operation parameter.
std::vector< hebench::APIBridge::WorkloadParam > w_params
Set of arguments for workload parameters.
std::string dataset_filename
File containing data for the benchmark. If empty string, benchmarks that can auto generate the datase...
static bool registerSupportedBenchmark(std::shared_ptr< IBenchmarkDescriptor > p_desc_obj)
Registers a benchmark description object that represents one of the supported workloads.
Base class for workload benchmarks in the offline category.
Token returned by a successful call to IBenchmarkDescriptor::matchBenchmarkDescriptor().
std::shared_ptr< IDataLoader > Ptr
bool matchBenchmarkDescriptor(const hebench::APIBridge::BenchmarkDescriptor &bench_desc, const std::vector< hebench::APIBridge::WorkloadParam > &w_params) const override
Determines if the represented benchmark can perform the workload described by a specified HEBench ben...
void completeWorkloadDescription(WorkloadDescriptionOutput &output, const Engine &engine, const BenchmarkDescription::Backend &backend_desc, const BenchmarkDescription::Configuration &config) const override
Completes the description for the matched benchmark.
static std::uint64_t fetchVectorSize(const std::vector< hebench::APIBridge::WorkloadParam > &w_params)
static DataLoader::Ptr create(PolynomialDegree polynomial_degree, std::uint64_t vector_size, std::uint64_t batch_size_input, hebench::APIBridge::DataType data_type)
void destroyBenchmark(hebench::TestHarness::PartialBenchmark *p_bench) override
Destroys an object returned by createBenchmark().
bool matchBenchmarkDescriptor(const hebench::APIBridge::BenchmarkDescriptor &bench_desc, const std::vector< hebench::APIBridge::WorkloadParam > &w_params) const override
Determines if the represented benchmark can perform the workload described by a specified HEBench ben...
void completeWorkloadDescription(WorkloadDescriptionOutput &output, const Engine &engine, const BenchmarkDescription::Backend &backend_desc, const BenchmarkDescription::Configuration &config) const override
Completes the description for the matched benchmark.
hebench::TestHarness::PartialBenchmark * createBenchmark(std::shared_ptr< Engine > p_engine, const DescriptionToken &description_token) override
Creates the represented IBenchmark object that can perform the workload specified by the HEBench benc...
void init() override
Initializes the partial benchmark members.
bool validateResult(IDataLoader::Ptr dataset, const std::uint64_t *param_data_pack_indices, const std::vector< hebench::APIBridge::NativeDataBuffer * > &p_outputs, hebench::APIBridge::DataType data_type) const override
Validates the result of an operation against the ground truth.
virtual bool validateResult(IDataLoader::Ptr dataset, const std::uint64_t *param_data_pack_indices, const std::vector< hebench::APIBridge::NativeDataBuffer * > &outputs, hebench::APIBridge::DataType data_type) const
Validates the result of an operation against the ground truth.
std::string workload_header
Workload specific information to be added to the report header.
static std::uint64_t computeSampleSizes(std::uint64_t *sample_sizes, std::size_t param_count, const std::vector< std::uint64_t > &config_sample_sizes, const hebench::APIBridge::BenchmarkDescriptor &bench_desc, std::uint64_t default_sample_size_fallback, bool force_config)
Extracts the batch sizes for a workload from a specified HEBench API benchmark descriptor.
static bool getForceConfigValues()
Specifies whether frontend will override backend descriptors using configuration data or not.
std::size_t operation_params_count
Number of parameters for the represented workload operation.
hebench::APIBridge::BenchmarkDescriptor concrete_descriptor
Benchmark descriptor completed with concrete values assigned to configurable fields.
Bundles values that need to be filled by a workload during completeWorkloadDescription().
const BenchmarkDescription::Backend & getBackendDescription() const
Allows read-only access to this benchmark backend description.
const BenchmarkDescription::Configuration & getBenchmarkConfiguration() const
Allows read-only access to this benchmark configuration.
#define IOS_MSG_DONE
#define IOS_MSG_INFO
DataType
Defines data types for a workload.
Definition: types.h:379
CategoryParams cat_params
Parameters for the category.
Definition: types.h:532
Category category
Category for the benchmark.
Definition: types.h:531
Workload workload
Workload for the benchmark.
Definition: types.h:529
@ LogisticRegression_PolyD3
Definition: types.h:203
@ LogisticRegression_PolyD7
Definition: types.h:247
@ LogisticRegression_PolyD5
Definition: types.h:225
Defines a benchmark test.
Definition: types.h:527