From 8a7d9ab10afa5e26f213413c30f033fb50cf7fef Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 25 Nov 2021 19:40:00 +0800 Subject: [PATCH 1/7] FastGeluV2 --- tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc | 39 +++++++++++++++++++ .../npu_bridge/estimator/npu_aicore_ops.py | 13 +++++++ 2 files changed, 52 insertions(+) create mode 100644 tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc diff --git a/tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc b/tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc new file mode 100644 index 000000000..d3394d990 --- /dev/null +++ b/tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc @@ -0,0 +1,39 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2021. 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. + */ + +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/framework/register_types.h" + +namespace tensorflow { +class FastGeluV2Op : public OpKernel { +public: + explicit FastGeluV2Op(OpKernelConstruction *context) : OpKernel(context) { + LOG(INFO) << "new FastGeluV2Op"; + } + ~FastGeluV2Op() { + LOG(INFO) << "del FastGeluV2Op"; + } + void Compute(OpKernelContext *context) override { + LOG(INFO) << "FastGeluV2Op Compute"; + } + bool IsExpensive() override { + LOG(INFO) << "in FastGeluV2 IsExpensive"; + return false; } +}; + +REGISTER_KERNEL_BUILDER(Name("FusedFastGeluV2").Device(DEVICE_CPU), FastGeluV2Op); +} // namespace tensorflow + diff --git a/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py b/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py index 97f0274cd..795008ddb 100644 --- a/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py +++ b/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py @@ -54,6 +54,19 @@ def _fast_gelu_grad(op, grad): """ return [npu_aicore_ops.fast_gelu_grad(grad, op.inputs[0])] # List of one Tensor, since we have one input +@ops.RegisterGradient("FastGeluV2") +def _fast_gelu_v2_grad(op, grad): + """The gradient for `fast_gelu_v2`. + + Args: + op: The `fast_gelu_v2` `Operation` that we are differentiating, which we can use + to find the inputs and outputs of the original op. + grad: Gradient with respect to the output of the `fast_gelu_v2` op. + + Returns: + Gradients with respect to the input of `fast_gelu_v2`. + """ + return [npu_aicore_ops.fast_gelu_v2_grad(grad, op.inputs[0])] # List of one Tensor, since we have one input def centralization(x, axes, name=None): """ -- Gitee From 93064e345600b8a3caa78c117816de282620eb10 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 26 Nov 2021 11:14:28 +0800 Subject: [PATCH 2/7] update ops --- .../python/npu_bridge/estimator/npu_aicore_ops.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py b/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py index 795008ddb..a837bd154 100644 --- a/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py +++ b/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py @@ -56,17 +56,17 @@ def _fast_gelu_grad(op, grad): @ops.RegisterGradient("FastGeluV2") def _fast_gelu_v2_grad(op, grad): - """The gradient for `fast_gelu_v2`. + """The gradient for `fast_gelu_v2`. - Args: + Args: op: The `fast_gelu_v2` `Operation` that we are differentiating, which we can use to find the inputs and outputs of the original op. grad: Gradient with respect to the output of the `fast_gelu_v2` op. - Returns: + Returns: Gradients with respect to the input of `fast_gelu_v2`. - """ - return [npu_aicore_ops.fast_gelu_v2_grad(grad, op.inputs[0])] # List of one Tensor, since we have one input + """ + return [npu_aicore_ops.fast_gelu_v2_grad(grad, op.inputs[0])] # List of one Tensor, since we have one input def centralization(x, axes, name=None): """ -- Gitee From eec07f98603b33bc69433f4e2d3a400460781221 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 27 Nov 2021 11:12:22 +0800 Subject: [PATCH 3/7] update npu_aicore_ops --- tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py b/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py index a837bd154..c97b20d0c 100644 --- a/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py +++ b/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py @@ -35,7 +35,7 @@ from tensorflow.python.framework import device from npu_bridge.estimator.npu.npu_common import NPUBasics from npu_bridge.helper import helper -npu_aicore_ops = helper.get_gen_ops(); +npu_aicore_ops = helper.get_gen_ops() DEFAULT_GRAPH_SEED = 87654321 _MAXINT32 = 2**31 - 1 @@ -54,6 +54,7 @@ def _fast_gelu_grad(op, grad): """ return [npu_aicore_ops.fast_gelu_grad(grad, op.inputs[0])] # List of one Tensor, since we have one input + @ops.RegisterGradient("FastGeluV2") def _fast_gelu_v2_grad(op, grad): """The gradient for `fast_gelu_v2`. -- Gitee From 5d4c8a64decc6173fa59c8c05c69d7180a6239f2 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 27 Nov 2021 16:50:01 +0800 Subject: [PATCH 4/7] update npu_aicore_ops.cc --- tf_adapter/ops/aicore/npu_aicore_ops.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tf_adapter/ops/aicore/npu_aicore_ops.cc b/tf_adapter/ops/aicore/npu_aicore_ops.cc index c56e4f9e4..b030e6eea 100644 --- a/tf_adapter/ops/aicore/npu_aicore_ops.cc +++ b/tf_adapter/ops/aicore/npu_aicore_ops.cc @@ -38,6 +38,12 @@ REGISTER_OP("FastGeluGrad") .Attr("T: realnumbertype") .SetShapeFn(tensorflow::shape_inference::MergeBothInputsShapeFn); +REGISTER_OP("FastGeluV2") + .Input("features: T") + .Output("activations: T") + .Attr("T: realnumbertype") + .SetShapeFn(tensorflow::shape_inference::UnchangedShape); + REGISTER_OP("DynamicGruV2") .Input("x: float16") .Input("weight_input: float16") -- Gitee From 0b6f397d07a6261d655a68cd13912ca8d823a017 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 30 Nov 2021 11:42:23 +0800 Subject: [PATCH 5/7] update npu_aicore_ops.py --- tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py b/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py index c97b20d0c..3102fe8a8 100644 --- a/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py +++ b/tf_adapter/python/npu_bridge/estimator/npu_aicore_ops.py @@ -69,6 +69,7 @@ def _fast_gelu_v2_grad(op, grad): """ return [npu_aicore_ops.fast_gelu_v2_grad(grad, op.inputs[0])] # List of one Tensor, since we have one input + def centralization(x, axes, name=None): """ centralization op -- Gitee From 4c1b84c4f0e150345b6c356cac7cf2fc8618ba26 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 30 Nov 2021 20:01:19 +0800 Subject: [PATCH 6/7] update fast gelu v2 ops.cc --- tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc b/tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc index d3394d990..8dfdde147 100644 --- a/tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc +++ b/tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc @@ -34,6 +34,6 @@ public: return false; } }; -REGISTER_KERNEL_BUILDER(Name("FusedFastGeluV2").Device(DEVICE_CPU), FastGeluV2Op); +REGISTER_KERNEL_BUILDER(Name("FastGeluV2").Device(DEVICE_CPU), FastGeluV2Op); } // namespace tensorflow -- Gitee From 54868ecd1ae6c65e472c918578aa515557298f95 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 30 Nov 2021 20:19:22 +0800 Subject: [PATCH 7/7] del fast gelu v2 ops.cc --- tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc | 39 ------------------- 1 file changed, 39 deletions(-) delete mode 100644 tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc diff --git a/tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc b/tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc deleted file mode 100644 index 8dfdde147..000000000 --- a/tf_adapter/kernels/aicore/fast_gelu_v2_ops.cc +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) Huawei Technologies Co., Ltd. 2021. 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. - */ - -#include "tensorflow/core/framework/op_kernel.h" -#include "tensorflow/core/framework/register_types.h" - -namespace tensorflow { -class FastGeluV2Op : public OpKernel { -public: - explicit FastGeluV2Op(OpKernelConstruction *context) : OpKernel(context) { - LOG(INFO) << "new FastGeluV2Op"; - } - ~FastGeluV2Op() { - LOG(INFO) << "del FastGeluV2Op"; - } - void Compute(OpKernelContext *context) override { - LOG(INFO) << "FastGeluV2Op Compute"; - } - bool IsExpensive() override { - LOG(INFO) << "in FastGeluV2 IsExpensive"; - return false; } -}; - -REGISTER_KERNEL_BUILDER(Name("FastGeluV2").Device(DEVICE_CPU), FastGeluV2Op); -} // namespace tensorflow - -- Gitee