utils.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import logging
  2. from ray.util.debug import log_once
  3. logger = logging.getLogger(__name__)
  4. def truncate_operator_name(name: str, max_name_length: int) -> str:
  5. from ray.data.context import DataContext
  6. ctx = DataContext.get_current()
  7. if not ctx.enable_progress_bar_name_truncation or len(name) <= max_name_length:
  8. return name
  9. op_names = name.split("->")
  10. if len(op_names) == 1:
  11. return op_names[0]
  12. # Include as many operators as possible without approximately
  13. # exceeding `MAX_NAME_LENGTH`. Always include the first and
  14. # last operator names so it is easy to identify the DAG.
  15. truncated_op_names = [op_names[0]]
  16. for op_name in op_names[1:-1]:
  17. if (
  18. len("->".join(truncated_op_names))
  19. + len("->")
  20. + len(op_name)
  21. + len("->")
  22. + len(op_names[-1])
  23. ) > max_name_length:
  24. truncated_op_names.append("...")
  25. if log_once("ray_data_truncate_operator_name"):
  26. logger.warning(
  27. f"Truncating long operator name to {max_name_length} "
  28. "characters. To disable this behavior, set "
  29. "`ray.data.DataContext.get_current()."
  30. "DEFAULT_ENABLE_PROGRESS_BAR_NAME_TRUNCATION = False`."
  31. )
  32. break
  33. truncated_op_names.append(op_name)
  34. truncated_op_names.append(op_names[-1])
  35. return "->".join(truncated_op_names)