diff --git a/logfile b/logfile deleted file mode 100644 index d5a4842..0000000 --- a/logfile +++ /dev/null @@ -1,16 +0,0 @@ -2025-09-15 11:51:32.458 MST [77299] LOG: starting PostgreSQL 15.14 (Homebrew) on aarch64-apple-darwin23.6.0, compiled by Apple clang version 16.0.0 (clang-1600.0.26.6), 64-bit -2025-09-15 11:51:32.459 MST [77299] LOG: listening on IPv4 address "127.0.0.1", port 8888 -2025-09-15 11:51:32.459 MST [77299] LOG: listening on IPv6 address "::1", port 8888 -2025-09-15 11:51:32.460 MST [77299] LOG: listening on Unix socket "/tmp/.s.PGSQL.8888" -2025-09-15 11:51:32.466 MST [77302] LOG: database system was interrupted; last known up at 2025-09-03 13:56:52 MST -2025-09-15 11:51:32.537 MST [77302] LOG: database system was not properly shut down; automatic recovery in progress -2025-09-15 11:51:32.541 MST [77302] LOG: redo starts at 0/43401828 -2025-09-15 11:51:35.046 MST [77302] LOG: invalid record length at 0/50F774F8: wanted 24, got 0 -2025-09-15 11:51:35.046 MST [77302] LOG: redo done at 0/50F774C0 system usage: CPU: user: 0.22 s, system: 0.93 s, elapsed: 2.50 s -2025-09-15 11:51:35.051 MST [77300] LOG: checkpoint starting: end-of-recovery immediate wait -2025-09-15 11:51:35.580 MST [77300] LOG: checkpoint complete: wrote 16375 buffers (99.9%); 0 WAL file(s) added, 13 removed, 0 recycled; write=0.390 s, sync=0.005 s, total=0.533 s; sync files=64, longest=0.003 s, average=0.001 s; distance=224727 kB, estimate=224727 kB -2025-09-15 11:51:35.584 MST [77299] LOG: database system is ready to accept connections -2025-09-15 11:53:12.240 MST [77334] ERROR: relation "customers" does not exist at character 22 -2025-09-15 11:53:12.240 MST [77334] STATEMENT: SELECT count(*) FROM CUSTOMERS; -2025-09-15 11:53:12.301 MST [77334] ERROR: relation "suppliers" does not exist at character 22 -2025-09-15 11:53:12.301 MST [77334] STATEMENT: SELECT count(*) FROM SUPPLIERS; diff --git a/src/ml_functions/ml_functions_scalar.cpp b/src/ml_functions/ml_functions_scalar.cpp index 82c2094..7a00bcd 100644 --- a/src/ml_functions/ml_functions_scalar.cpp +++ b/src/ml_functions/ml_functions_scalar.cpp @@ -23,69 +23,64 @@ namespace duckdb { class MatrixData final : public FunctionData { public: - unsigned int num_rows_; - unsigned int num_columns_; - - bool use_gpu_; - - explicit MatrixData (int num_rows, int num_columns, bool use_gpu) { - num_rows_ = num_rows; - num_columns_ = num_columns; - use_gpu_ = use_gpu; - } - - unique_ptr Copy() const override { - return make_uniq(num_rows_, num_columns_, use_gpu_); - } - - bool Equals(const FunctionData &other) const override { - auto &other_data = other.Cast(); - return num_rows_ == other_data.num_rows_ && num_columns_ == other_data.num_columns_ - && use_gpu_ == other_data.use_gpu_; - } - + unsigned int num_rows_; + unsigned int num_columns_; + + bool use_gpu_; + + explicit MatrixData(int num_rows, int num_columns, bool use_gpu) { + num_rows_ = num_rows; + num_columns_ = num_columns; + use_gpu_ = use_gpu; + } + + unique_ptr Copy() const override { + return make_uniq(num_rows_, num_columns_, use_gpu_); + } + + bool Equals(const FunctionData &other) const override { + auto &other_data = other.Cast(); + return num_rows_ == other_data.num_rows_ && num_columns_ == other_data.num_columns_ && + use_gpu_ == other_data.use_gpu_; + } }; - class MatrixDataByPointer final : public FunctionData { public: - float* weights_; - unsigned int num_rows_; - unsigned int num_columns_; - bool use_gpu_; - - - explicit MatrixDataByPointer (float* weights, int num_rows, int num_columns, bool use_gpu) { - weights_ = new float[num_rows * num_columns]; - std::memcpy(weights_, weights, num_rows * num_columns * sizeof(float)); - num_rows_ = num_rows; - num_columns_ = num_columns; - use_gpu_ = use_gpu; - } - - unique_ptr Copy() const override { - return make_uniq(weights_, num_rows_, num_columns_, use_gpu_); - } - - bool Equals(const FunctionData &other) const override { - auto &other_data = other.Cast(); - return num_rows_ == other_data.num_rows_ && num_columns_ == other_data.num_columns_ - && weights_ == other_data.weights_ && use_gpu_ == other_data.use_gpu_; - } - + float *weights_; + unsigned int num_rows_; + unsigned int num_columns_; + bool use_gpu_; + + explicit MatrixDataByPointer(float *weights, int num_rows, int num_columns, bool use_gpu) { + weights_ = new float[num_rows * num_columns]; + std::memcpy(weights_, weights, num_rows * num_columns * sizeof(float)); + num_rows_ = num_rows; + num_columns_ = num_columns; + use_gpu_ = use_gpu; + } + + unique_ptr Copy() const override { + return make_uniq(weights_, num_rows_, num_columns_, use_gpu_); + } + + bool Equals(const FunctionData &other) const override { + auto &other_data = other.Cast(); + return num_rows_ == other_data.num_rows_ && num_columns_ == other_data.num_columns_ && + weights_ == other_data.weights_ && use_gpu_ == other_data.use_gpu_; + } }; - /* static unique_ptr MatrixBindByPointer(ClientContext &context, ScalarFunction &bound_function, - vector> &arguments) { + vector> &arguments) { const auto expr0 = ExpressionExecutor::EvaluateScalar(context, *arguments[0]); const auto weights_value = expr0.GetPointer(); const auto expr1 = ExpressionExecutor::EvaluateScalar(context, *arguments[1]); const auto num_rows_value = expr1.GetValue(); - const auto expr2 = ExpressionExecutor::EvaluateScalar(context, *arguments[2]); + const auto expr2 = ExpressionExecutor::EvaluateScalar(context, *arguments[2]); const auto num_columns_value = expr2.GetValue(); const auto expr3 = ExpressionExecutor::EvaluateScalar(context, *arguments[3]); const auto use_gpu = expr3.GetValue(); @@ -101,118 +96,113 @@ static unique_ptr MatrixBind(ClientContext &context, ScalarFunctio */ - struct ML_MatrixMultiply { public: + static void Execute(DataChunk &args, ExpressionState &state, Vector &result) { + + const auto count = args.size(); + + auto &lhs = ArrayVector::GetEntry(args.data[0]); + auto &rhs = ArrayVector::GetEntry(args.data[1]); + auto &res = ArrayVector::GetEntry(result); + + const auto &lhs_validity = FlatVector::Validity(lhs); + const auto &rhs_validity = FlatVector::Validity(rhs); + + UnifiedVectorFormat lhs_format; + UnifiedVectorFormat rhs_format; + + args.data[0].ToUnifiedFormat(count, lhs_format); + args.data[1].ToUnifiedFormat(count, rhs_format); + + auto lhs_data = FlatVector::GetData(lhs); + auto rhs_data = FlatVector::GetData(rhs); + auto res_data = FlatVector::GetData(res); + + /* + for (int i = 0; i < count; i++) { + std::cout << "lhs-"<< i << ":" << lhs.GetValue(i) << std::endl; + std::cout << "rhs-"<< i << ":"<< rhs.GetValue(i) << std::endl; + } + std::cout << "lhs type:" << int(lhs.GetVectorType()) << std::endl; + std::cout << "lhs type:" << lhs.GetType().ToString() << std::endl; + std::cout << "lhs type:" << EnumUtil::ToChars(lhs.GetType().InternalType()) << std::endl; + std::cout << "rhs type:" << int(GetTypeIdSize(lhs.GetType().InternalType())) << std::endl; + std::cout << "rhs type:" << int(rhs.GetVectorType()) << std::endl; + std::cout << "rhs type:" << EnumUtil::ToChars(rhs.GetType().InternalType()) << std::endl; + std::cout << "rhs type:" << int(GetTypeIdSize(rhs.GetType().InternalType())) << std::endl; + + auto lhs_buffer = lhs.GetBuffer(); + auto rhs_buffer = rhs.GetBuffer(); + + std::cout << "lhs buffer type:" << EnumUtil::ToChars(lhs_buffer->GetBufferType()) << + std::endl; std::cout << "rhs buffer type:" << EnumUtil::ToChars(rhs_buffer->GetBufferType()) + << std::endl; + */ + + Value v2 = args.data[2].GetValue(0); + Value v3 = args.data[3].GetValue(0); + int num_rows = IntegerValue::Get(v2); + int num_columns = IntegerValue::Get(v3); + std::cout << "num rows: " << num_rows << "; num columns: " << num_columns << std::endl; - static void Execute(DataChunk &args, ExpressionState &state, Vector &result) { - - const auto count = args.size(); - - auto &lhs = ArrayVector::GetEntry(args.data[0]); - auto &rhs = ArrayVector::GetEntry(args.data[1]); - auto &res = ArrayVector::GetEntry(result); - - const auto &lhs_validity = FlatVector::Validity(lhs); - const auto &rhs_validity = FlatVector::Validity(rhs); - - UnifiedVectorFormat lhs_format; - UnifiedVectorFormat rhs_format; - - args.data[0].ToUnifiedFormat(count, lhs_format); - args.data[1].ToUnifiedFormat(count, rhs_format); - - auto lhs_data = FlatVector::GetData(lhs); - auto rhs_data = FlatVector::GetData(rhs); - auto res_data = FlatVector::GetData(res); - - /* - for (int i = 0; i < count; i++) { - std::cout << "lhs-"<< i << ":" << lhs.GetValue(i) << std::endl; - std::cout << "rhs-"<< i << ":"<< rhs.GetValue(i) << std::endl; - } - std::cout << "lhs type:" << int(lhs.GetVectorType()) << std::endl; - std::cout << "lhs type:" << lhs.GetType().ToString() << std::endl; - std::cout << "lhs type:" << EnumUtil::ToChars(lhs.GetType().InternalType()) << std::endl; - std::cout << "rhs type:" << int(GetTypeIdSize(lhs.GetType().InternalType())) << std::endl; - std::cout << "rhs type:" << int(rhs.GetVectorType()) << std::endl; - std::cout << "rhs type:" << EnumUtil::ToChars(rhs.GetType().InternalType()) << std::endl; - std::cout << "rhs type:" << int(GetTypeIdSize(rhs.GetType().InternalType())) << std::endl; - - auto lhs_buffer = lhs.GetBuffer(); - auto rhs_buffer = rhs.GetBuffer(); - - std::cout << "lhs buffer type:" << EnumUtil::ToChars(lhs_buffer->GetBufferType()) << std::endl; - std::cout << "rhs buffer type:" << EnumUtil::ToChars(rhs_buffer->GetBufferType()) << std::endl; - */ - - - Value v2 = args.data[2].GetValue(0); - Value v3 = args.data[3].GetValue(0); - int num_rows = IntegerValue::Get(v2); - int num_columns = IntegerValue::Get(v3); - std::cout << "num rows: " << num_rows << "; num columns: " << num_columns << std::endl; - - const auto rhs_idx = rhs_format.sel->get_index(0); - const auto right_offset = rhs_idx * num_rows; - if (!rhs_validity.CheckAllValid(right_offset + num_rows, right_offset)) { - throw InvalidInputException(StringUtil::Format("right argument can not contain NULL values")); - } - const auto rhs_data_ptr = rhs_data + right_offset; - Eigen::Map> - weight_matrix((float *)rhs_data_ptr, num_rows, num_columns); - - for (idx_t i = 0; i < count; i++) { - const auto lhs_idx = lhs_format.sel->get_index(i); - - if (!lhs_format.validity.RowIsValid(lhs_idx)) { - FlatVector::SetNull(result, i, true); - continue; - } - - const auto left_offset = lhs_idx * num_rows; - if (!lhs_validity.CheckAllValid(left_offset + num_rows, left_offset)) { - throw InvalidInputException(StringUtil::Format("left argument can not contain NULL values")); - } - - const auto result_offset = i * num_rows; - - const auto lhs_data_ptr = lhs_data + left_offset; - const auto res_data_ptr = res_data + result_offset; - - bool use_gpu = false; - - if (use_gpu) { - // TODO: implementation of matrix multiplication in GPU - throw std::runtime_error( - "GPU implementation of Matrix Multiple is not implemented."); - } else { - - Eigen::Map> - input_matrix((float *)lhs_data_ptr, 1, num_rows); - - Eigen::Matrix - result_matrix = input_matrix * weight_matrix; - - //dispatch results to multiple rows - - std::memcpy ( - (float*)res_data_ptr, result_matrix.row(0).data(),num_columns); - } - - } - } - - static void RegisterByArray(ExtensionLoader &loader, int num_features, int num_neurons) { - - FunctionBuilder::RegisterScalar(loader, "matmul", [&](ScalarFunctionBuilder &func) { + const auto rhs_idx = rhs_format.sel->get_index(0); + const auto right_offset = rhs_idx * num_rows; + if (!rhs_validity.CheckAllValid(right_offset + num_rows, right_offset)) { + throw InvalidInputException(StringUtil::Format("right argument can not contain NULL values")); + } + const auto rhs_data_ptr = rhs_data + right_offset; + Eigen::Map> weight_matrix( + (float *)rhs_data_ptr, num_rows, num_columns); + + for (idx_t i = 0; i < count; i++) { + const auto lhs_idx = lhs_format.sel->get_index(i); + + if (!lhs_format.validity.RowIsValid(lhs_idx)) { + FlatVector::SetNull(result, i, true); + continue; + } + + const auto left_offset = lhs_idx * num_rows; + if (!lhs_validity.CheckAllValid(left_offset + num_rows, left_offset)) { + throw InvalidInputException(StringUtil::Format("left argument can not contain NULL values")); + } + + const auto result_offset = i * num_rows; + + const auto lhs_data_ptr = lhs_data + left_offset; + const auto res_data_ptr = res_data + result_offset; + + bool use_gpu = false; + + if (use_gpu) { + // TODO: implementation of matrix multiplication in GPU + throw std::runtime_error("GPU implementation of Matrix Multiple is not implemented."); + } else { + + Eigen::Map> input_matrix( + (float *)lhs_data_ptr, 1, num_rows); + + Eigen::Matrix result_matrix = + input_matrix * weight_matrix; + + // dispatch results to multiple rows + + std::memcpy((float *)res_data_ptr, result_matrix.row(0).data(), num_columns); + } + } + } + + static void RegisterByArray(ExtensionLoader &loader, int num_features, int num_neurons) { + + FunctionBuilder::RegisterScalar(loader, "matmul", [&](ScalarFunctionBuilder &func) { func.AddVariant([&](ScalarFunctionVariantBuilder &variant) { variant.AddParameter("input", LogicalType::ARRAY(LogicalType::FLOAT, num_features)); variant.AddParameter("weight", LogicalType::ARRAY(LogicalType::FLOAT, num_features * num_neurons)); - variant.AddParameter("num_rows", LogicalType::INTEGER); - variant.AddParameter("num_columns", LogicalType::INTEGER); - variant.SetReturnType(LogicalType::ARRAY(LogicalType::FLOAT, num_neurons)); + variant.AddParameter("num_rows", LogicalType::INTEGER); + variant.AddParameter("num_columns", LogicalType::INTEGER); + variant.SetReturnType(LogicalType::ARRAY(LogicalType::FLOAT, num_neurons)); variant.SetFunction(Execute); }); @@ -222,18 +212,13 @@ struct ML_MatrixMultiply { func.SetTag("ext", "cactusdb"); func.SetTag("category", "relation"); - }); - - } - + }); + } }; void RegisterMLScalarFunctions(ExtensionLoader &loader) { - ML_MatrixMultiply::RegisterByArray(loader, 8, 2); - -} - - + ML_MatrixMultiply::RegisterByArray(loader, 8, 2); } +} // namespace duckdb