|
| 1 | +// Copyright (c) MLLM Team. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include "mllm/backends/ascend/ops/AscendSliceOp.hpp" |
| 5 | + |
| 6 | +#include <acl/acl.h> |
| 7 | +#include <atb/atb_infer.h> |
| 8 | +#include <atb/types.h> |
| 9 | +#include <atb/utils.h> |
| 10 | +#include <atb/infer_op_params.h> |
| 11 | + |
| 12 | +#include "mllm/utils/Common.hpp" |
| 13 | +#include "mllm/core/DataTypes.hpp" |
| 14 | +#include "mllm/core/Tensor.hpp" |
| 15 | +#include "mllm/backends/ascend/memory/AscendMemoryManager.hpp" |
| 16 | +#include "mllm/backends/ascend/AscendCommon.hpp" |
| 17 | + |
| 18 | +namespace mllm::ascend { |
| 19 | + |
| 20 | +AscendSliceOp::AscendSliceOp(const aops::SliceOpOptions& options) : aops::SliceOp(options) {} |
| 21 | + |
| 22 | +void AscendSliceOp::setup(const std::vector<Tensor>& inputs, std::vector<Tensor>& outputs) { |
| 23 | + BaseOp::setup(inputs, outputs); |
| 24 | +} |
| 25 | + |
| 26 | +void AscendSliceOp::reshape(const std::vector<Tensor>& inputs, std::vector<Tensor>& outputs) { |
| 27 | + auto& input = inputs[0]; |
| 28 | + auto shape = input.shape(); |
| 29 | + auto slice_index = options().indices_; |
| 30 | + |
| 31 | + MLLM_RT_ASSERT_EQ(slice_index.size(), shape.size()); |
| 32 | + |
| 33 | + std::vector<int> out_shape; |
| 34 | + for (size_t i = 0; i < shape.size(); ++i) { |
| 35 | + const auto& pair = slice_index[i]; |
| 36 | + int32_t start = pair.start_; |
| 37 | + int32_t end = pair.end_; |
| 38 | + |
| 39 | + if (start == kAll) { start = 0; } |
| 40 | + if (end == kAll) { end = shape[i]; } |
| 41 | + |
| 42 | + if (start < 0) { start = start + shape[i]; } |
| 43 | + if (end < 0) { end = end + shape[i]; } |
| 44 | + |
| 45 | + start = std::max(0, std::min(start, static_cast<int>(shape[i]))); |
| 46 | + end = std::max(0, std::min(end, static_cast<int>(shape[i]))); |
| 47 | + |
| 48 | + int len = std::max(0, end - start); |
| 49 | + out_shape.push_back(len); |
| 50 | + } |
| 51 | + |
| 52 | + outputs.emplace_back(Tensor::empty(out_shape, input.dtype(), input.device())); |
| 53 | +} |
| 54 | + |
| 55 | +void AscendSliceOp::forward(const std::vector<Tensor>& inputs, std::vector<Tensor>& outputs) { |
| 56 | + atb::infer::SliceParam param; |
| 57 | + auto& input = inputs[0]; |
| 58 | + auto shape = input.shape(); |
| 59 | + auto slice_index = options().indices_; |
| 60 | + |
| 61 | + for(size_t i=0; i<shape.size(); ++i) { |
| 62 | + int32_t start = slice_index[i].start_; |
| 63 | + int32_t end = slice_index[i].end_; |
| 64 | + int32_t dim_size = shape[i]; |
| 65 | + |
| 66 | + if (start == kAll) start = 0; |
| 67 | + if (end == kAll) end = dim_size; |
| 68 | + |
| 69 | + if (start < 0) start += dim_size; |
| 70 | + if (end < 0) end += dim_size; |
| 71 | + |
| 72 | + start = std::max(0, std::min(start, dim_size)); |
| 73 | + end = std::max(0, std::min(end, dim_size)); |
| 74 | + |
| 75 | + param.offsets.push_back(start); |
| 76 | + param.size.push_back(std::max(0, end - start)); |
| 77 | + } |
| 78 | + |
| 79 | + atb::Operation* op = nullptr; |
| 80 | + auto st = atb::CreateOperation(param, &op); |
| 81 | + if (st != atb::NO_ERROR || op == nullptr) { |
| 82 | + MLLM_ERROR_EXIT(ExitCode::kAscendError, "ATB CreateOperation(Slice) failed, status={}", static_cast<int>(st)); |
| 83 | + } |
| 84 | + |
| 85 | + atb::Context* atb_ctx = getGlobalAtbContext(); |
| 86 | + |
| 87 | + atb::SVector<atb::Tensor> inTensors; |
| 88 | + std::vector<atb::Tensor> atb_inputs(inputs.size()); |
| 89 | + for (size_t i = 0; i < inputs.size(); ++i) { |
| 90 | + fillAtbTensor(inputs[i], atb_inputs[i]); |
| 91 | + inTensors.push_back(atb_inputs[i]); |
| 92 | + } |
| 93 | + |
| 94 | + atb::Tensor atb_output; |
| 95 | + fillAtbTensor(outputs[0], atb_output); |
| 96 | + atb::SVector<atb::Tensor> outTensors; |
| 97 | + outTensors.push_back(atb_output); |
| 98 | + |
| 99 | + atb::VariantPack vp; |
| 100 | + vp.inTensors = inTensors; |
| 101 | + vp.outTensors = outTensors; |
| 102 | + |
| 103 | + uint64_t workspaceSize = 0; |
| 104 | + st = op->Setup(vp, workspaceSize, atb_ctx); |
| 105 | + if (st != atb::NO_ERROR) { |
| 106 | + MLLM_ERROR_EXIT(ExitCode::kAscendError, "ATB SliceOp Setup failed, status={}", static_cast<int>(st)); |
| 107 | + } |
| 108 | + |
| 109 | + void* workspace = nullptr; |
| 110 | + int workspace_block_id = -1; |
| 111 | + if (workspaceSize > 0) { |
| 112 | + auto& mem_mgr = getAscendMemoryManager(); |
| 113 | + mem_mgr.allocateBlock(static_cast<uint32_t>(workspaceSize), workspace_block_id); |
| 114 | + mem_mgr.getBlockPtr(workspace_block_id, workspace); |
| 115 | + } |
| 116 | + |
| 117 | + { |
| 118 | + ASCEND_TIME_SCOPE("AscendSliceOp::forward"); |
| 119 | + st = op->Execute(vp, reinterpret_cast<uint8_t*>(workspace), workspaceSize, atb_ctx); |
| 120 | + } |
| 121 | + |
| 122 | + if (st != atb::NO_ERROR) { |
| 123 | + MLLM_ERROR_EXIT(ExitCode::kAscendError, "ATB SliceOp Execute failed, status={}", static_cast<int>(st)); |
| 124 | + } |
| 125 | + |
| 126 | + syncGlobalAtbStream(); |
| 127 | + |
| 128 | + if (workspace_block_id != -1) { |
| 129 | + auto& mem_mgr = getAscendMemoryManager(); |
| 130 | + mem_mgr.freeBlock(workspace_block_id); |
| 131 | + } |
| 132 | + |
| 133 | + atb::DestroyOperation(op); |
| 134 | +} |
| 135 | + |
| 136 | +} // namespace mllm::ascend |
0 commit comments