1 /** 2 * Copyright 2021 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_STRIDESLICE_CPU_KERNEL_H_ 18 #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_STRIDESLICE_CPU_KERNEL_H_ 19 20 #include <vector> 21 #include <memory> 22 #include "backend/kernel_compiler/cpu/cpu_kernel.h" 23 #include "backend/kernel_compiler/cpu/cpu_kernel_factory.h" 24 #include "nnacl/fp32/strided_slice_fp32.h" 25 26 namespace mindspore { 27 namespace kernel { 28 class StridedSliceCPUKernel : public CPUKernel { 29 public: 30 StridedSliceCPUKernel() = default; 31 ~StridedSliceCPUKernel() override = default; 32 33 void InitKernel(const CNodePtr &kernel_node) override; 34 35 bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace, 36 const std::vector<AddressPtr> &outputs) override; 37 38 private: 39 enum ParallelStrategy { kOnSplitAxis, kOnOuter }; 40 void InitSliceParam(const std::vector<int64_t> &begin, const std::vector<int64_t> &end, 41 const std::vector<int64_t> &stride); 42 bool MatchParallelPattern(); 43 void InitParallelParam(); 44 void ParallelRun(const uint8_t *input_addr, uint8_t *output_addr, int thread_num); 45 int RunTaskOnOuter(const uint8_t *input_addr, uint8_t *output_addr, int start_pos); 46 int RunTaskOnSplitAxis(const uint8_t *input_addr, uint8_t *output_addr, int start_pos); 47 48 TypeId dtype_; 49 int data_size_{4}; 50 int split_axis_{-1}; 51 int inner_{1}; 52 int outer_{1}; 53 int cal_num_per_thread_{1}; 54 bool parallel_{false}; 55 ParallelStrategy parallel_strategy_{kOnSplitAxis}; 56 std::vector<size_t> input_shape_; 57 std::vector<size_t> output_shape_; 58 StridedSliceParameter slice_param_; 59 }; 60 61 MS_REG_CPU_KERNEL(StridedSlice, KernelAttr().AddInputAttr(kNumberTypeBool).AddOutputAttr(kNumberTypeBool), 62 StridedSliceCPUKernel); 63 MS_REG_CPU_KERNEL(StridedSlice, KernelAttr().AddInputAttr(kNumberTypeInt32).AddOutputAttr(kNumberTypeInt32), 64 StridedSliceCPUKernel); 65 MS_REG_CPU_KERNEL(StridedSlice, KernelAttr().AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), 66 StridedSliceCPUKernel); 67 MS_REG_CPU_KERNEL(StridedSlice, KernelAttr().AddInputAttr(kNumberTypeFloat64).AddOutputAttr(kNumberTypeFloat64), 68 StridedSliceCPUKernel); 69 } // namespace kernel 70 } // namespace mindspore 71 72 #endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_STRIDESLICE_CPU_KERNEL_H_ 73