old_contents: # Copyright (c) 2017 Sony Corporation. 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.
  
  #
  # *WARNING*
  # THIS FILE IS AUTO-GENERATED BY CODE GENERATOR.
  # 1. IMPLEMENT BACKWARD WRT INPUTS OF THE CORRESPONDING FUNCTION
  # 2. IMPLEMENT BACKWARD_FUNCTION_CLASS IF NECESSARY (see e.g., affine.py)
  # 3. UPDATE THE MAPPING IF NECESSARY (see function_backward_functions.py.tmpl)
  
  
  import nnabla.functions as F
  from .utils import force_list
  
  from .abs import abs_backward
  from .pow_scalar import pow_scalar_backward
  from .sum import sum_backward
  
  
  def norm_backward(inputs, p=None, axes=None, keep_dims=False):
      """
      Args:
        inputs (list of nn.Variable): Incomming grads/inputs to/of the forward function.
        kwargs (dict of arguments): Dictionary of the corresponding function arguments.
  
      Return:
        list of Variable: Return the gradients wrt inputs of the corresponding function.
      """
      dy = inputs[0]
      x0 = inputs[1]
  
      if p is None:
          p = 2.0
      axes = list(range(x0.ndim)) if axes is None else force_list(axes)
  
      # Recompute intermediate data of composite graph
      # abs -> pow -> sum (-> pow)
      y_abs = F.abs(x0)
      y_pow = F.pow_scalar(y_abs, p)
      y_sum = F.sum(y_pow, axes, keep_dims)
      # y = F.pow_scalar(x0_sum, 1.0 / p) # No need for grad calculation
  
      # Backprop composite functions
      # abs <- pow <- sum <- pow
      dx = pow_scalar_backward([dy, y_sum], 1.0 / p)
      dx = sum_backward([dx, y_pow], axes, keep_dims)
      dx = pow_scalar_backward([dx, y_abs], p)
      dx = abs_backward([dx, x0])
  
      return dx
  
new_contents: # Copyright (c) 2017 Sony Corporation. 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.
  
  #
  # *WARNING*
  # THIS FILE IS AUTO-GENERATED BY CODE GENERATOR.
  # 1. IMPLEMENT BACKWARD WRT INPUTS OF THE CORRESPONDING FUNCTION
  # 2. IMPLEMENT BACKWARD_FUNCTION_CLASS IF NECESSARY (see e.g., affine.py)
  # 3. UPDATE THE MAPPING IF NECESSARY (see function_backward_functions.py.tmpl)
  
  
  import nnabla.functions as F
  from .utils import force_list, no_grad
  
  
  def norm_backward(inputs, p=None, axes=None, keep_dims=False):
      """
      Args:
        inputs (list of nn.Variable): Incomming grads/inputs to/of the forward function.
        kwargs (dict of arguments): Dictionary of the corresponding function arguments.
  
      Return:
        list of Variable: Return the gradients wrt inputs of the corresponding function.
      """
      dy = inputs[0]
      x0 = inputs[1]
  
      if p is None:
          p = 2.0
      axes = list(range(x0.ndim)) if axes is None else force_list(axes)
  
      x_abs = F.abs(x0)
      x_pow = F.pow_scalar(x_abs, p)
      x_sum = F.sum(x_pow, axes, keepdims=True)
  
      # Add axis for mul2
      if not keep_dims:
          shape = list(x0.shape)
          for a in axes:
              shape[a] = 1
          dy = dy.reshape(shape)
  
      x_sign = no_grad(F.sign(x0))
      dx = dy * x_sum ** (1./p - 1.) * x_abs ** (p - 1.) * x_sign
  
      return dx
  
subject: Optimize norm_backward