// Copyright (c) 2021 CINN Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #include #include #include "paddle/cinn/ir/ir.h" namespace cinn { namespace hlir { namespace pe { /** * @brief sums array elements over a given axis * * @param A The input Tensor * @param stages The stage map * @param axis Axis or axes along which a sum is performed. If axis is empty, the operation will sum over all elements * of the input array. If axis is negative it counts from the last to the first axis. * @param keep_dims If it is set true, the axes which are reduced are left in the result as dimensions with size one. * With this option, the result will broadcast correctly against the input array. * @param initial Starting value for the sum. * @param output_name The name of the output Tensor * * @return The result Tensors. */ ir::Tensor ReduceSum(const ir::Tensor& A, const std::vector& axis, const bool keep_dims = false, const std::string& output_name = "T_Reduce_Sum_out"); /** * @brief product array elements over a given axis * * @param A The input Tensor * @param stages The stage map * @param axis Axis or axes along which a production is performed. If axis is empty, the operation will product over all * elements of the input array. If axis is negative it counts from the last to the first axis. * @param keep_dims If it is set true, the axes which are reduced are left in the result as dimensions with size one. * With this option, the result will broadcast correctly against the input array. * @param initial Starting value for the production. * @param output_name The name of the output Tensor * * @return The result Tensors. */ ir::Tensor ReduceProd(const ir::Tensor& A, const std::vector& axis, const bool keep_dims = false, const std::string& output_name = "T_Reduce_Prod_out"); /** * @brief find the maxium of array elements over a given axis * * @param A The input Tensor * @param stages The stage map * @param axis Axis or axes to find the maximum over. If axis is empty, the operation will product over all elements of * the input array. If axis is negative it counts from the last to the first axis. * @param keep_dims If it is set true, the axes which are reduced are left in the result as dimensions with size one. * With this option, the result will broadcast correctly against the input array. * @param output_name The name of the output Tensor * * @return The result Tensor. */ ir::Tensor ReduceMax(const ir::Tensor& A, const std::vector& axis, const bool keep_dims = false, const std::string& output_name = "T_Reduce_Max_out"); /** * @brief find the minimum of array elements over a given axis * * @param A The input Tensor * @param stages The stage map * @param axis Axis or axes to find the minimum over. If axis is empty, the operation will product over all elements of * the input array. If axis is negative it counts from the last to the first axis. * @param keep_dims If it is set true, the axes which are reduced are left in the result as dimensions with size one. * With this option, the result will broadcast correctly against the input array. * @param output_name The name of the output Tensor * * @return The result Tensor. */ ir::Tensor ReduceMin(const ir::Tensor& A, const std::vector& axis, const bool keep_dims = false, const std::string& output_name = "T_Reduce_Min_out"); /** * @brief find the logic and of array elements over a given axis * * @param A The input Tensor * @param stages The stage map * @param axis Axis or axes to find the logic and over. If axis is empty, the operation will product over all elements * of the input array. If axis is negative it counts from the last to the first axis. * @param keep_dims If it is set true, the axes which are reduced are left in the result as dimensions with size one. * With this option, the result will broadcast correctly against the input array. * @param output_name The name of the output Tensor * * @return The result Tensor. */ ir::Tensor ReduceAll(const ir::Tensor& A, const std::vector& axis, const bool keep_dims = false, const std::string& output_name = "T_Reduce_All_out"); /** * @brief find the logic or of array elements over a given axis * * @param A The input Tensor * @param stages The stage map * @param axis Axis or axes to find the logic or over. If axis is empty, the operation will product over all elements of * the input array. If axis is negative it counts from the last to the first axis. * @param keep_dims If it is set true, the axes which are reduced are left in the result as dimensions with size one. * With this option, the result will broadcast correctly against the input array. * @param output_name The name of the output Tensor * * @return The result Tensor. */ ir::Tensor ReduceAny(const ir::Tensor& A, const std::vector& axis, const bool keep_dims = false, const std::string& output_name = "T_Reduce_Any_out"); /** * @brief find the max of array elements over the last dimension * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector WarpReduceMax(const ir::Tensor& A, const int last_reduce_dim_num, const bool keep_dim = false, const std::string& output_name = "T_Warp_Reduce_Max_out"); /** * @brief compute the sum of array elements over the last dimension * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector WarpReduceSum(const ir::Tensor& A, const int last_reduce_dim_num, const bool keep_dim = false, const std::string& output_name = "T_Warp_Reduce_Sum_out"); /** * @brief compute the average of array elements over the last dimension * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector WarpReduceAvg(const ir::Tensor& A, const int last_reduce_dim_num, const bool keep_dim = false, const std::string& output_name = "T_Warp_Reduce_Avg_out"); /** * @brief compute the sum of array elements over the last dimension with block reduce. * 'BlockReduceSumInternal' is used as the internal compute of reduce sum, do not use it directly. * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector BlockReduceSumInternal(const ir::Tensor& A, const std::vector& axes, const bool keep_dim = false, const std::string& output_name = "T_Block_Reduce_Sum_Internal_out"); /** * @brief compute the Product of array elements over the last dimension with block reduce. * 'BlockReduceSumInternal' is used as the internal compute of reduce sum, do not use it directly. * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector BlockReduceProdInternal(const ir::Tensor& A, const std::vector& axes, const bool keep_dim = false, const std::string& output_name = "T_Block_Reduce_Prod_Internal_out"); /** * @brief compute the Max of array elements over the last dimension with block reduce. * 'BlockReduceSumInternal' is used as the internal compute of reduce sum, do not use it directly. * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector BlockReduceMaxInternal(const ir::Tensor& A, const std::vector& axes, const bool keep_dim = false, const std::string& output_name = "T_Block_Reduce_Max_Internal_out"); /** * @brief compute the Min of array elements over the last dimension with block reduce. * 'BlockReduceSumInternal' is used as the internal compute of reduce sum, do not use it directly. * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector BlockReduceMinInternal(const ir::Tensor& A, const std::vector& axes, const bool keep_dim = false, const std::string& output_name = "T_Block_Reduce_Min_Internal_out"); /** * @brief compute the logic and of array elements over the last dimension with block reduce. * 'BlockReduceSumInternal' is used as the internal compute of reduce sum, do not use it directly. * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector BlockReduceAllInternal(const ir::Tensor& A, const std::vector& axes, const bool keep_dim = false, const std::string& output_name = "T_Block_Reduce_All_Internal_out"); /** * @brief compute the logic or of array elements over the last dimension with block reduce. * 'BlockReduceSumInternal' is used as the internal compute of reduce sum, do not use it directly. * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector BlockReduceAnyInternal(const ir::Tensor& A, const std::vector& axes, const bool keep_dim = false, const std::string& output_name = "T_Block_Reduce_Any_Internal_out"); /** * @brief compute the Sum of array elements over the last dimension with block reduce * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector BlockReduceSum(const ir::Tensor& A, const std::vector& axes, const int block_size, const bool keep_dim = false, const std::string& output_name = "T_Block_Reduce_Sum_out"); /** * @brief compute the Product of array elements over the last dimension with block reduce * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector BlockReduceProd(const ir::Tensor& A, const std::vector& axes, const int block_size, const bool keep_dim = false, const std::string& output_name = "T_Block_Reduce_Prod_out"); /** * @brief compute the Max of array elements over the last dimension with block reduce * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector BlockReduceMax(const ir::Tensor& A, const std::vector& axes, const int block_size, const bool keep_dim = false, const std::string& output_name = "T_Block_Reduce_Max_out"); /** * @brief compute the Min of array elements over the last dimension with block reduce * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector BlockReduceMin(const ir::Tensor& A, const std::vector& axes, const int block_size, const bool keep_dim = false, const std::string& output_name = "T_Block_Reduce_Min_out"); /** * @brief compute the logic and of array elements over the last dimension with block reduce * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector BlockReduceAll(const ir::Tensor& A, const std::vector& axes, const int block_size, const bool keep_dim = false, const std::string& output_name = "T_Block_Reduce_All_out"); /** * @brief compute the logic or of array elements over the last dimension with block reduce * * @param A The input Tensor. * @param last_reduce_dim_num the number of last reduce dimension. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector BlockReduceAny(const ir::Tensor& A, const std::vector& axes, const int block_size, const bool keep_dim = false, const std::string& output_name = "T_Block_Reduce_Any_out"); /** * @brief compute the value of array elements over the last dimension with block reduce * * @param A The input Tensor. * @param axes the reduce axes. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector BlockShuffleReduceSum(const ir::Tensor& A, const std::vector& axes, const bool keep_dim, const std::string& output_name = "T_Reduce_Sum_out"); std::vector BlockShuffleReduceProd(const ir::Tensor& A, const std::vector& axes, const bool keep_dim, const std::string& output_name = "T_Reduce_Prod_out"); std::vector BlockShuffleReduceMax(const ir::Tensor& A, const std::vector& axes, const bool keep_dim, const std::string& output_name = "T_Reduce_Max_out"); std::vector BlockShuffleReduceMin(const ir::Tensor& A, const std::vector& axes, const bool keep_dim, const std::string& output_name = "T_Reduce_Min_out"); std::vector BlockShuffleReduceAll(const ir::Tensor& A, const std::vector& axes, const bool keep_dim, const std::string& output_name = "T_Reduce_All_out"); std::vector BlockShuffleReduceAny(const ir::Tensor& A, const std::vector& axes, const bool keep_dim, const std::string& output_name = "T_Reduce_Any_out"); /** * @brief compute the value of array elements over the last dimension with block reduce * * @param A The input Tensor. * @param axes the reduce axes. * @param keep_dim keep the output tensor shape size as input. * @param output_name The name of the output Tensor. */ std::vector TwoStepBlockReduceSum(const ir::Tensor& A, const std::vector& axes, const bool keep_dim, const std::string& output_name = "T_Reduce_Sum_out"); std::vector TwoStepBlockReduceProd(const ir::Tensor& A, const std::vector& axes, const bool keep_dim, const std::string& output_name = "T_Reduce_Prod_out"); std::vector TwoStepBlockReduceMax(const ir::Tensor& A, const std::vector& axes, const bool keep_dim, const std::string& output_name = "T_Reduce_Max_out"); std::vector TwoStepBlockReduceMin(const ir::Tensor& A, const std::vector& axes, const bool keep_dim, const std::string& output_name = "T_Reduce_Min_out"); std::vector TwoStepBlockReduceAll(const ir::Tensor& A, const std::vector& axes, const bool keep_dim, const std::string& output_name = "T_Reduce_All_out"); std::vector TwoStepBlockReduceAny(const ir::Tensor& A, const std::vector& axes, const bool keep_dim, const std::string& output_name = "T_Reduce_Any_out"); } // namespace pe } // namespace hlir } // namespace cinn