code: def nanmedian(input, dim=None, keepdim=False, *, out=None):
      if dim is None:
          flattened_input = ivy.flatten(input)
          sorted_input = ivy.sort(flattened_input)
          nonnan_index = int(
              sorted_input.shape[0] - ivy.isnan(sorted_input).sum()
          )
          return sorted_input[(nonnan_index - 1) // 2]
  
      nanmedian_tuple = namedtuple("nanmedian", ["values", "indices"])
  
      if input.ndim == 0:
          result = nanmedian_tuple(input, ivy.array(0))
      else:
          sorted_indices = ivy.argsort(input, axis=dim)
          nonnan_index = (
              sorted_indices.shape[dim]
              - ivy.isnan(input).sum(axis=1)
              - 1
          ) // 2
          nonnan_index = ivy.expand_dims(nonnan_index, axis=1)
          nanmedian_indices = ivy.gather_nd(
              sorted_indices, nonnan_index, batch_dims=1
          )
          nanmedian_values = ivy.take_along_axis(
              input, ivy.expand_dims(nanmedian_indices, axis=dim), dim
          ).squeeze(axis=dim)
  
          if keepdim:
              nanmedian_values = ivy.expand_dims(
                  nanmedian_values, axis=dim
              )
              nanmedian_indices = ivy.expand_dims(
                  nanmedian_tuple, axis=dim
              )
  
          result = nanmedian_tuple(nanmedian_values, nanmedian_indices)
      if out is not None:
          ivy.inplace_update(out[0], result.values)
          ivy.inplace_update(out[1], result.indices)
          return out
      return result
  
imports: import ivy
  from ivy.func_wrapper import with_unsupported_dtypes, with_supported_dtypes
  from ivy.functional.frontends.torch.func_wrapper import to_ivy_arrays_and_back, numpy_to_torch_style_args
  from collections import namedtuple
  import ivy.functional.frontends.torch
label_desc: Use image node_id 17 for calling a global function with example usage: nanmedian(input, dim, keepdim) and returns: result, sorted_input, out