OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TestTorchTableFunctions.cpp File Reference
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include "TestTorchTableFunctions.h"
#include "torch/script.h"
#include "torch/torch.h"
+ Include dependency graph for TestTorchTableFunctions.cpp:

Go to the source code of this file.

Functions

EXTENSION_NOINLINE int32_t tf_test_runtime_torch (TableFunctionManager &mgr, Column< int64_t > &input, Column< int64_t > &output)
 
template<typename T >
TEMPLATE_NOINLINE int32_t tf_test_runtime_torch_template__template (TableFunctionManager &mgr, const Column< T > &input, Column< T > &output)
 
template TEMPLATE_NOINLINE int32_t tf_test_runtime_torch_template__template (TableFunctionManager &mgr, const Column< int64_t > &input, Column< int64_t > &output)
 
template TEMPLATE_NOINLINE int32_t tf_test_runtime_torch_template__template (TableFunctionManager &mgr, const Column< double > &input, Column< double > &output)
 
EXTENSION_NOINLINE int32_t tf_test_torch_generate_random_column (TableFunctionManager &mgr, int32_t num_elements, Column< double > &output)
 
torch::Tensor make_features_from_columns (const ColumnList< double > &cols, int32_t batch_size)
 
torch::Tensor f (torch::Tensor x, torch::Tensor W_target, torch::Tensor b_target)
 
std::string poly_desc (torch::Tensor W, torch::Tensor b)
 
std::pair< torch::Tensor,
torch::Tensor > 
get_batch (const ColumnList< double > &cols, torch::Tensor W_target, torch::Tensor b_target, int32_t batch_size)
 
EXTENSION_NOINLINE int32_t tf_test_torch_regression (TableFunctionManager &mgr, const ColumnList< double > &features, int32_t batch_size, bool use_gpu, bool save_model, const TextEncodingNone &model_filename, Column< double > &output)
 
EXTENSION_NOINLINE int32_t tf_test_torch_load_model (TableFunctionManager &mgr, const TextEncodingNone &model_filename, Column< bool > &output)
 

Variables

torch::Device _test_torch_tfs_device = torch::kCPU
 

Function Documentation

torch::Tensor f ( torch::Tensor  x,
torch::Tensor  W_target,
torch::Tensor  b_target 
)

Definition at line 103 of file TestTorchTableFunctions.cpp.

Referenced by _geoToHex2d(), QueryFragmentDescriptor::assignFragsToKernelDispatch(), QueryFragmentDescriptor::assignFragsToMultiDispatch(), threading_tbb::async(), atomicSumFltSkipVal(), TransformUTMTo4326::calculateY(), ResultSetReductionJIT::codegen(), GpuReductionHelperJIT::codegen(), FixedWidthInt::codegenDecode(), FixedWidthUnsigned::codegenDecode(), DiffFixedWidthInt::codegenDecode(), FixedWidthReal::codegenDecode(), FixedWidthSmallDate::codegenDecode(), org.apache.calcite.sql2rel.SqlToRelConverter::collectInsertTargets(), com.mapd.calcite.parser.HeavyDBParser::convertSqlToRelNode(), ArrowResultSetConverter::convertToArrow(), org.apache.calcite.sql2rel.SqlToRelConverter::convertWhere(), File_Namespace::create(), File_Namespace::FileMgr::createFileInfo(), com.mapd.tests.DateTimeTest.DateAddUnit::DateAddUnit(), com.mapd.tests.DateTimeTest.DateExtractUnit::DateExtractUnit(), Catalog_Namespace::SysCatalog::execInTransaction(), Catalog_Namespace::Catalog::execInTransaction(), Analyzer::Expr::find_expr(), Analyzer::UOper::find_expr(), Analyzer::BinOper::find_expr(), Analyzer::InValues::find_expr(), Analyzer::MLPredictExpr::find_expr(), Analyzer::PCAProjectExpr::find_expr(), Analyzer::CharLengthExpr::find_expr(), Analyzer::KeyForStringExpr::find_expr(), Analyzer::SampleRatioExpr::find_expr(), Analyzer::CardinalityExpr::find_expr(), Analyzer::LikeExpr::find_expr(), Analyzer::RegexpExpr::find_expr(), Analyzer::WidthBucketExpr::find_expr(), Analyzer::LikelihoodExpr::find_expr(), Analyzer::AggExpr::find_expr(), Analyzer::CaseExpr::find_expr(), Analyzer::ExtractExpr::find_expr(), Analyzer::DateaddExpr::find_expr(), Analyzer::DatediffExpr::find_expr(), Analyzer::DatetruncExpr::find_expr(), Analyzer::StringOper::find_expr(), Analyzer::FunctionOper::find_expr(), RasterFormat_Namespace::format_raster_data(), get_batch(), org.apache.calcite.sql2rel.SqlToRelConverter::getInitializerFactory(), import_export::Importer::importGDALRaster(), FilterSelectivity::isFilterSelectiveEnough(), CodeGenerator::link_udf_module(), org.apache.calcite.sql2rel.SqlToRelConverter.Blackboard::lookupExp(), com.mapd.tests.DateTimeTest::main(), AutomaticIRMetadataGuard::makeQueryEngineFilename(), File_Namespace::open(), File_Namespace::FileMgr::openExistingFile(), GenericKeyHandler::operator()(), BoundingBoxIntersectKeyHandler::operator()(), RangeKeyHandler::operator()(), heavyai::JSON::operator[](), threading_serial::parallel_for(), threading_std::parallel_for(), ArrowForeignStorageBase::parseArrowTable(), org.apache.calcite.sql.validate.SqlValidatorImpl.Permute::Permute(), Data_Namespace::ProcMeminfoParser::ProcMeminfoParser(), Parser::QuerySpec::QuerySpec(), File_Namespace::FileBuffer::readMetadata(), reg_hex_horiz_pixel_bin_packed(), reg_hex_horiz_pixel_bin_x(), reg_hex_horiz_pixel_bin_y(), reg_hex_vert_pixel_bin_packed(), reg_hex_vert_pixel_bin_x(), reg_hex_vert_pixel_bin_y(), threading_serial::task_group::run(), threading_std::task_group::run(), anonymous_namespace{NativeCodegen.cpp}::show_defined(), com.mapd.tests.DateTimeTest::testAdd(), com.mapd.tests.DateTimeTest::testDateAdd(), com.mapd.tests.DateTimeTest::testDateExtract(), com.mapd.tests.DateTimeTest::testDateTrunc(), com.mapd.tests.DateTimeTest::testDiff(), com.mapd.tests.DateTimeTest::testSub(), tf_metadata_getter__cpu_template(), tf_metadata_getter_bad__cpu_template(), tf_metadata_setter__cpu_template(), GDALTableFunctions::tf_raster_contour_rasterize_impl(), tf_test_torch_regression(), org.apache.calcite.sql2rel.SqlToRelConverter::toRel(), anonymous_namespace{ResultSetReductionCodegen.cpp}::translate_body(), anonymous_namespace{ResultSetReductionCodegen.cpp}::translate_for(), translate_function(), org.apache.calcite.sql.validate.SqlValidatorImpl::validateInsert(), anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::visitBinOper(), and File_Namespace::FileBuffer::writeMetadata().

103  {
104  return x.mm(W_target) + b_target.item();
105 }
std::pair<torch::Tensor, torch::Tensor> get_batch ( const ColumnList< double > &  cols,
torch::Tensor  W_target,
torch::Tensor  b_target,
int32_t  batch_size 
)

Definition at line 126 of file TestTorchTableFunctions.cpp.

References f(), and make_features_from_columns().

Referenced by tf_test_torch_regression().

129  {
130  auto x = make_features_from_columns(cols, batch_size);
131  auto y = f(x, W_target, b_target);
132  return std::make_pair(x, y);
133 }
torch::Tensor make_features_from_columns(const ColumnList< double > &cols, int32_t batch_size)
torch::Tensor f(torch::Tensor x, torch::Tensor W_target, torch::Tensor b_target)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

torch::Tensor make_features_from_columns ( const ColumnList< double > &  cols,
int32_t  batch_size 
)

Definition at line 85 of file TestTorchTableFunctions.cpp.

References _test_torch_tfs_device, ColumnList< T >::numCols(), and ColumnList< T >::size().

Referenced by get_batch().

86  {
87  int32_t poly_degree = cols.numCols();
88  torch::Tensor output = torch::empty({batch_size, poly_degree}, {torch::kCPU});
89 
90  // build a tensor of (batch_size, poly_degree) dimensions, where each row is sampled
91  // randomly from the input columns formated as (x, x^2 ..., x^poly_degree)
92  for (int i = 0; i < batch_size; i++) {
93  int32_t idx = rand() % cols.size();
94  for (int j = 0; j < poly_degree; j++) {
95  output[i][j] = cols[j][idx];
96  }
97  }
98 
99  return output.to(_test_torch_tfs_device);
100 }
DEVICE int64_t numCols() const
DEVICE int64_t size() const
torch::Device _test_torch_tfs_device

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string poly_desc ( torch::Tensor  W,
torch::Tensor  b 
)

Definition at line 108 of file TestTorchTableFunctions.cpp.

Referenced by tf_test_torch_regression().

108  {
109  auto size = W.size(0);
110  std::ostringstream stream;
111 
112  if (W.scalar_type() != c10::ScalarType::Float ||
113  b.scalar_type() != c10::ScalarType::Float) {
114  throw std::runtime_error(
115  "Attempted to print polynomial with non-float coefficients!");
116  }
117 
118  stream << "y = ";
119  for (int64_t i = 0; i < size; ++i)
120  stream << W[i].item<float>() << " x^" << size - i << " ";
121  stream << "+ " << b[0].item<float>();
122  return stream.str();
123 }

+ Here is the caller graph for this function:

EXTENSION_NOINLINE int32_t tf_test_runtime_torch ( TableFunctionManager mgr,
Column< int64_t > &  input,
Column< int64_t > &  output 
)

Definition at line 43 of file TestTorchTableFunctions.cpp.

45  {
46  return 0;
47 }
template<typename T >
TEMPLATE_NOINLINE int32_t tf_test_runtime_torch_template__template ( TableFunctionManager mgr,
const Column< T > &  input,
Column< T > &  output 
)

Definition at line 51 of file TestTorchTableFunctions.cpp.

53  {
54  return 0;
55 }
template TEMPLATE_NOINLINE int32_t tf_test_runtime_torch_template__template ( TableFunctionManager mgr,
const Column< int64_t > &  input,
Column< int64_t > &  output 
)
template TEMPLATE_NOINLINE int32_t tf_test_runtime_torch_template__template ( TableFunctionManager mgr,
const Column< double > &  input,
Column< double > &  output 
)
EXTENSION_NOINLINE int32_t tf_test_torch_generate_random_column ( TableFunctionManager mgr,
int32_t  num_elements,
Column< double > &  output 
)

Definition at line 70 of file TestTorchTableFunctions.cpp.

References TableFunctionManager::set_output_row_size().

72  {
73  mgr.set_output_row_size(num_elements);
74  torch::Tensor random = torch::randn({num_elements}, at::dtype(at::kDouble));
75  random = random.unsqueeze(1);
76  double* data_ptr = (double*)random.data_ptr();
77 
78  for (int32_t i = 0; i < num_elements; ++i) {
79  output[i] = *data_ptr++;
80  }
81 
82  return num_elements;
83 }
void set_output_row_size(int64_t num_rows)
Definition: heavydbTypes.h:373

+ Here is the call graph for this function:

EXTENSION_NOINLINE int32_t tf_test_torch_load_model ( TableFunctionManager mgr,
const TextEncodingNone model_filename,
Column< bool > &  output 
)

Definition at line 226 of file TestTorchTableFunctions.cpp.

References TextEncodingNone::getString(), boost::serialization::load(), and TableFunctionManager::set_output_row_size().

228  {
229  mgr.set_output_row_size(1);
230  torch::jit::script::Module module;
231  try {
232  module = torch::jit::load(model_filename.getString());
233  } catch (const std::exception& e) {
234  return mgr.ERROR_MESSAGE("Error loading torchscript model: " + e.what());
235  }
236 
237  output[0] = true;
238  return 1;
239 }
void set_output_row_size(int64_t num_rows)
Definition: heavydbTypes.h:373
void load(Archive &ar, ExplainedQueryHint &query_hint, const unsigned int version)
std::string getString() const
Definition: heavydbTypes.h:641

+ Here is the call graph for this function:

EXTENSION_NOINLINE int32_t tf_test_torch_regression ( TableFunctionManager mgr,
const ColumnList< double > &  features,
int32_t  batch_size,
bool  use_gpu,
bool  save_model,
const TextEncodingNone model_filename,
Column< double > &  output 
)

Definition at line 146 of file TestTorchTableFunctions.cpp.

References _test_torch_tfs_device, f(), get_batch(), TextEncodingNone::getString(), ColumnList< T >::numCols(), poly_desc(), boost::serialization::save(), and TableFunctionManager::set_output_row_size().

152  {
153  int32_t poly_degree = features.numCols();
154  // we output target and trained coefficients + bias
155  int32_t output_size = (poly_degree + 1) * 2;
156  mgr.set_output_row_size(output_size);
157  std::srand(std::time(nullptr)); // not ideal RNG, but fine for test purpooses
158 #ifdef HAVE_CUDA_TORCH
159  if (torch::cuda::is_available() && use_gpu) {
160  _test_torch_tfs_device = torch::kCUDA;
161  }
162 #endif
163 
164  auto W_target = torch::randn({poly_degree, 1}, at::device(_test_torch_tfs_device)) * 5;
165  auto b_target = torch::randn({1}, at::device(_test_torch_tfs_device)) * 5;
166 
167  // Define the model and optimizer
168  auto fc = torch::nn::Linear(W_target.size(0), 1);
169  fc->to(_test_torch_tfs_device);
170  torch::optim::SGD optim(fc->parameters(), .1);
171 
172  float loss = 0;
173  int64_t batch_idx = 0;
174 
175  while (++batch_idx) {
176  // Get data
177  torch::Tensor batch_x, batch_y;
178  std::tie(batch_x, batch_y) = get_batch(features, W_target, b_target, batch_size);
179 
180  // Reset gradients
181  optim.zero_grad();
182 
183  // Forward pass
184  auto output = torch::smooth_l1_loss(fc(batch_x), batch_y);
185  loss = output.item<float>();
186 
187  // Backward pass
188  output.backward();
189 
190  // Apply gradients
191  optim.step();
192 
193  // Stop criterion
194  if (loss < 1e-3f)
195  break;
196  }
197 
198  if (save_model) {
199  torch::save(fc, model_filename.getString());
200  }
201 
202  // output column with target + trained coefficients ordered by degree, then bias
203  torch::Tensor output_coefficients = fc->weight.view({-1}).cpu();
204  torch::Tensor goal_coefficients = W_target.view({-1}).cpu();
205  int32_t out_column_idx, input_idx;
206  for (out_column_idx = 0, input_idx = 0; input_idx < output_coefficients.size(0);
207  ++input_idx) {
208  output[out_column_idx++] = output_coefficients[input_idx].item<float>();
209  output[out_column_idx++] = goal_coefficients[input_idx].item<float>();
210  }
211  output[out_column_idx++] = fc->bias[0].item<float>();
212  output[out_column_idx] = b_target[0].item<float>();
213 
214  std::fprintf(stdout, "Loss: %lf after %ld batches\n", loss, batch_idx);
215  std::fprintf(stdout,
216  "==> Learned function:\t%s\n",
217  poly_desc(output_coefficients, fc->bias).c_str());
218  std::fprintf(stdout,
219  "==> Actual function:\t%s\n",
220  poly_desc(W_target.view({-1}).cpu(), b_target).c_str());
221 
222  return output_size;
223 }
void set_output_row_size(int64_t num_rows)
Definition: heavydbTypes.h:373
std::string getString() const
Definition: heavydbTypes.h:641
DEVICE int64_t numCols() const
void save(Archive &ar, const ExplainedQueryHint &query_hint, const unsigned int version)
std::pair< torch::Tensor, torch::Tensor > get_batch(const ColumnList< double > &cols, torch::Tensor W_target, torch::Tensor b_target, int32_t batch_size)
std::string poly_desc(torch::Tensor W, torch::Tensor b)
torch::Tensor f(torch::Tensor x, torch::Tensor W_target, torch::Tensor b_target)
torch::Device _test_torch_tfs_device

+ Here is the call graph for this function:

Variable Documentation

torch::Device _test_torch_tfs_device = torch::kCPU