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 33 kB

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