def METHOD_NAME(type_spec: computation_types.Type) -> pb.Type:
  """Serializes 'type_spec' as a pb.Type.
  Note: Currently only serialization for tensor, named tuple, sequence, and
  function types is implemented.
  Args:
    type_spec: A `computation_types.Type`.
  Returns:
    The corresponding instance of `pb.Type`.
  Raises:
    TypeError: if the argument is of the wrong type.
    NotImplementedError: for type variants for which serialization is not
      implemented.
  """
  cached_proto = _type_serialization_cache.get(type_spec, None)
  if cached_proto is not None:
    return cached_proto
  if isinstance(type_spec, computation_types.TensorType):
    proto = pb.Type(tensor=_to_tensor_type_proto(type_spec))
  elif isinstance(type_spec, computation_types.SequenceType):
    proto = pb.Type(
        sequence=pb.SequenceType(element=METHOD_NAME(type_spec.element))
    )
  elif isinstance(type_spec, computation_types.StructType):
    proto = pb.Type(
        struct=pb.StructType(
            element=[
                pb.StructType.Element(name=e[0], value=METHOD_NAME(e[1]))
                for e in structure.iter_elements(type_spec)
            ]
        )
    )
  elif isinstance(type_spec, computation_types.FunctionType):
    if type_spec.parameter is not None:
      serialized_parameter = METHOD_NAME(type_spec.parameter)
    else:
      serialized_parameter = None
    proto = pb.Type(
        function=pb.FunctionType(
            parameter=serialized_parameter,
            result=METHOD_NAME(type_spec.result),
        )
    )
  elif isinstance(type_spec, computation_types.PlacementType):
    proto = pb.Type(placement=pb.PlacementType())
  elif isinstance(type_spec, computation_types.FederatedType):
    proto = pb.Type(
        federated=pb.FederatedType(
            member=METHOD_NAME(type_spec.member),
            placement=pb.PlacementSpec(
                value=pb.Placement(uri=type_spec.placement.uri)
            ),
            all_equal=type_spec.all_equal,
        )
    )
  else:
    raise NotImplementedError
  _type_serialization_cache[type_spec] = proto
  return proto