code: def test_cp_lstsq_grad(shape, rank):
      """Validate the gradient calculation between a CP and dense tensor."""
      cp_tensor = ivy.random_cp(shape, rank, normalise_factors=False)
  
      # If we're taking the gradient of comparison with self it should be 0
      cp_grad = ivy.CPTensor.cp_lstsq_grad(
          cp_tensor, ivy.CPTensor.cp_to_tensor(cp_tensor)
      )
      assert ivy.CPTensor.cp_norm(cp_grad) <= 10e-5
  
      # Check that we can solve for a direction of descent
      dense = ivy.random_cp(
          shape, rank, full=True, normalise_factors=False
      )
      cost_before = ivy.sqrt(
          ivy.sum(
              ivy.square(ivy.CPTensor.cp_to_tensor(cp_tensor) - dense)
          )
      )
  
      cp_grad = ivy.CPTensor.cp_lstsq_grad(cp_tensor, dense)
      cp_new = ivy.CPTensor(cp_tensor)
      for ii in range(len(shape)):
          cp_new.factors[ii] = (
              cp_tensor.factors[ii] - 1e-3 * cp_grad.factors[ii]
          )
  
      cost_after = ivy.sqrt(
          ivy.sum(ivy.square(ivy.CPTensor.cp_to_tensor(cp_new) - dense))
      )
      assert cost_before > cost_after
  
file_path: ivy/ivy_tests/test_ivy/test_misc/test_factorized_tensor/test_cp_tensor.py
imports: import ivy
  import numpy
  import pytest
label_desc: Use image node_id 2 for calling a global function with example usage: test_cp_lstsq_grad(shape, rank) without return types