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.

_checkparam.py 28 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. """Check parameters."""
  16. import re
  17. import inspect
  18. import math
  19. from enum import Enum
  20. from functools import reduce, wraps
  21. from itertools import repeat
  22. from collections.abc import Iterable
  23. import numpy as np
  24. from mindspore import log as logger
  25. from mindspore.common import dtype as mstype
  26. class Rel(Enum):
  27. """Numerical relationship between variables, logical relationship enumeration definition of range."""
  28. # scalar compare
  29. EQ = 1 # ==
  30. NE = 2 # !=
  31. LT = 3 # <
  32. LE = 4 # <=
  33. GT = 5 # >
  34. GE = 6 # >=
  35. # scalar range check
  36. INC_NEITHER = 7 # (), include neither
  37. INC_LEFT = 8 # [), include left
  38. INC_RIGHT = 9 # (], include right
  39. INC_BOTH = 10 # [], include both
  40. # collection in, not in
  41. IN = 11
  42. NOT_IN = 12
  43. @staticmethod
  44. def get_strs(rel):
  45. """Get value from rel_strs."""
  46. return rel_strs.get(rel, "")
  47. @staticmethod
  48. def get_fns(rel):
  49. """Get value from rel_fns."""
  50. return rel_fns.get(rel, lambda *args: False)
  51. rel_fns = {
  52. # scalar compare
  53. Rel.EQ: lambda x, y: x == y,
  54. Rel.NE: lambda x, y: x != y,
  55. Rel.LT: lambda x, y: x < y,
  56. Rel.LE: lambda x, y: x <= y,
  57. Rel.GT: lambda x, y: x > y,
  58. Rel.GE: lambda x, y: x >= y,
  59. # scalar range check
  60. Rel.INC_NEITHER: lambda x, lower, upper: (lower < x < upper),
  61. Rel.INC_LEFT: lambda x, lower, upper: (lower <= x < upper),
  62. Rel.INC_RIGHT: lambda x, lower, upper: (lower < x <= upper),
  63. Rel.INC_BOTH: lambda x, lower, upper: (lower <= x <= upper),
  64. # collection in, not in
  65. Rel.IN: lambda x, y: x in y,
  66. Rel.NOT_IN: lambda x, y: x not in y,
  67. }
  68. rel_strs = {
  69. # scalar compare
  70. Rel.EQ: "== {}",
  71. Rel.NE: "!= {}",
  72. Rel.LT: "< {}",
  73. Rel.LE: "<= {}",
  74. Rel.GT: "> {}",
  75. Rel.GE: ">= {}",
  76. # scalar range check
  77. Rel.INC_NEITHER: "({}, {})",
  78. Rel.INC_LEFT: "[{}, {})",
  79. Rel.INC_RIGHT: "({}, {}]",
  80. Rel.INC_BOTH: "[{}, {}]",
  81. # collection in, not in
  82. Rel.IN: "in {}",
  83. Rel.NOT_IN: "not in {}",
  84. }
  85. def check_number(arg_value, value, rel, arg_type=int, arg_name=None, prim_name=None):
  86. """
  87. Check argument integer.
  88. Usage:
  89. - number = check_int(number, 0, Rel.GE, "number", None) # number >= 0
  90. """
  91. rel_fn = Rel.get_fns(rel)
  92. type_mismatch = not isinstance(arg_value, arg_type) or isinstance(arg_value, bool)
  93. type_except = TypeError if type_mismatch else ValueError
  94. prim_name = f'in `{prim_name}`' if prim_name else ''
  95. arg_name = f'`{arg_name}`' if arg_name else ''
  96. if math.isinf(arg_value) or math.isnan(arg_value) or np.isinf(arg_value) or np.isnan(arg_value):
  97. raise ValueError(f'{arg_name} {prim_name} must be legal value, but got `{arg_value}`.')
  98. if type_mismatch or not rel_fn(arg_value, value):
  99. rel_str = Rel.get_strs(rel).format(value)
  100. raise type_except(f'{arg_name} {prim_name} should be an {arg_type.__name__} and must {rel_str}, '
  101. f'but got `{arg_value}` with type `{type(arg_value).__name__}`.')
  102. return arg_value
  103. def check_is_number(arg_value, arg_type, arg_name=None, prim_name=None):
  104. """
  105. Checks input value is float type or not.
  106. Usage:
  107. - number = check_is_number(number, int)
  108. - number = check_is_number(number, int, "bias")
  109. - number = check_is_number(number, int, "bias", "bias_class")
  110. """
  111. prim_name = f'in \'{prim_name}\'' if prim_name else ''
  112. arg_name = f'\'{prim_name}\'' if arg_name else 'Input value'
  113. if isinstance(arg_value, arg_type) and not isinstance(arg_value, bool):
  114. if math.isinf(arg_value) or math.isnan(arg_value) or np.isinf(arg_value) or np.isnan(arg_value):
  115. raise ValueError(f'{arg_name} {prim_name} must be legal float, but got `{arg_value}`.')
  116. return arg_value
  117. raise TypeError(f'{arg_name} {prim_name} must be {arg_type.__name__}, but got `{type(arg_value).__name__}`')
  118. def check_number_range(arg_value, lower_limit, upper_limit, rel, value_type, arg_name=None, prim_name=None):
  119. """
  120. Method for checking whether an int value is in some range.
  121. Usage:
  122. - number = check_number_range(number, 0.0, 1.0, Rel.INC_NEITHER, "number", float) # number in [0.0, 1.0]
  123. - number = check_number_range(number, 0, 1, Rel.INC_NEITHER, "number", int) # number in [0, 1]
  124. """
  125. rel_fn = Rel.get_fns(rel)
  126. prim_name = f'in `{prim_name}`' if prim_name else ''
  127. arg_name = f'`{arg_name}`' if arg_name else ''
  128. type_mismatch = not isinstance(arg_value, (np.ndarray, np.generic, value_type)) or isinstance(arg_value, bool)
  129. if type_mismatch:
  130. raise TypeError("{} {} must be `{}`, but got `{}`.".format(
  131. arg_name, prim_name, value_type.__name__, type(arg_value).__name__))
  132. if not rel_fn(arg_value, lower_limit, upper_limit):
  133. rel_str = Rel.get_strs(rel).format(lower_limit, upper_limit)
  134. raise ValueError("{} {} should be in range of {}, but got {:.3e} with type `{}`.".format(
  135. arg_name, prim_name, rel_str, arg_value, type(arg_value).__name__))
  136. return arg_value
  137. class Validator:
  138. """validator for checking input parameters"""
  139. @staticmethod
  140. def check(arg_name, arg_value, value_name, value, rel=Rel.EQ, prim_name=None, excp_cls=ValueError):
  141. """
  142. Method for judging relation between two int values or list/tuple made up of ints.
  143. This method is not suitable for judging relation between floats, since it does not consider float error.
  144. """
  145. rel_fn = Rel.get_fns(rel)
  146. if not rel_fn(arg_value, value):
  147. rel_str = Rel.get_strs(rel).format(f'{value_name}: {value}')
  148. msg_prefix = f'For \'{prim_name}\' the' if prim_name else "The"
  149. raise excp_cls(f'{msg_prefix} `{arg_name}` should be {rel_str}, but got {arg_value}.')
  150. return arg_value
  151. @staticmethod
  152. def check_int(arg_value, value, rel, arg_name=None, prim_name=None):
  153. """
  154. Checks input integer value `arg_value` compare to `value`.
  155. Usage:
  156. - number = check_int(number, 0, Rel.GE, "number", None) # number >= 0
  157. """
  158. return check_number(arg_value, value, rel, int, arg_name, prim_name)
  159. @staticmethod
  160. def check_is_int(arg_value, arg_name=None, prim_name=None):
  161. """
  162. Checks input value is float type or not.
  163. Usage:
  164. - number = check_is_int(number, int)
  165. - number = check_is_int(number, int, "bias")
  166. - number = check_is_int(number, int, "bias", "bias_class")
  167. """
  168. return check_is_number(arg_value, int, arg_name, prim_name)
  169. @staticmethod
  170. def check_equal_int(arg_value, value, arg_name=None, prim_name=None):
  171. """
  172. Checks input integer value `arg_value` compare to `value`.
  173. Usage:
  174. - number = check_int(number, 0, Rel.GE, "number", None) # number >= 0
  175. """
  176. return check_number(arg_value, value, Rel.EQ, int, arg_name, prim_name)
  177. @staticmethod
  178. def check_positive_int(arg_value, arg_name=None, prim_name=None):
  179. """
  180. Check argument is positive integer, which mean arg_value > 0.
  181. Usage:
  182. - number = check_positive_int(number)
  183. - number = check_positive_int(number, "bias")
  184. """
  185. return check_number(arg_value, 0, Rel.GT, int, arg_name, prim_name)
  186. @staticmethod
  187. def check_negative_int(arg_value, arg_name=None, prim_name=None):
  188. """
  189. Check argument is negative integer, which mean arg_value < 0.
  190. Usage:
  191. - number = check_negative_int(number)
  192. - number = check_negative_int(number, "bias")
  193. """
  194. return check_number(arg_value, 0, Rel.LT, int, arg_name, prim_name)
  195. @staticmethod
  196. def check_non_positive_int(arg_value, arg_name=None, prim_name=None):
  197. """
  198. Check argument is non-negative integer, which mean arg_value <= 0.
  199. Usage:
  200. - number = check_non_positive_int(number)
  201. - number = check_non_positive_int(number, "bias")
  202. """
  203. return check_number(arg_value, 0, Rel.LE, int, arg_name, prim_name)
  204. @staticmethod
  205. def check_non_negative_int(arg_value, arg_name=None, prim_name=None):
  206. """
  207. Check argument is non-negative integer, which mean arg_value >= 0.
  208. Usage:
  209. - number = check_non_negative_int(number)
  210. - number = check_non_negative_int(number, "bias")
  211. """
  212. return check_number(arg_value, 0, Rel.GE, int, arg_name, prim_name)
  213. @staticmethod
  214. def check_float(arg_value, value, rel, arg_name=None, prim_name=None):
  215. """
  216. Checks input float value `arg_value` compare to `value`.
  217. Usage:
  218. - number = check_float(number, 0.0, Rel.GE, "number", None) # number >= 0
  219. """
  220. return check_number(arg_value, value, rel, float, arg_name, prim_name)
  221. @staticmethod
  222. def check_is_float(arg_value, arg_name=None, prim_name=None):
  223. """
  224. Checks input value is float type or not.
  225. Usage:
  226. - number = check_is_float(number, int)
  227. - number = check_is_float(number, int, "bias")
  228. - number = check_is_float(number, int, "bias", "bias_class")
  229. """
  230. return check_is_number(arg_value, float, arg_name, prim_name)
  231. @staticmethod
  232. def check_positive_float(arg_value, arg_name=None, prim_name=None):
  233. """
  234. Check argument is positive float, which mean arg_value > 0.
  235. Usage:
  236. - number = check_positive_float(number)
  237. - number = check_positive_float(number, "bias")
  238. - number = check_positive_float(number, "bias", "bias_class")
  239. """
  240. return check_number(arg_value, 0, Rel.GT, float, arg_name, prim_name)
  241. @staticmethod
  242. def check_negative_float(arg_value, arg_name=None, prim_name=None):
  243. """
  244. Check argument is negative float, which mean arg_value < 0.
  245. Usage:
  246. - number = check_negative_float(number)
  247. - number = check_negative_float(number, "bias")
  248. """
  249. return check_number(arg_value, 0, Rel.LT, float, arg_name, prim_name)
  250. @staticmethod
  251. def check_non_positive_float(arg_value, arg_name=None, prim_name=None):
  252. """
  253. Check argument is non-negative float, which mean arg_value <= 0.
  254. Usage:
  255. - number = check_non_positive_float(number)
  256. - number = check_non_positive_float(number, "bias")
  257. """
  258. return check_number(arg_value, 0, Rel.LE, float, arg_name, prim_name)
  259. @staticmethod
  260. def check_non_negative_float(arg_value, arg_name=None, prim_name=None):
  261. """
  262. Check argument is non-negative float, which mean arg_value >= 0.
  263. Usage:
  264. - number = check_non_negative_float(number)
  265. - number = check_non_negative_float(number, "bias")
  266. """
  267. return check_number(arg_value, 0, Rel.GE, float, arg_name, prim_name)
  268. @staticmethod
  269. def check_number(arg_name, arg_value, value, rel, prim_name):
  270. """Number value judgment."""
  271. rel_fn = Rel.get_fns(rel)
  272. if not rel_fn(arg_value, value):
  273. rel_str = Rel.get_strs(rel).format(value)
  274. raise ValueError(f'For \'{prim_name}\' the `{arg_name}` must {rel_str}, but got {arg_value}.')
  275. return arg_value
  276. @staticmethod
  277. def check_isinstance(arg_name, arg_value, classes):
  278. """Check arg isinstance of classes"""
  279. if not isinstance(arg_value, classes):
  280. raise ValueError(f'The `{arg_name}` should be isinstance of {classes}, but got {arg_value}.')
  281. return arg_value
  282. @staticmethod
  283. def check_bool(arg_value, arg_name=None):
  284. """
  285. Check argument is instance of bool.
  286. Usage:
  287. - has_bias = check_bool(has_bias)
  288. - has_bias = check_bool(has_bias, "has_bias")
  289. """
  290. if not isinstance(arg_value, bool):
  291. arg_name = arg_name if arg_name else "Parameter"
  292. raise TypeError(f'`{arg_name}` should be isinstance of bool, but got `{arg_value}`.')
  293. return arg_value
  294. @staticmethod
  295. def check_int_range(arg_value, lower_limit, upper_limit, rel, arg_name=None, prim_name=None):
  296. """
  297. Method for checking whether input value is in int range.
  298. Usage:
  299. - number = check_int_range(number, 0, 1, Rel.INC_NEITHER) # number in [0, 1]
  300. - number = check_int_range(number, 0, 1, Rel.INC_NEITHER, "number") # number in [0, 1]
  301. """
  302. return check_number_range(arg_value, lower_limit, upper_limit, rel, int, arg_name, prim_name)
  303. @staticmethod
  304. def check_float_range(arg_value, lower_limit, upper_limit, rel, arg_name=None, prim_name=None):
  305. """
  306. Method for checking whether input value is in float range.
  307. Usage:
  308. - number = check_float_range(number, 0.0, 1.0, Rel.INC_NEITHER) # number in [0.0, 1.0]
  309. - number = check_float_range(number, 0.0, 1.0, Rel.INC_NEITHER, "number") # number in [0.0, 1.0]
  310. """
  311. return check_number_range(arg_value, lower_limit, upper_limit, rel, float, arg_name, prim_name)
  312. @staticmethod
  313. def check_string(arg_value, valid_values, arg_name=None, prim_name=None):
  314. """
  315. Check whether string is in some value list.
  316. Usage:
  317. - method = check_string(method, ["string1", "string2", "string3"], "method")
  318. """
  319. if isinstance(arg_value, str) and arg_value in valid_values:
  320. return arg_value
  321. arg_name = arg_name if arg_name else "Parameter"
  322. msg_prefix = f'For \'{prim_name}\' the' if prim_name else "The"
  323. raise ValueError(f'{msg_prefix} `{arg_name}` should be str and must be in `{valid_values}`,'
  324. f' but got `{arg_value}`.')
  325. @staticmethod
  326. def check_str_by_regular(target, reg=None, flag=re.ASCII, prim_name=None):
  327. if reg is None:
  328. # Named string regular expression
  329. reg = r"^\w+[0-9a-zA-Z\_\.]*$"
  330. if re.match(reg, target, flag) is None:
  331. prim_name = f'in `{prim_name}`' if prim_name else ""
  332. raise ValueError("'{}' {} is illegal, it should be match regular'{}' by flags'{}'".format(
  333. target, prim_name, reg, flag))
  334. return True
  335. @staticmethod
  336. def check_pad_value_by_mode(pad_mode, padding, prim_name):
  337. """Validates value of padding according to pad_mode"""
  338. if pad_mode != 'pad' and padding != 0:
  339. raise ValueError(f"For '{prim_name}', padding must be zero when pad_mode is '{pad_mode}'.")
  340. return padding
  341. @staticmethod
  342. def check_subclass(arg_name, type_, template_types, prim_name):
  343. """Checks whether some type is subclass of another type"""
  344. if not isinstance(template_types, Iterable):
  345. template_types = (template_types,)
  346. hit = False
  347. for template_type in template_types:
  348. if isinstance(template_type, mstype.Type):
  349. if mstype.issubclass_(type_, template_type):
  350. hit = True
  351. break
  352. elif type_ is template_type:
  353. hit = True
  354. break
  355. if not hit:
  356. type_str = (type(type_).__name__ if isinstance(type_, (tuple, list)) else "") + str(type_)
  357. raise TypeError(f'For \'{prim_name}\' the type of `{arg_name}` should be subclass'
  358. f' of {",".join((str(x) for x in template_types))}, but got {type_str}.')
  359. @staticmethod
  360. def check_const_input(arg_name, arg_value, prim_name):
  361. """Checks valid value."""
  362. if arg_value is None:
  363. raise ValueError(f'For \'{prim_name}\' the `{arg_name}` must be a const input, but got {arg_value}.')
  364. return arg_value
  365. @staticmethod
  366. def check_type(arg_name, arg_value, valid_types):
  367. """Type checking."""
  368. def raise_error_msg():
  369. """func for raising error message when check failed"""
  370. raise TypeError(f'The type of `{arg_name}` should be in {valid_types}, but got {type(arg_value).__name__}.')
  371. if isinstance(arg_value, type(mstype.tensor)):
  372. arg_value = arg_value.element_type()
  373. if isinstance(arg_value, bool) and bool not in tuple(valid_types):
  374. raise_error_msg()
  375. if arg_value in valid_types:
  376. return arg_value
  377. if isinstance(arg_value, tuple(valid_types)):
  378. return arg_value
  379. raise_error_msg()
  380. @staticmethod
  381. def check_type_same(args, valid_values, prim_name):
  382. """Checks whether the types of inputs are the same."""
  383. def _check_tensor_type(arg):
  384. arg_key, arg_val = arg
  385. elem_type = arg_val
  386. Validator.check_subclass(arg_key, elem_type, valid_values, prim_name)
  387. return (arg_key, elem_type)
  388. def _check_types_same(arg1, arg2):
  389. arg1_name, arg1_type = arg1
  390. arg2_name, arg2_type = arg2
  391. if arg1_type != arg2_type:
  392. raise TypeError(f'For \'{prim_name}\' type of `{arg2_name}` should be same as `{arg1_name}`,'
  393. f' but `{arg1_name}` with type {arg1_type} and `{arg2_name}` with type {arg2_type}.')
  394. return arg1
  395. elem_types = map(_check_tensor_type, args.items())
  396. reduce(_check_types_same, elem_types)
  397. @staticmethod
  398. def check_tensor_type_same(args, valid_values, prim_name):
  399. """Checks whether the element types of input tensors are the same."""
  400. tensor_types = [mstype.tensor_type(t) for t in valid_values]
  401. Validator.check_type_same(args, tensor_types, prim_name)
  402. @staticmethod
  403. def check_scalar_or_tensor_type_same(args, valid_values, prim_name, allow_mix=False):
  404. """
  405. Checks whether the types of inputs are the same. If the input args are tensors, checks their element types.
  406. If `allow_mix` is True, Tensor(float32) and float32 are type compatible, otherwise an exception will be raised.
  407. """
  408. def _check_argument_type(arg):
  409. arg_key, arg_val = arg
  410. if isinstance(arg_val, type(mstype.tensor)):
  411. arg_val = arg_val.element_type()
  412. if not arg_val in valid_values:
  413. raise TypeError(f'For \'{prim_name}\' the `{arg_key}` should be in {valid_values},'
  414. f' but `{arg_key}` is {arg_val}.')
  415. return arg
  416. def _check_types_same(arg1, arg2):
  417. arg1_name, arg1_type = arg1
  418. arg2_name, arg2_type = arg2
  419. except_flag = False
  420. if isinstance(arg1_type, type(mstype.tensor)) and isinstance(arg2_type, type(mstype.tensor)):
  421. arg1_type = arg1_type.element_type()
  422. arg2_type = arg2_type.element_type()
  423. elif not (isinstance(arg1_type, type(mstype.tensor)) or isinstance(arg2_type, type(mstype.tensor))):
  424. pass
  425. elif allow_mix:
  426. arg1_type = arg1_type.element_type() if isinstance(arg1_type, type(mstype.tensor)) else arg1_type
  427. arg2_type = arg2_type.element_type() if isinstance(arg2_type, type(mstype.tensor)) else arg2_type
  428. else:
  429. except_flag = True
  430. if except_flag or arg1_type != arg2_type:
  431. raise TypeError(f'For \'{prim_name}\' type of `{arg2_name}` should be same as `{arg1_name}`,'
  432. f' but `{arg1_name}` is {arg1_type} and `{arg2_name}` is {arg2_type}.')
  433. return arg1
  434. reduce(_check_types_same, map(_check_argument_type, args.items()))
  435. @staticmethod
  436. def check_value_type(arg_name, arg_value, valid_types, prim_name=None):
  437. """Checks whether a value is instance of some types."""
  438. valid_types = valid_types if isinstance(valid_types, Iterable) else (valid_types,)
  439. def raise_error_msg():
  440. """func for raising error message when check failed"""
  441. type_names = [t.__name__ for t in valid_types]
  442. num_types = len(valid_types)
  443. msg_prefix = f'For \'{prim_name}\' the' if prim_name else 'The'
  444. raise TypeError(f'{msg_prefix} type of `{arg_name}` should be {"one of " if num_types > 1 else ""}'
  445. f'{type_names if num_types > 1 else type_names[0]}, but got {type(arg_value).__name__}.')
  446. # Notice: bool is subclass of int, so `check_value_type('x', True, [int])` will check fail, and
  447. # `check_value_type('x', True, [bool, int])` will check pass
  448. if isinstance(arg_value, bool) and bool not in tuple(valid_types):
  449. raise_error_msg()
  450. if isinstance(arg_value, tuple(valid_types)):
  451. return arg_value
  452. raise_error_msg()
  453. @staticmethod
  454. def check_type_name(arg_name, arg_type, valid_types, prim_name):
  455. """Checks whether a type in some specified types"""
  456. valid_types = valid_types if isinstance(valid_types, Iterable) else (valid_types,)
  457. def get_typename(t):
  458. return t.__name__ if hasattr(t, '__name__') else str(t)
  459. if isinstance(arg_type, type(mstype.tensor)):
  460. arg_type = arg_type.element_type()
  461. if arg_type in valid_types:
  462. return arg_type
  463. type_names = [get_typename(t) for t in valid_types]
  464. msg_prefix = f'For \'{prim_name}\' the' if prim_name else 'The'
  465. if len(valid_types) == 1:
  466. raise TypeError(f'{msg_prefix} type of `{arg_name}` should be {type_names[0]},'
  467. f' but got {get_typename(arg_type)}.')
  468. raise TypeError(f'{msg_prefix} type of `{arg_name}` should be one of {type_names},'
  469. f' but got {get_typename(arg_type)}.')
  470. @staticmethod
  471. def check_reduce_shape(ori_shape, shape, axis, prim_name):
  472. """Checks whether shape is ori_shape reduced on axis"""
  473. axis = axis if isinstance(axis, Iterable) else (axis,)
  474. exp_shape = [ori_shape[i] for i in range(len(ori_shape)) if i not in axis]
  475. if list(shape) != exp_shape:
  476. raise ValueError(f'For {prim_name}, {ori_shape} reduce on {axis} should be '
  477. f'{tuple(exp_shape)}, but got {shape}.')
  478. def check_input_format(input_param):
  479. """Judge input format."""
  480. if input_param == "NCHW":
  481. return input_param
  482. raise ValueError("The data format must be NCHW.")
  483. def _expand_tuple(n_dimensions):
  484. """To expand a number to tuple."""
  485. def convert(m):
  486. if not isinstance(m, tuple):
  487. if isinstance(m, int):
  488. return tuple(repeat(m, n_dimensions))
  489. raise TypeError("Input type must be int or tuple.")
  490. if not len(m) is n_dimensions:
  491. raise TypeError("Input dimension is incorrect.")
  492. for i in m:
  493. if not isinstance(i, int):
  494. raise TypeError("Incorrect type inside of a tuple!")
  495. return m
  496. return convert
  497. def check_input_data(*data, data_class):
  498. """Input data check."""
  499. for item in data:
  500. if isinstance(item, (list, tuple)):
  501. for v in item:
  502. check_input_data(v, data_class=data_class)
  503. else:
  504. if not isinstance(item, data_class):
  505. raise ValueError(f'Please provide as model inputs'
  506. f' either a single'
  507. f' or a list of {data_class.__name__},'
  508. f' but got part data type is {str(type(item))}.')
  509. if item.size() == 0:
  510. msg = "Please provide non-empty data."
  511. logger.error(msg)
  512. raise ValueError(msg)
  513. def check_output_data(data):
  514. """Output data check."""
  515. if data is None:
  516. raise RuntimeError('Executor return data ' + str(data) + ', please check your net or input data.')
  517. once = _expand_tuple(1)
  518. twice = _expand_tuple(2)
  519. triple = _expand_tuple(3)
  520. valid_data_types = (int, float, np.int8, np.int16, np.int32, np.int64,
  521. np.uint8, np.uint16, np.uint32, np.uint64, np.float16,
  522. np.float32, np.float64, bool, np.bool_)
  523. def check_type(arg_name, arg_value, valid_types):
  524. """Check value type."""
  525. # if input type is Tensor ,get element type
  526. if isinstance(arg_value, type(mstype.tensor)):
  527. arg_value = arg_value.element_type()
  528. # First, check if arg_value has argvalid_types
  529. if isinstance(arg_value, tuple(valid_types)):
  530. return type(arg_value).__name__
  531. # Second, wrap arg_value with numpy array so that it can be checked through numpy api
  532. if isinstance(arg_value, (list, tuple)):
  533. arg_value = np.array(arg_value)
  534. # Thirdly, check the data type by numpy's dtype api
  535. valid = False
  536. if isinstance(arg_value, np.ndarray):
  537. valid = arg_value.dtype in valid_data_types
  538. # Notice: bool is subclass of int, so `check_type('x', True, [int])` will check fail, and
  539. # `check_type('x', True, [bool, int])` will check pass
  540. if isinstance(arg_value, bool) and bool not in tuple(valid_types):
  541. valid = False
  542. if not valid:
  543. type_names = [t.__name__ for t in valid_types]
  544. if len(valid_types) == 1:
  545. raise TypeError(f'The type of `{arg_name}` should be {type_names[0]},'
  546. f' but got {type(arg_value).__name__}.')
  547. raise TypeError(f'The type of `{arg_name}` should be one of {type_names},'
  548. f' but got {type(arg_value).__name__}.')
  549. return type(arg_value).__name__
  550. def check_typename(arg_name, arg_type, valid_types):
  551. """Check type name."""
  552. def get_typename(t):
  553. return t.__name__ if hasattr(t, '__name__') else str(t)
  554. if isinstance(arg_type, type(mstype.tensor)):
  555. arg_type = arg_type.element_type()
  556. if arg_type in valid_types:
  557. return arg_type
  558. if isinstance(arg_type, tuple(valid_types)):
  559. return arg_type
  560. type_names = [get_typename(t) for t in valid_types]
  561. if len(valid_types) == 1:
  562. raise TypeError(f'The type of `{arg_name}` should be {type_names[0]},'
  563. f' but got {get_typename(arg_type)}.')
  564. raise TypeError(f'The type of `{arg_name}` should be one of {type_names},'
  565. f' but got {get_typename(arg_type)}.')
  566. def args_type_check(*type_args, **type_kwargs):
  567. """Check whether input data type is correct."""
  568. def type_check(func):
  569. sig = inspect.signature(func)
  570. bound_types = sig.bind_partial(*type_args, **type_kwargs).arguments
  571. @wraps(func)
  572. def wrapper(*args, **kwargs):
  573. nonlocal bound_types
  574. bound_values = sig.bind(*args, **kwargs)
  575. argument_dict = bound_values.arguments
  576. if "kwargs" in bound_types:
  577. bound_types = bound_types["kwargs"]
  578. if "kwargs" in argument_dict:
  579. argument_dict = argument_dict["kwargs"]
  580. for name, value in argument_dict.items():
  581. if name in bound_types:
  582. if value is not None and not isinstance(value, bound_types[name]):
  583. raise TypeError('Argument {} must be {}'.format(name, bound_types[name]))
  584. return func(*args, **kwargs)
  585. return wrapper
  586. return type_check