code: def update_loss_scaling(
      is_overall_finite,
      prev_loss_scaling,
      num_good_steps,
      num_bad_steps,
      incr_every_n_steps,
      decr_every_n_nan_or_inf,
      incr_ratio,
      decr_ratio,
  ):
      """
      Update loss scaling according to overall gradients. If all gradients is
      finite after incr_every_n_steps, loss scaling will increase by incr_ratio.
      Otherwisw, loss scaling will decrease by decr_ratio after
      decr_every_n_nan_or_inf steps and each step some gradients are infinite.
      Args:
          is_overall_finite (Variable): A boolean variable indicates whether
                                       all gradients are finite.
          prev_loss_scaling (Variable): Previous loss scaling.
          num_good_steps (Variable): A variable accumulates good steps in which
                                     all gradients are finite.
          num_bad_steps (Variable): A variable accumulates bad steps in which
                                    some gradients are infinite.
          incr_every_n_steps (Variable): A variable represents increasing loss
                                         scaling every n consecutive steps with
                                         finite gradients.
          decr_every_n_nan_or_inf (Variable): A variable represents decreasing
                                              loss scaling every n accumulated
                                              steps with nan or inf gradients.
          incr_ratio(float): The multiplier to use when increasing the loss
                             scaling.
          decr_ratio(float): The less-than-one-multiplier to use when decreasing
                             loss scaling.
      """
      zero_steps = fluid.layers.fill_constant(
          shape=[1], dtype="int32", value=0
      )
      with fluid.layers.Switch() as switch:
          with switch.case(is_overall_finite):
              should_incr_loss_scaling = fluid.layers.less_than(
                  incr_every_n_steps, num_good_steps + 1
              )
              with fluid.layers.Switch() as switch1:
                  with switch1.case(should_incr_loss_scaling):
                      new_loss_scaling = prev_loss_scaling * incr_ratio
                      loss_scaling_is_finite = fluid.layers.isfinite(
                          new_loss_scaling
                      )
                      with fluid.layers.Switch() as switch2:
                          with switch2.case(loss_scaling_is_finite):
                              fluid.layers.assign(
                                  new_loss_scaling, prev_loss_scaling
                              )
                          with switch2.default():
                              pass
                      fluid.layers.assign(zero_steps, num_good_steps)
                      fluid.layers.assign(zero_steps, num_bad_steps)
  
                  with switch1.default():
                      fluid.layers.increment(num_good_steps)
                      fluid.layers.assign(zero_steps, num_bad_steps)
  
          with switch.default():
              should_decr_loss_scaling = fluid.layers.less_than(
                  decr_every_n_nan_or_inf, num_bad_steps + 1
              )
              with fluid.layers.Switch() as switch3:
                  with switch3.case(should_decr_loss_scaling):
                      new_loss_scaling = prev_loss_scaling * decr_ratio
                      static_loss_scaling = fluid.layers.fill_constant(
                          shape=[1], dtype="float32", value=1.0
                      )
                      less_than_one = fluid.layers.less_than(
                          new_loss_scaling, static_loss_scaling
                      )
                      with fluid.layers.Switch() as switch4:
                          with switch4.case(less_than_one):
                              fluid.layers.assign(
                                  static_loss_scaling, prev_loss_scaling
                              )
                          with switch4.default():
                              fluid.layers.assign(
                                  new_loss_scaling, prev_loss_scaling
                              )
                      fluid.layers.assign(zero_steps, num_good_steps)
                      fluid.layers.assign(zero_steps, num_bad_steps)
                  with switch3.default():
                      fluid.layers.assign(zero_steps, num_good_steps)
                      fluid.layers.increment(num_bad_steps)
  
file_path: PGL/ogb_examples/linkproppred/ogbl-ppa/utils/fp16.py
imports: from __future__ import print_function
  import paddle
  import paddle.fluid
label_desc: Use image node_id 6 for calling a global function with example usage: update_loss_scaling(is_overall_finite, prev_loss_scaling, num_good_steps, num_bad_steps, incr_every_n_steps, decr_every_n_nan_or_inf, incr_ratio, decr_ratio) without return types