diff --git a/onnxruntime/core/providers/cuda/reduction/reduction_ops.cc b/onnxruntime/core/providers/cuda/reduction/reduction_ops.cc index 4568c0785d..c4d6bc11c0 100644 --- a/onnxruntime/core/providers/cuda/reduction/reduction_ops.cc +++ b/onnxruntime/core/providers/cuda/reduction/reduction_ops.cc @@ -202,9 +202,11 @@ Status ReduceKernel::ReduceKernelShared( auto output_count = output_shape.Size(); if (ReduceTensorIndices == CUDNN_REDUCE_TENSOR_NO_INDICES) { + IAllocatorUniquePtr input_data_buffer(nullptr, [](T*) {}); CudaT* input_data = nullptr; if (calculate_sqt_) { - input_data = reinterpret_cast(GetScratchBuffer(input_count).get()); + input_data_buffer = GetScratchBuffer(input_count); + input_data = reinterpret_cast(input_data_buffer.get()); fast_divmod tmp_div; Impl_Mul(static_cast(SimpleBroadcast::NoBroadcast), nullptr, reinterpret_cast(X), nullptr, @@ -225,8 +227,10 @@ Status ReduceKernel::ReduceKernelShared( // Exp(X-ReduceMax) const TensorShape rhs_shape(output_dims); - auto exp_result = GetScratchBuffer(input_count).get(); - auto log_sum_result = GetScratchBuffer(output_count).get(); + auto exp_result_buffer = GetScratchBuffer(input_count); + auto exp_result = exp_result_buffer.get(); + auto log_sum_result_buffer = GetScratchBuffer(output_count); + auto log_sum_result = log_sum_result_buffer.get(); BinaryElementwisePreparation prepare; ORT_RETURN_IF_ERROR(prepare.BinaryElementwiseBroadcastPrepareHelper(input_shape, rhs_shape, input_shape)); Impl_Sub(prepare.output_rank_or_simple_broadcast, diff --git a/onnxruntime/core/providers/rocm/reduction/reduction_ops.cc b/onnxruntime/core/providers/rocm/reduction/reduction_ops.cc index 2cbe727ed6..43e4023d19 100644 --- a/onnxruntime/core/providers/rocm/reduction/reduction_ops.cc +++ b/onnxruntime/core/providers/rocm/reduction/reduction_ops.cc @@ -202,9 +202,11 @@ Status ReduceKernel::ReduceKernelShared( auto output_count = output_shape.Size(); if (ReduceTensorIndices == MIOPEN_REDUCE_TENSOR_NO_INDICES) { + IAllocatorUniquePtr input_data_buffer(nullptr, [](T*) {}); HipT* input_data = nullptr; if (calculate_sqt_) { - input_data = reinterpret_cast(GetScratchBuffer(input_count).get()); + input_data_buffer = GetScratchBuffer(input_count); + input_data = reinterpret_cast(input_data_buffer.get()); fast_divmod tmp_div; Impl_Mul(static_cast(SimpleBroadcast::NoBroadcast), nullptr, reinterpret_cast(X), nullptr, @@ -225,8 +227,10 @@ Status ReduceKernel::ReduceKernelShared( // Exp(X-ReduceMax) const TensorShape rhs_shape(output_dims); - auto exp_result = GetScratchBuffer(input_count).get(); - auto log_sum_result = GetScratchBuffer(output_count).get(); + auto exp_result_buffer = GetScratchBuffer(input_count); + auto exp_result = exp_result_buffer.get(); + auto log_sum_result_buffer = GetScratchBuffer(output_count); + auto log_sum_result = log_sum_result_buffer.get(); BinaryElementwisePreparation prepare; ORT_RETURN_IF_ERROR(prepare.BinaryElementwiseBroadcastPrepareHelper(input_shape, rhs_shape, input_shape)); Impl_Sub(prepare.output_rank_or_simple_broadcast,