已合并
add scalar convert for torch.pow #4489
nomiz创建于 3月18日
add scalar convert for torch.pow #4489
已合并
从已删除 :master合入到Ascend/op-pluginmaster
共 2 个文件变更+108-15
| @@ -3890,21 +3890,10 @@ official: | |||
| 3890 | - func: pow.Tensor_Tensor(Tensor self, Tensor exponent) -> Tensor | 3890 | - func: pow.Tensor_Tensor(Tensor self, Tensor exponent) -> Tensor |
| 3891 | acl_op: all_version | 3891 | acl_op: all_version |
| 3892 | op_api: all_version | 3892 | op_api: all_version |
| 3893 | - gen_opapi: | ||
| 3894 | - out: | ||
| 3895 | - size: broadcast_ops_npu_output_size(self, exponent) | ||
| 3896 | - dtype: at::result_type(self, exponent) | ||
| 3897 | - name: self, exponent | ||
| 3898 | - exec: aclnnPowTensorTensor | ||
| 3899 | 3893 | ||
| 3900 | - func: pow.Tensor_Tensor_out(Tensor self, Tensor exponent, *, Tensor(a!) out) -> Tensor(a!) | 3894 | - func: pow.Tensor_Tensor_out(Tensor self, Tensor exponent, *, Tensor(a!) out) -> Tensor(a!) |
| 3901 | acl_op: all_version | 3895 | acl_op: all_version |
| 3902 | op_api: all_version | 3896 | op_api: all_version |
| 3903 | - gen_opapi: | ||
| 3904 | - out: | ||
| 3905 | - size: broadcast_ops_npu_output_size(self, exponent) | ||
| 3906 | - name: self, exponent | ||
| 3907 | - exec: aclnnPowTensorTensor | ||
| 3908 | 3897 | ||
| 3909 | - func: pow_.Scalar(Tensor(a!) self, Scalar exponent) -> Tensor(a!) | 3898 | - func: pow_.Scalar(Tensor(a!) self, Scalar exponent) -> Tensor(a!) |
| 3910 | acl_op: all_version | 3899 | acl_op: all_version |
| @@ -3915,10 +3904,6 @@ official: | |||
| 3915 | - func: pow_.Tensor(Tensor(a!) self, Tensor exponent) -> Tensor(a!) | 3904 | - func: pow_.Tensor(Tensor(a!) self, Tensor exponent) -> Tensor(a!) |
| 3916 | acl_op: all_version | 3905 | acl_op: all_version |
| 3917 | op_api: all_version | 3906 | op_api: all_version |
| 3918 | - gen_opapi: | ||
| 3919 | - self: | ||
| 3920 | - name: self, exponent | ||
| 3921 | - exec: aclnnInplacePowTensorTensor | ||
| 3922 | 3907 | ||
| 3923 | - func: polar(Tensor abs, Tensor angle) -> Tensor | 3908 | - func: polar(Tensor abs, Tensor angle) -> Tensor |
| 3924 | acl_op: all_version | 3909 | acl_op: all_version |
| @@ -0,0 +1,108 @@ | |||
| 1 | +// Copyright (c) 2023 Huawei Technologies Co., Ltd | ||
| 2 | +// Copyright (c) 2019, Facebook CORPORATION. | ||
| 3 | +// All rights reserved. | ||
| 4 | +// | ||
| 5 | +// Licensed under the BSD 3-Clause License (the "License"); | ||
| 6 | +// you may not use this file except in compliance with the License. | ||
| 7 | +// You may obtain a copy of the License at | ||
| 8 | +// | ||
| 9 | +// https://opensource.org/licenses/BSD-3-Clause | ||
| 10 | +// | ||
| 11 | +// Unless required by applicable law or agreed to in writing, software | ||
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, | ||
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 14 | +// See the License for the specific language governing permissions and | ||
| 15 | +// limitations under the License. | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +namespace op_api { | ||
| 23 | +using npu_preparation = at_npu::native::OpPreparation; | ||
| 24 | + | ||
| 25 | +static at::Tensor self_tensor_to_device(const at::Tensor &tensor, const at::ScalarType result_type, | ||
| 26 | + const c10::Device device) | ||
| 27 | +{ | ||
| 28 | + if (npu_preparation::is_scalar_wrapped_to_tensor(tensor) || | ||
| 29 | + (tensor.dim() == 0 && !torch_npu::utils::is_npu(tensor))) { | ||
| 30 | + at::Scalar scalar = tensor.item(); | ||
| 31 | + return npu_preparation::copy_scalar_to_device(scalar, result_type, device); | ||
| 32 | + } | ||
| 33 | + return tensor; | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +static at::Tensor pow_dest_output(const at::Tensor &self, const at::Tensor &exponent) | ||
| 37 | +{ | ||
| 38 | + bool isSelfWrapped = npu_preparation::is_scalar_wrapped_to_tensor(self); | ||
| 39 | + return isSelfWrapped ? exponent : self; | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +static at::Tensor &pow_out_npu_nocheck(const at::Tensor &self, const at::Tensor &exponent, at::Tensor &out) | ||
| 43 | +{ | ||
| 44 | + if (exponent.dim() == 0 && !torch_npu::utils::is_npu(exponent)) { | ||
| 45 | + c10::Scalar exponent_scalar = exponent.item(); | ||
| 46 | + EXEC_NPU_CMD(aclnnPowTensorScalar, self, exponent_scalar, out); | ||
| 47 | + } else { | ||
| 48 | + EXEC_NPU_CMD(aclnnPowTensorTensor, self, exponent, out); | ||
| 49 | + } | ||
| 50 | + return out; | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +static at::Tensor &inplace_pow_out_npu_nocheck(at::Tensor &self, const at::Tensor &exponent) | ||
| 54 | +{ | ||
| 55 | + if (exponent.dim() == 0 && !torch_npu::utils::is_npu(exponent)) { | ||
| 56 | + c10::Scalar exponent_scalar = exponent.item(); | ||
| 57 | + EXEC_NPU_CMD(aclnnInplacePowTensorScalar, self, exponent_scalar); | ||
| 58 | + } else { | ||
| 59 | + EXEC_NPU_CMD(aclnnInplacePowTensorTensor, self, exponent); | ||
| 60 | + } | ||
| 61 | + return self; | ||
| 62 | +} | ||
| 63 | + | ||
| 64 | +at::Tensor pow(const at::Tensor &self, const at::Tensor &exponent) | ||
| 65 | +{ | ||
| 66 | + DO_COMPATIBILITY(aclnnPowTensorTensor, acl_op::pow(self, exponent)); | ||
| 67 | + std::vector<at::Tensor> tensor_list = {self, exponent}; | ||
| 68 | + auto maybe_names = op_plugin::utils::compute_names_npu(tensor_list); | ||
| 69 | + // calculate the output size | ||
| 70 | + at::Tensor output_tensor = pow_dest_output(self, exponent); | ||
| 71 | + auto output_size = op_infer::broadcast_ops_npu_output_size(self, exponent); | ||
| 72 | + at::ScalarType result_type = at::native::result_type(self, exponent); | ||
| 73 | + at::Tensor self_cp = self_tensor_to_device(self, result_type, output_tensor.device()); | ||
| 74 | + // construct the output tensor of the NPU | ||
| 75 | + at::Tensor out = npu_preparation::apply_tensor_without_format(output_size, | ||
| 76 | + output_tensor.options().dtype(result_type)); | ||
| 77 | + // calculate the output result of the NPU | ||
| 78 | + pow_out_npu_nocheck(self_cp, exponent, out); | ||
| 79 | + at::namedinference::propagate_names_if_nonempty(out, maybe_names); | ||
| 80 | + return out; | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +at::Tensor &pow_out(const at::Tensor &self, const at::Tensor &exponent, at::Tensor &out) | ||
| 84 | +{ | ||
| 85 | + DO_COMPATIBILITY(aclnnPowTensorTensor, acl_op::pow_out(self, exponent, out)); | ||
| 86 | + std::vector<at::Tensor> tensor_list = {self, exponent}; | ||
| 87 | + auto maybe_names = op_plugin::utils::compute_names_npu(tensor_list); | ||
| 88 | + // calculate the output size | ||
| 89 | + auto output_size = op_infer::broadcast_ops_npu_output_size(self, exponent); | ||
| 90 | + at::ScalarType result_type = out.scalar_type(); | ||
| 91 | + at::Tensor self_cp = self_tensor_to_device(self, result_type, out.device()); | ||
| 92 | + npu_preparation::check_tensor({self, exponent}, out, result_type, output_size); | ||
| 93 | + // calculate the output result of the NPU | ||
| 94 | + pow_out_npu_nocheck(self_cp, exponent, out); | ||
| 95 | + at::namedinference::propagate_names_if_nonempty(out, maybe_names); | ||
| 96 | + return out; | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +at::Tensor &pow_(at::Tensor &self, const at::Tensor &exponent) | ||
| 100 | +{ | ||
| 101 | + DO_COMPATIBILITY(aclnnInplacePowTensorTensor, acl_op::pow_(self, exponent)); | ||
| 102 | + std::vector<at::Tensor> tensor_list = {self, exponent}; | ||
| 103 | + auto maybe_names = op_plugin::utils::compute_names_npu(tensor_list); | ||
| 104 | + inplace_pow_out_npu_nocheck(self, exponent); | ||
| 105 | + at::namedinference::propagate_names_if_nonempty(self, maybe_names); | ||
| 106 | + return self; | ||
| 107 | +} | ||
| 108 | +} | ||