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.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. from ..device import get_device_count
  10. class SublinearMemoryConfig:
  11. r"""Configuration for sublinear memory optimization.
  12. Args:
  13. thresh_nr_try: number of samples both for searching in linear space
  14. and around current thresh in sublinear memory optimization. Default: 10.
  15. It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_THRESH_NR_TRY'.
  16. genetic_nr_iter: number of iterations to find the best checkpoints in genetic algorithm.
  17. Default: 0.
  18. It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_GENETIC_NR_ITER'.
  19. genetic_pool_size: number of samples for the crossover random selection
  20. during genetic optimization. Default: 20.
  21. It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_GENETIC_POOL_SIZE'.
  22. lb_memory_mb: memory lower bound of bottleneck size in MB for sublinear memory optimization.
  23. It can be used to perform manual tradeoff between memory and speed. Default: 0.
  24. It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_LOWER_BOUND_MB'.
  25. num_worker: number of thread workers to search the optimum checkpoints
  26. in sublinear memory optimization. Default: half of cpu number in the system.
  27. Note: the value must be greater or equal to one.
  28. It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_WORKERS'.
  29. Note that the environmental variable MGB_COMP_GRAPH_OPT must be set to 'enable_sublinear_memory_opt=1'
  30. in order for the above environmental variable to be effective.
  31. """
  32. def __init__(
  33. self,
  34. thresh_nr_try: int = 10,
  35. genetic_nr_iter: int = 0,
  36. genetic_pool_size: int = 20,
  37. lb_memory_mb: int = 0,
  38. num_worker: int = max(1, get_device_count("cpu") // 2),
  39. ):
  40. assert thresh_nr_try >= 0, "thresh_nr_try must be greater or equal to zero"
  41. self.thresh_nr_try = thresh_nr_try
  42. assert genetic_nr_iter >= 0, "genetic_nr_iter must be greater or equal to zero"
  43. self.genetic_nr_iter = genetic_nr_iter
  44. assert (
  45. genetic_pool_size >= 0
  46. ), "genetic_pool_size must be greater or equal to zero"
  47. self.genetic_pool_size = genetic_pool_size
  48. self.lb_memory_mb = lb_memory_mb
  49. assert num_worker > 0, "num_worker must be greater or equal to one"
  50. self.num_worker = num_worker