• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 Huawei Technologies Co., Ltd
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# ============================================================================
15import numpy as np
16from mindspore import Tensor
17from mindspore.ops import prim_attr_register, PrimitiveWithInfer
18
19# y = x^2
20class CusSquare(PrimitiveWithInfer):
21    """CusSquare definition"""
22    from square_impl import CusSquareImpl
23
24    @prim_attr_register
25    def __init__(self):
26        """init CusSquare"""
27        self.init_prim_io_names(inputs=['x'], outputs=['y'])
28
29    def vm_impl(self, x):
30        x = x.asnumpy()
31        return Tensor(np.multiply(x, x))
32
33    def infer_shape(self, data_shape):
34        return data_shape
35
36    def infer_dtype(self, data_dtype):
37        return data_dtype
38
39    def get_bprop(self):
40        def bprop(data, out, dout):
41            gradient = data * 2
42            dx = gradient * dout
43            return (dx,)
44        return bprop
45