From b5c45830ab93002cf090c9d5708e20721fa63185 Mon Sep 17 00:00:00 2001 From: Nishak Date: Fri, 20 Dec 2024 04:59:42 -0800 Subject: [PATCH 1/3] adding ET_KERNEL_CHECK for allocate_temp_memory --- backends/cadence/hifi/operators/op_atan2.cpp | 11 +++++++++++ backends/cadence/hifi/operators/op_clamp.cpp | 3 +++ backends/cadence/hifi/operators/op_mean.cpp | 2 ++ backends/cadence/hifi/operators/op_pow.cpp | 9 +++++++++ backends/cadence/hifi/operators/op_softmax.cpp | 5 +++++ backends/cadence/hifi/operators/op_where.cpp | 3 +++ 6 files changed, 33 insertions(+) diff --git a/backends/cadence/hifi/operators/op_atan2.cpp b/backends/cadence/hifi/operators/op_atan2.cpp index 234432ae5d3..2dfd657bfe0 100644 --- a/backends/cadence/hifi/operators/op_atan2.cpp +++ b/backends/cadence/hifi/operators/op_atan2.cpp @@ -66,10 +66,15 @@ Tensor& atan2_out( WORD32* __restrict__ ptr1 = (WORD32* __restrict__)kernels::allocate_temp_memory( ctx, num_elm * sizeof(WORD32)); + + ET_KERNEL_CHECK(ctx, ptr1 != nullptr, MemoryAllocationFailed, out); + WORD32* __restrict__ ptr2 = (WORD32* __restrict__)kernels::allocate_temp_memory( ctx, num_elm * sizeof(WORD32)); + ET_KERNEL_CHECK(ctx, ptr2 != nullptr, MemoryAllocationFailed, out); + WORD32* __restrict__ pin1 = (WORD32* __restrict__)a.const_data_ptr(); WORD32* __restrict__ pin2 = @@ -108,9 +113,13 @@ Tensor& atan2_out( (FLOAT32* __restrict__)kernels::allocate_temp_memory( ctx, num_elm * sizeof(WORD32)); + ET_KERNEL_CHECK(ctx, ptr1 != nullptr, MemoryAllocationFailed, out); + FLOAT32* __restrict__ pin1 = (FLOAT32* __restrict__)a.const_data_ptr(); + ET_KERNEL_CHECK(ctx, pin1 != nullptr, MemoryAllocationFailed, out); + WORD32 p_out_shape[kNnlibMaxDim]; WORD32 p_inp1_shape[kNnlibMaxDim]; @@ -137,6 +146,8 @@ Tensor& atan2_out( (WORD32* __restrict__)kernels::allocate_temp_memory( ctx, num_elm * sizeof(WORD32)); + ET_KERNEL_CHECK(ctx, ptr1 != nullptr, MemoryAllocationFailed, out); + WORD32* __restrict__ pin1 = (WORD32* __restrict__)b.const_data_ptr(); diff --git a/backends/cadence/hifi/operators/op_clamp.cpp b/backends/cadence/hifi/operators/op_clamp.cpp index 8ed57a8e168..f92fbd34460 100644 --- a/backends/cadence/hifi/operators/op_clamp.cpp +++ b/backends/cadence/hifi/operators/op_clamp.cpp @@ -359,6 +359,9 @@ Tensor& clamp_tensor_out( ctx, (out_shape[0] * out_shape[1] * out_shape[2] * out_shape[3]) * sizeof(int)); + + ET_KERNEL_CHECK(ctx, p_scratch != nullptr, MemoryAllocationFailed, out); + const FLOAT32* p_brd_cond = (const FLOAT32*)p_scratch; xa_nn_broadcast_32_32( (WORD32*)p_brd_cond, out_shape, (WORD32*)inp_data, inp_shape, 4); diff --git a/backends/cadence/hifi/operators/op_mean.cpp b/backends/cadence/hifi/operators/op_mean.cpp index cdc844ec5c7..2584f753f6f 100644 --- a/backends/cadence/hifi/operators/op_mean.cpp +++ b/backends/cadence/hifi/operators/op_mean.cpp @@ -129,6 +129,8 @@ Tensor& mean_dim_out( (void* __restrict__)kernels::allocate_temp_memory( ctx, scratch_size * sizeof(int)); + ET_KERNEL_CHECK(ctx, p_scratch_in != nullptr, MemoryAllocationFailed, out); + xa_nn_reduce_mean_4D_f32_f32( p_out, out_shape, diff --git a/backends/cadence/hifi/operators/op_pow.cpp b/backends/cadence/hifi/operators/op_pow.cpp index 3c42698f213..a2775ca19fc 100644 --- a/backends/cadence/hifi/operators/op_pow.cpp +++ b/backends/cadence/hifi/operators/op_pow.cpp @@ -122,10 +122,15 @@ Tensor& pow_Tensor_Tensor_out( WORD32* __restrict__ ptr1 = (WORD32* __restrict__)kernels::allocate_temp_memory( ctx, num_elm * sizeof(int)); + + ET_KERNEL_CHECK(ctx, ptr1 != nullptr, MemoryAllocationFailed, out); + WORD32* __restrict__ ptr2 = (WORD32* __restrict__)kernels::allocate_temp_memory( ctx, num_elm * sizeof(int)); + ET_KERNEL_CHECK(ctx, ptr2 != nullptr, MemoryAllocationFailed, out); + WORD32* __restrict__ pin1 = (WORD32* __restrict__)a.const_data_ptr(); WORD32* __restrict__ pin2 = @@ -158,6 +163,8 @@ Tensor& pow_Tensor_Tensor_out( (FLOAT32* __restrict__)kernels::allocate_temp_memory( ctx, num_elm * sizeof(int)); + ET_KERNEL_CHECK(ctx, ptr1 != nullptr, MemoryAllocationFailed, out); + FLOAT32* __restrict__ pin1 = (FLOAT32* __restrict__)a.const_data_ptr(); @@ -185,6 +192,8 @@ Tensor& pow_Tensor_Tensor_out( (WORD32* __restrict__)kernels::allocate_temp_memory( ctx, num_elm * sizeof(int)); + ET_KERNEL_CHECK(ctx, ptr1 != nullptr, MemoryAllocationFailed, out); + WORD32* __restrict__ pin1 = (WORD32* __restrict__)b.const_data_ptr(); diff --git a/backends/cadence/hifi/operators/op_softmax.cpp b/backends/cadence/hifi/operators/op_softmax.cpp index a2068fd15b6..0d687ff0956 100644 --- a/backends/cadence/hifi/operators/op_softmax.cpp +++ b/backends/cadence/hifi/operators/op_softmax.cpp @@ -101,9 +101,14 @@ Tensor& softmax_out( int* p_out = (int*)kernels::allocate_temp_memory(ctx, out.numel() * sizeof(int)); + + ET_KERNEL_CHECK(ctx, p_out != nullptr, MemoryAllocationFailed, out); + int* p_out1 = (int*)kernels::allocate_temp_memory(ctx, out.numel() * sizeof(int)); + ET_KERNEL_CHECK(ctx, p_out1 != nullptr, MemoryAllocationFailed, out); + WORD32 ret_val = xa_nn_transpose_32_32( p_out, p_out_shape, diff --git a/backends/cadence/hifi/operators/op_where.cpp b/backends/cadence/hifi/operators/op_where.cpp index 7e9c5f9dfa0..0bb3883df6c 100644 --- a/backends/cadence/hifi/operators/op_where.cpp +++ b/backends/cadence/hifi/operators/op_where.cpp @@ -113,6 +113,9 @@ Tensor& where_out( ctx, (out_shape[0] * out_shape[1] * out_shape[2] * out_shape[3]) * sizeof(int)); + + ET_KERNEL_CHECK(ctx, p_scratch != nullptr, MemoryAllocationFailed, out); + const unsigned char* p_brd_cond = (const unsigned char*)p_scratch; xa_nn_broadcast_8_8( (WORD8* __restrict__)p_brd_cond, From e44ffe65a5ebd527309c1813f2013553aaf3ef08 Mon Sep 17 00:00:00 2001 From: Nishak Date: Fri, 20 Dec 2024 05:07:23 -0800 Subject: [PATCH 2/3] solving lint error --- backends/cadence/hifi/operators/op_clamp.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backends/cadence/hifi/operators/op_clamp.cpp b/backends/cadence/hifi/operators/op_clamp.cpp index f92fbd34460..e8d83e37dac 100644 --- a/backends/cadence/hifi/operators/op_clamp.cpp +++ b/backends/cadence/hifi/operators/op_clamp.cpp @@ -360,7 +360,8 @@ Tensor& clamp_tensor_out( (out_shape[0] * out_shape[1] * out_shape[2] * out_shape[3]) * sizeof(int)); - ET_KERNEL_CHECK(ctx, p_scratch != nullptr, MemoryAllocationFailed, out); + ET_KERNEL_CHECK( + ctx, p_scratch != nullptr, MemoryAllocationFailed, out); const FLOAT32* p_brd_cond = (const FLOAT32*)p_scratch; xa_nn_broadcast_32_32( From 91cfe3072bd437815ac7346624f6b3623421089a Mon Sep 17 00:00:00 2001 From: Nishak Date: Sun, 22 Dec 2024 21:21:27 -0800 Subject: [PATCH 3/3] Removing redundant check --- backends/cadence/hifi/operators/op_atan2.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/backends/cadence/hifi/operators/op_atan2.cpp b/backends/cadence/hifi/operators/op_atan2.cpp index 2dfd657bfe0..2b0e3131c9b 100644 --- a/backends/cadence/hifi/operators/op_atan2.cpp +++ b/backends/cadence/hifi/operators/op_atan2.cpp @@ -118,8 +118,6 @@ Tensor& atan2_out( FLOAT32* __restrict__ pin1 = (FLOAT32* __restrict__)a.const_data_ptr(); - ET_KERNEL_CHECK(ctx, pin1 != nullptr, MemoryAllocationFailed, out); - WORD32 p_out_shape[kNnlibMaxDim]; WORD32 p_inp1_shape[kNnlibMaxDim];