class_docstr: A variant of the Adam optimizer that handles sparse updates more efficiently.
  
  The original Adam algorithm maintains two moving-average accumulators for
  each trainable variable; the accumulators are updated at every step.
  In this variant, only moments that show up in the gradient get updated,
  and only those portions of the gradient get applied to the parameters.
  Compared with the original Adam optimizer, it can provide large improvements in
  model training throughput for some applications.
class_docstr_tok: ["A","variant","of","the","Adam","optimizer","that","handles","sparse","updates","more","efficiently",".","The","original","Adam","algorithm","maintains","two","moving-average","accumulators","for","each","trainable","variable",";","the","accumulators","are","updated","at","every","step",".","In","this","variant",",","only","moments","that","show","up","in","the","gradient","get","updated",",","and","only","those","portions","of","the","gradient","get","applied","to","the","parameters",".","Compared","with","the","original","Adam","optimizer",",","it","can","provide","large","improvements","in","model","training","throughput","for","some","applications","."]
code: def _resource_apply_sparse(
      self, grad, var, indices, apply_state=None
  ):
      var_device, var_dtype = var.device, var.dtype.base_dtype
      temp_coeff = (apply_state or {}).get((var_device, var_dtype))
      coefficients = temp_coeff or self._fallback_apply_state(
          var_device, var_dtype
      )
  
      import tensorflow as tf
  
      with tf.name_scope("update_sparse_mt"):
          m = self.get_slot(var, "m")
          m_scaled_g_values = grad * coefficients["one_minus_beta_1_t"]
          m_sparse = tf.gather(m, indices=indices)
  
          m_sparse_scaled = m_sparse * (coefficients["beta_1_t"] - 1)
  
          m_t = self._resource_scatter_add(
              m, indices, m_scaled_g_values + m_sparse_scaled
          )
          m_t_sparse = (
              m_sparse * coefficients["beta_1_t"] + m_scaled_g_values
          )
  
      with tf.name_scope("update_sparse_vt"):
          v = self.get_slot(var, "v")
          v_scaled_g_values = (grad * grad) * coefficients[
              "one_minus_beta_2_t"
          ]
          v_sparse = tf.gather(v, indices=indices)
          v_sparse_scaled = v_sparse * (coefficients["beta_2_t"] - 1)
  
          v_t = self._resource_scatter_add(
              v, indices, v_scaled_g_values + v_sparse_scaled
          )
  
          v_t_sparse = (
              v_sparse * coefficients["beta_2_t"] + v_scaled_g_values
          )
  
      if not self.amsgrad:
          with tf.name_scope("update_sparse_var"):
              v_sqrt_sparse = tf.sqrt(v_t_sparse)
              var_update = self._resource_scatter_add(
                  var,
                  indices,
                  -1
                  * coefficients["lr"]
                  * m_t_sparse
                  / (v_sqrt_sparse + coefficients["epsilon"]),
              )
              return tf.group(*[var_update, m_t, v_t])
      else:
          invalidInputError(False, "do not support amsgrad for now")
          v_hat = self.get_slot(var, "vhat")
          v_hat_t = math_ops.maximum(v_hat, v_t)
          with ops.control_dependencies([v_hat_t]):
              v_hat_t = state_ops.assign(
                  v_hat, v_hat_t, use_locking=self._use_locking
              )
          v_hat_sqrt = math_ops.sqrt(v_hat_t)
          var_update = state_ops.assign_sub(
              var,
              coefficients["lr"]
              * m_t
              / (v_hat_sqrt + coefficients["epsilon"]),
              use_locking=self._use_locking,
          )
          return control_flow_ops.group(
              *[var_update, m_t, v_t, v_hat_t]
          )
  
file_path: BigDL/python/nano/src/bigdl/nano/tf/optimizers/sparse_adam.py
imports: import tensorflow.compat.v2
  from keras import backend_config
  from tensorflow.python.util.tf_export import keras_export
  import tensorflow
  from bigdl.nano.utils.common import invalidInputError
  from bigdl.nano.utils.tf import KERAS_VERSION_LESS_2_9
  from tensorflow.keras.optimizers import Adam
label_desc: Use image node_id 2 for calling the SparseAdam obj's underlying member method code with example usage: obj._resource_apply_sparse(grad, var, indices, apply_state) and returns: control_flow_ops, tf