Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #include "pad_api_call.h"
11 :
12 : #include <sstream>
13 : #include "attr_utils.h"
14 : #include "ascir_ops.h"
15 : #include "common_utils.h"
16 : #include "common/ge_common/debug/log.h"
17 : #include "graph/ascendc_ir/utils/asc_tensor_utils.h"
18 : #include "common/checker.h"
19 : #include "api_call/utils/api_call_factory.h"
20 : #include "codegen/expression_convert_struct.h"
21 :
22 : namespace codegen {
23 : using namespace std;
24 : using namespace af::ops;
25 : using namespace af::ascir_op;
26 : using namespace ascgen_utils;
27 :
28 : Status PadApiCall::Generate(const TPipe &tpipe, const std::vector<ascir::AxisId> ¤t_axis,
29 : const std::vector<std::reference_wrapper<const Tensor>> &inputs,
30 : const std::vector<std::reference_wrapper<const Tensor>> &outputs,
31 : std::string &result) const {
32 : auto x = inputs[0].get();
33 : auto y = outputs[0].get();
34 :
35 : // 获取tmp_buf复用TBuf的id
36 : int64_t life_time_axis_id = -1L;
37 : int64_t id = -1L;
38 : auto it = this->tmp_buf_id.find(life_time_axis_id);
39 : if (it != this->tmp_buf_id.end()) {
40 : id = it->second;
41 : }
42 :
43 : (void)RegisterBasicDumpParam(this->api_name_, inputs, outputs, {}, tpipe.tmp_buf.name + "_" + std::to_string(id));
44 : stringstream ss;
45 : stringstream axis_size_product;
46 0 : const size_t axis_num = y.vectorized_axis.size();
47 : for (size_t i = 0; i < axis_num; i++) {
48 : auto axis = tpipe.tiler.GetAxis(y.vectorized_axis[i]);
49 : if (i == (axis_num - 1UL)) {
50 : axis_size_product << axis.actual_size;
51 : } else {
52 : axis_size_product << axis.actual_size << " * ";
53 : }
54 : }
55 :
56 : string blk_align;
57 : GE_CHK_STATUS_RET(KernelUtils::BlkAlign(x.dtype, blk_align), "Codegen blk align failed in Pad api");
58 : uint32_t tiling_case_id = tpipe.tiler.GetTilingCaseId();
59 : std::string apiTilingDataString = "t->" + this->api_tiling_data_field + "_" + std::to_string(tiling_case_id);
60 : auto axis_pos = y.vectorized_axis_pos[axis_num - 1];
61 : if (tpipe.tiler.Size(y.vectorized_strides[axis_num - 2]) != tpipe.tiler.ActualSize(y.axis_size[axis_pos])) {
62 : GE_ASSERT_TRUE(id != -1L, "PadApiCall cannot find tmp buffer id to use.");
63 : ss << "if (" << tpipe.tiler.Size(y.vectorized_strides[axis_num - 2])
64 : << " != " << tpipe.tiler.ActualSize(y.axis_size[axis_pos]) << ") {" // 2表示倒数第二个维度
65 : << std::endl;
66 : ss << "AscendC::PadParams padParams = {0, static_cast<uint16_t>("
67 : << tpipe.tiler.Size(y.vectorized_strides[axis_num - 2]) << " - " << tpipe.tiler.ActualSize(y.axis_size[axis_pos])
68 : << "), 0};" // 2表示倒数第二个维度
69 : << std::endl;
70 : ss << "PadTiling apiTilingData = " << apiTilingDataString << ";" << std::endl;
71 : ss << "AscendC::Pad(" << y << "[" << tpipe.tiler.TensorVectorizedOffset(current_axis, y) << "], " << x << "["
72 : << tpipe.tiler.TensorVectorizedOffset(current_axis, x) << "], " << "padParams, " << tpipe.tmp_buf << "_"
73 : << std::to_string(id) << ", " << "apiTilingData);" << std::endl;
74 : ss << "} else {" << std::endl;
75 : ss << "AscendC::DataCopy(" << y << "[" << tpipe.tiler.TensorVectorizedOffset(current_axis, y) << "], " << x << "["
76 : << tpipe.tiler.TensorVectorizedOffset(current_axis, x) << "], " << blk_align << "(" << axis_size_product.str()
77 : << "));" << std::endl;
78 : ss << "}" << std::endl;
79 : } else {
80 : ss << "AscendC::DataCopy(" << y << "[" << tpipe.tiler.TensorVectorizedOffset(current_axis, y) << "], " << x << "["
81 : << tpipe.tiler.TensorVectorizedOffset(current_axis, x) << "], " << blk_align << "(" << axis_size_product.str()
82 : << "));" << std::endl;
83 : }
84 : result = ss.str();
85 : return af::SUCCESS;
86 : }
87 :
88 : Status PadApiCall::ParseAttr(const ascir::NodeView &node) {
89 : GE_ASSERT_SUCCESS(GetApiTilingFieldName(node, this->api_tiling_data_field));
90 : GELOGD("Get Pad api tiling field name success, field_name: %s\n", this->api_tiling_data_field.c_str());
91 : return af::SUCCESS;
92 : }
93 :
94 : static ApiCallRegister<PadApiCall> register_pad_api_call("PadApiCall");
95 : } // namespace codegen
|