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.

utils.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Some utils."""
  16. import hashlib
  17. import logging
  18. import os
  19. import inspect
  20. from functools import wraps
  21. from dataclasses import dataclass
  22. def cal_sha256(file_path):
  23. """
  24. Calculate sha256 value of input file.
  25. Args:
  26. file_path (str): file path to calculate.
  27. Returns:
  28. str, returns sha256 value of input file or empty str if cal failed.
  29. """
  30. buf_size = 64 * 1024 # once read 64kb
  31. sha256 = hashlib.sha256()
  32. file_real_path = os.path.realpath(file_path)
  33. if not os.path.isfile(file_real_path) or not os.access(file_real_path, os.R_OK):
  34. return ""
  35. _, extension = os.path.splitext(file_path)
  36. try:
  37. if extension == '.ptx':
  38. with open(file_real_path, 'r') as kf:
  39. while True:
  40. data = kf.read(buf_size)
  41. if not data:
  42. break
  43. sha256.update(data.encode("utf-8"))
  44. else:
  45. with open(file_real_path, 'rb') as kf:
  46. while True:
  47. data = kf.read(buf_size)
  48. if not data:
  49. break
  50. sha256.update(data)
  51. except IOError:
  52. logging.error("Open file %s failed.", file_path)
  53. return ""
  54. return sha256.hexdigest()
  55. def cell_attr_register(fn=None, attrs=None):
  56. """
  57. Cell init attributes register.
  58. Registering the decorator of the built-in operator cell __init__
  59. function will add save all the parameters of __init__ as operator attributes.
  60. Args:
  61. fn (function): __init__ function of cell.
  62. attrs (list(string) | string): attr list.
  63. Returns:
  64. function, original function.
  65. """
  66. def wrap_cell(fn):
  67. @wraps(fn)
  68. def deco(self, *args, **kwargs):
  69. arguments = []
  70. if attrs is None:
  71. bound_args = inspect.signature(fn).bind(self, *args, **kwargs)
  72. arguments = bound_args.arguments
  73. del arguments['self']
  74. arguments = arguments.values()
  75. fn(self, *args, **kwargs)
  76. if attrs is not None:
  77. if isinstance(attrs, list):
  78. for item in attrs:
  79. if not isinstance(item, str):
  80. raise ValueError(f"attr must be a string")
  81. if hasattr(self, item):
  82. arguments.append(getattr(self, item))
  83. elif isinstance(attrs, str):
  84. if hasattr(self, attrs):
  85. arguments = getattr(self, attrs)
  86. else:
  87. raise ValueError(f"attrs must be list or string")
  88. self.cell_init_args = type(self).__name__ + str(arguments)
  89. return deco
  90. if fn is not None:
  91. return wrap_cell(fn)
  92. return wrap_cell
  93. @dataclass
  94. class Slice:
  95. """
  96. Slice class
  97. """
  98. start: int
  99. end: int
  100. step: int