From 21cc2d88b4e97b343c8518ab893b45e886823dc3 Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Sat, 22 Feb 2020 06:35:13 +1000 Subject: [PATCH] Move some vectors out of loops to minimize memory allocations/reallocations (#3062) * Move some vectors out of loops to minimize memory allocations/reallocations. Remove some unused/unnecessary vectors. --- .../core/providers/cpu/ml/linearregressor.cc | 5 +++- .../core/providers/cpu/ml/svmclassifier.cc | 30 +++++++++++-------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/onnxruntime/core/providers/cpu/ml/linearregressor.cc b/onnxruntime/core/providers/cpu/ml/linearregressor.cc index 455a11dc86..c5f0edf13e 100644 --- a/onnxruntime/core/providers/cpu/ml/linearregressor.cc +++ b/onnxruntime/core/providers/cpu/ml/linearregressor.cc @@ -35,9 +35,12 @@ Status LinearRegressor::Compute(OpKernelContext* ctx) const { int64_t yindex = 0; bool useIntercepts = intercepts_.size() == static_cast(targets_); + std::vector scores; + scores.reserve(targets_); + for (int64_t i = 0; i < N; i++) //for each point { - std::vector scores; + scores.clear(); int64_t current_weight_0 = i * stride; for (int j = 0; j < targets_; j++) //for each target { diff --git a/onnxruntime/core/providers/cpu/ml/svmclassifier.cc b/onnxruntime/core/providers/cpu/ml/svmclassifier.cc index ce7b5c01dd..331efde193 100644 --- a/onnxruntime/core/providers/cpu/ml/svmclassifier.cc +++ b/onnxruntime/core/providers/cpu/ml/svmclassifier.cc @@ -124,14 +124,22 @@ Status SVMClassifier::Compute(OpKernelContext* ctx) const { const T* x_data = X->template Data(); int64_t zindex = 0; + std::vector scores; + std::vector kernels; + std::vector votes; + std::vector probsp2; + + const int64_t class_count_squared = class_count_ * class_count_; + probsp2.reserve(class_count_squared); + scores.reserve(class_count_squared); + for (int64_t n = 0; n < N; n++) //for each example { + scores.clear(); + kernels.clear(); + int64_t current_weight_0 = n * stride; int64_t maxclass = -1; - std::vector decisions; - std::vector scores; - std::vector kernels; - std::vector votes; if (vector_count_ == 0 && mode_ == SVM_TYPE::SVM_LINEAR) { for (int64_t j = 0; j < class_count_; j++) { //for each class @@ -150,7 +158,8 @@ Status SVMClassifier::Compute(OpKernelContext* ctx) const { feature_count_, get_kernel_type()); kernels.push_back(val); } - votes.resize(class_count_, 0); + + votes.assign(class_count_, 0); for (int64_t i = 0; i < class_count_; i++) { // for each class for (int64_t j = i + 1; j < class_count_; j++) { // for each class double sum = 0; @@ -182,9 +191,7 @@ Status SVMClassifier::Compute(OpKernelContext* ctx) const { if (proba_.size() > 0 && mode_ == SVM_TYPE::SVM_SVC) { //compute probabilities from the scores - int64_t num = class_count_ * class_count_; - std::vector probsp2(num, 0.f); - std::vector estimates(class_count_, 0.f); + probsp2.assign(class_count_squared, 0.f); int64_t index = 0; for (int64_t i = 0; i < class_count_; ++i) { int64_t p1 = i * class_count_ + i + 1; @@ -199,10 +206,9 @@ Status SVMClassifier::Compute(OpKernelContext* ctx) const { p2 += class_count_; } } - multiclass_probability(class_count_, probsp2, estimates); - // copy probabilities back into scores - scores.resize(estimates.size()); - std::copy(estimates.begin(), estimates.end(), scores.begin()); + + scores.assign(class_count_, 0.f); + multiclass_probability(class_count_, probsp2, scores); } float max_weight = 0;