From 261a9078a52a6ad68e4772618c64a47da3045841 Mon Sep 17 00:00:00 2001 From: Changming Sun Date: Tue, 2 Apr 2019 17:59:57 -0700 Subject: [PATCH] Remove redundant code in ml::write_scores --- onnxruntime/core/providers/cpu/ml/ml_common.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/onnxruntime/core/providers/cpu/ml/ml_common.h b/onnxruntime/core/providers/cpu/ml/ml_common.h index 579d6342cb..3e8c32f979 100644 --- a/onnxruntime/core/providers/cpu/ml/ml_common.h +++ b/onnxruntime/core/providers/cpu/ml/ml_common.h @@ -284,10 +284,11 @@ static inline void ComputeSoftmaxZero(std::vector& values) { } } -static inline void write_scores(std::vector& scores, POST_EVAL_TRANSFORM post_transform, int64_t write_index, Tensor* Z, int add_second_class) { +template +void write_scores(std::vector& scores, POST_EVAL_TRANSFORM post_transform, int64_t write_index, Tensor* Z, + int add_second_class) { if (post_transform == POST_EVAL_TRANSFORM::PROBIT && scores.size() == 1) { scores[0] = ComputeProbit(scores[0]); - Z->template MutableData()[write_index] = scores[0]; } else if (scores.size() >= 2) { //multiclass if (post_transform == POST_EVAL_TRANSFORM::LOGISTIC) { for (float& score : scores) { @@ -322,10 +323,12 @@ static inline void write_scores(std::vector& scores, POST_EVAL_TRANSFORM } } } - for (float score : scores) { - Z->template MutableData()[write_index] = score; - write_index++; + T* out_p = Z->template MutableData() + write_index; + size_t len; + if (!IAllocator::CalcMemSizeForArray(scores.size(), sizeof(T), &len)) { + ORT_THROW("length overflow"); } + memcpy(out_p, scores.data(), len); } } // namespace ml