|
| 1 | +// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +#include "custom_engine/ir_translator/translator_registry.h" |
| 18 | + |
| 19 | +namespace custom_engine { |
| 20 | + |
| 21 | +static GcuOpPtr TranslateSlice( |
| 22 | + GcuBuilderPtr gcu_builder, |
| 23 | + const pir::Operation *op, |
| 24 | + const std::vector<std::vector<GcuOpPtr>> &gcu_op_inputs) { |
| 25 | + // Get attributes |
| 26 | + const auto &attributes = op->attributes(); |
| 27 | + auto axes_list = |
| 28 | + attributes.at("axes").dyn_cast<pir::ArrayAttribute>().AsVector(); |
| 29 | + std::vector<int64_t> axes; |
| 30 | + if (axes_list.size() > 0) { |
| 31 | + PADDLE_ENFORCE_EQ(axes_list[0].isa<pir::Int64Attribute>(), |
| 32 | + true, |
| 33 | + common::errors::Unimplemented( |
| 34 | + "the 0th axes MUST be pir::Int64Attribute")); |
| 35 | + for (size_t i = 0; i < axes_list.size(); ++i) { |
| 36 | + axes.push_back(axes_list[i].dyn_cast<pir::Int64Attribute>().data()); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + auto infer_flags_list = |
| 41 | + attributes.at("infer_flags").dyn_cast<pir::ArrayAttribute>().AsVector(); |
| 42 | + std::vector<int64_t> infer_flags; |
| 43 | + if (infer_flags_list.size() > 0) { |
| 44 | + PADDLE_ENFORCE_EQ(infer_flags_list[0].isa<pir::Int64Attribute>(), |
| 45 | + true, |
| 46 | + common::errors::Unimplemented( |
| 47 | + "the 0th infer_flags MUST be pir::Int64Attribute")); |
| 48 | + for (size_t i = 0; i < infer_flags_list.size(); ++i) { |
| 49 | + infer_flags.push_back( |
| 50 | + infer_flags_list[i].dyn_cast<pir::Int64Attribute>().data()); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + auto decrease_axis_list = |
| 55 | + attributes.at("decrease_axis").dyn_cast<pir::ArrayAttribute>().AsVector(); |
| 56 | + std::vector<int64_t> decrease_axis; |
| 57 | + if (decrease_axis_list.size() > 0) { |
| 58 | + PADDLE_ENFORCE_EQ(decrease_axis_list[0].isa<pir::Int64Attribute>(), |
| 59 | + true, |
| 60 | + common::errors::Unimplemented( |
| 61 | + "the 0th decrease_axis MUST be pir::Int64Attribute")); |
| 62 | + for (size_t i = 0; i < decrease_axis_list.size(); ++i) { |
| 63 | + decrease_axis.push_back( |
| 64 | + decrease_axis_list[i].dyn_cast<pir::Int64Attribute>().data()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + auto input = *(gcu_op_inputs[0][0]); |
| 69 | + |
| 70 | + auto starts_tensor = *(gcu_op_inputs[1][0]); |
| 71 | + PADDLE_ENFORCE_EQ(starts_tensor.IsConstant(), |
| 72 | + true, |
| 73 | + common::errors::PreconditionNotMet( |
| 74 | + "Input[1] starts_tensor is not a Constant.")); |
| 75 | + auto starts = starts_tensor.GetConstData<int64_t>(); |
| 76 | + |
| 77 | + auto ends_tensor = *(gcu_op_inputs[2][0]); |
| 78 | + PADDLE_ENFORCE_EQ(ends_tensor.IsConstant(), |
| 79 | + true, |
| 80 | + common::errors::PreconditionNotMet( |
| 81 | + "Input[1] ends_tensor is not a Constant.")); |
| 82 | + auto ends = ends_tensor.GetConstData<int64_t>(); |
| 83 | + |
| 84 | + auto rank = input.GetType().GetRank(); |
| 85 | + const std::vector<int64_t> &input_shapes = input.GetType().GetShape(); |
| 86 | + std::vector<int64_t> start_indices(rank, 0); |
| 87 | + std::vector<int64_t> limit_indices = input_shapes; |
| 88 | + for (size_t i = 0; i < axes.size(); ++i) { |
| 89 | + int dim = axes[i]; |
| 90 | + if (dim < 0) { |
| 91 | + dim += rank; |
| 92 | + } |
| 93 | + start_indices[dim] = |
| 94 | + starts[i] < 0 ? starts[i] + input_shapes[dim] : starts[i]; |
| 95 | + start_indices[dim] = std::max(start_indices[dim], 0L); |
| 96 | + start_indices[dim] = std::min(start_indices[dim], input_shapes[dim]); |
| 97 | + |
| 98 | + limit_indices[dim] = ends[i] < 0 ? ends[i] + input_shapes[dim] : ends[i]; |
| 99 | + limit_indices[dim] = std::min(limit_indices[dim], input_shapes[dim]); |
| 100 | + limit_indices[dim] = std::max(limit_indices[dim], 0L); |
| 101 | + } |
| 102 | + std::vector<int64_t> strides(rank, 1); |
| 103 | + |
| 104 | + auto slice = builder::Slice(input, start_indices, limit_indices, strides); |
| 105 | + |
| 106 | + if (decrease_axis.size() == 0) { |
| 107 | + return std::make_shared<GcuOp>(slice); |
| 108 | + } else { |
| 109 | + auto slice_shape = slice.GetType().GetShape(); |
| 110 | + std::vector<int64_t> new_shape; |
| 111 | + size_t iter = 0; |
| 112 | + for (int64_t i = 0; i < static_cast<int64_t>(slice_shape.size()); ++i) { |
| 113 | + if (iter < decrease_axis.size() && i == decrease_axis[iter]) { |
| 114 | + ++iter; |
| 115 | + } else { |
| 116 | + new_shape.emplace_back(slice_shape[i]); |
| 117 | + } |
| 118 | + } |
| 119 | + if (new_shape.empty()) { |
| 120 | + new_shape.emplace_back(1); |
| 121 | + } |
| 122 | + return std::make_shared<GcuOp>(builder::Reshape(slice, new_shape)); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +} // namespace custom_engine |
| 127 | + |
| 128 | +REGISTER_OP_TRANSLATOR(pd_op_slice, custom_engine::TranslateSlice) |
0 commit comments