You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sublinear_memory_config.py 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. from ..device import get_device_count
  3. class SublinearMemoryConfig:
  4. r"""Configuration for sublinear memory optimization.
  5. Args:
  6. thresh_nr_try: number of samples both for searching in linear space
  7. and around current thresh in sublinear memory optimization. Default: 10.
  8. It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_THRESH_NR_TRY'.
  9. genetic_nr_iter: number of iterations to find the best checkpoints in genetic algorithm.
  10. Default: 0.
  11. It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_GENETIC_NR_ITER'.
  12. genetic_pool_size: number of samples for the crossover random selection
  13. during genetic optimization. Default: 20.
  14. It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_GENETIC_POOL_SIZE'.
  15. lb_memory_mb: memory lower bound of bottleneck size in MB for sublinear memory optimization.
  16. It can be used to perform manual tradeoff between memory and speed. Default: 0.
  17. It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_LOWER_BOUND_MB'.
  18. num_worker: number of thread workers to search the optimum checkpoints
  19. in sublinear memory optimization. Default: half of cpu number in the system.
  20. Note: the value must be greater or equal to one.
  21. It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_WORKERS'.
  22. Note that the environmental variable MGB_COMP_GRAPH_OPT must be set to 'enable_sublinear_memory_opt=1'
  23. in order for the above environmental variable to be effective.
  24. """
  25. def __init__(
  26. self,
  27. thresh_nr_try: int = 10,
  28. genetic_nr_iter: int = 0,
  29. genetic_pool_size: int = 20,
  30. lb_memory_mb: int = 0,
  31. num_worker: int = max(1, get_device_count("cpu") // 2),
  32. ):
  33. assert thresh_nr_try >= 0, "thresh_nr_try must be greater or equal to zero"
  34. self.thresh_nr_try = thresh_nr_try
  35. assert genetic_nr_iter >= 0, "genetic_nr_iter must be greater or equal to zero"
  36. self.genetic_nr_iter = genetic_nr_iter
  37. assert (
  38. genetic_pool_size >= 0
  39. ), "genetic_pool_size must be greater or equal to zero"
  40. self.genetic_pool_size = genetic_pool_size
  41. self.lb_memory_mb = lb_memory_mb
  42. assert num_worker > 0, "num_worker must be greater or equal to one"
  43. self.num_worker = num_worker