introspect.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. Introspection helper functions.
  3. """
  4. __all__ = ['opt_func_info']
  5. def opt_func_info(func_name=None, signature=None):
  6. """
  7. Returns a dictionary containing the currently supported CPU dispatched
  8. features for all optimized functions.
  9. Parameters
  10. ----------
  11. func_name : str (optional)
  12. Regular expression to filter by function name.
  13. signature : str (optional)
  14. Regular expression to filter by data type.
  15. Returns
  16. -------
  17. dict
  18. A dictionary where keys are optimized function names and values are
  19. nested dictionaries indicating supported targets based on data types.
  20. Examples
  21. --------
  22. Retrieve dispatch information for functions named 'add' or 'sub' and
  23. data types 'float64' or 'float32':
  24. >>> import numpy as np
  25. >>> dict = np.lib.introspect.opt_func_info(
  26. ... func_name="add|abs", signature="float64|complex64"
  27. ... )
  28. >>> import json
  29. >>> print(json.dumps(dict, indent=2)) # may vary (architecture)
  30. {
  31. "absolute": {
  32. "dd": {
  33. "current": "SSE41",
  34. "available": "SSE41 baseline(SSE SSE2 SSE3)"
  35. },
  36. "Ff": {
  37. "current": "FMA3__AVX2",
  38. "available": "AVX512F FMA3__AVX2 baseline(SSE SSE2 SSE3)"
  39. },
  40. "Dd": {
  41. "current": "FMA3__AVX2",
  42. "available": "AVX512F FMA3__AVX2 baseline(SSE SSE2 SSE3)"
  43. }
  44. },
  45. "add": {
  46. "ddd": {
  47. "current": "FMA3__AVX2",
  48. "available": "FMA3__AVX2 baseline(SSE SSE2 SSE3)"
  49. },
  50. "FFF": {
  51. "current": "FMA3__AVX2",
  52. "available": "FMA3__AVX2 baseline(SSE SSE2 SSE3)"
  53. }
  54. }
  55. }
  56. """
  57. import re
  58. from numpy._core._multiarray_umath import __cpu_targets_info__ as targets, dtype
  59. if func_name is not None:
  60. func_pattern = re.compile(func_name)
  61. matching_funcs = {
  62. k: v for k, v in targets.items()
  63. if func_pattern.search(k)
  64. }
  65. else:
  66. matching_funcs = targets
  67. if signature is not None:
  68. sig_pattern = re.compile(signature)
  69. matching_sigs = {}
  70. for k, v in matching_funcs.items():
  71. matching_chars = {}
  72. for chars, targets in v.items():
  73. if any(
  74. sig_pattern.search(c) or sig_pattern.search(dtype(c).name)
  75. for c in chars
  76. ):
  77. matching_chars[chars] = targets # noqa: PERF403
  78. if matching_chars:
  79. matching_sigs[k] = matching_chars
  80. else:
  81. matching_sigs = matching_funcs
  82. return matching_sigs