bigdl-core-npu 2.6.0b20250114__cp310-cp310-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (234) hide show
  1. bigdl-core-npu/__init__.py +0 -0
  2. bigdl-core-npu/include/common.h +96 -0
  3. bigdl-core-npu/include/npu_llm.h +74 -0
  4. bigdl-core-npu/npu_llm.dll +0 -0
  5. bigdl-core-npu/npu_llm.lib +0 -0
  6. bigdl_core_npu-2.6.0b20250114.dist-info/METADATA +44 -0
  7. bigdl_core_npu-2.6.0b20250114.dist-info/RECORD +234 -0
  8. bigdl_core_npu-2.6.0b20250114.dist-info/WHEEL +5 -0
  9. bigdl_core_npu-2.6.0b20250114.dist-info/top_level.txt +2 -0
  10. intel_npu_acceleration_library/__init__.py +24 -0
  11. intel_npu_acceleration_library/_version.py +6 -0
  12. intel_npu_acceleration_library/backend/__init__.py +37 -0
  13. intel_npu_acceleration_library/backend/base.py +250 -0
  14. intel_npu_acceleration_library/backend/bindings.py +383 -0
  15. intel_npu_acceleration_library/backend/compression.py +24 -0
  16. intel_npu_acceleration_library/backend/convolution.py +58 -0
  17. intel_npu_acceleration_library/backend/factory.py +1161 -0
  18. intel_npu_acceleration_library/backend/linear.py +60 -0
  19. intel_npu_acceleration_library/backend/matmul.py +59 -0
  20. intel_npu_acceleration_library/backend/mlp.py +58 -0
  21. intel_npu_acceleration_library/backend/ops.py +142 -0
  22. intel_npu_acceleration_library/backend/qlinear.py +75 -0
  23. intel_npu_acceleration_library/backend/qmatmul.py +66 -0
  24. intel_npu_acceleration_library/backend/runtime.py +215 -0
  25. intel_npu_acceleration_library/backend/sdpa.py +107 -0
  26. intel_npu_acceleration_library/backend/tensor.py +1120 -0
  27. intel_npu_acceleration_library/backend/utils.py +70 -0
  28. intel_npu_acceleration_library/compiler.py +194 -0
  29. intel_npu_acceleration_library/device.py +230 -0
  30. intel_npu_acceleration_library/dtypes.py +155 -0
  31. intel_npu_acceleration_library/external/openvino/__init__.py +72 -0
  32. intel_npu_acceleration_library/external/openvino/_offline_transformations/__init__.py +21 -0
  33. intel_npu_acceleration_library/external/openvino/_pyopenvino.cp310-win_amd64.pyd +0 -0
  34. intel_npu_acceleration_library/external/openvino/_pyopenvino.cp311-win_amd64.pyd +0 -0
  35. intel_npu_acceleration_library/external/openvino/_pyopenvino.cp312-win_amd64.pyd +0 -0
  36. intel_npu_acceleration_library/external/openvino/_pyopenvino.cp38-win_amd64.pyd +0 -0
  37. intel_npu_acceleration_library/external/openvino/_pyopenvino.cp39-win_amd64.pyd +0 -0
  38. intel_npu_acceleration_library/external/openvino/experimental/__init__.py +14 -0
  39. intel_npu_acceleration_library/external/openvino/frontend/__init__.py +34 -0
  40. intel_npu_acceleration_library/external/openvino/frontend/frontend.py +44 -0
  41. intel_npu_acceleration_library/external/openvino/frontend/jax/__init__.py +15 -0
  42. intel_npu_acceleration_library/external/openvino/frontend/jax/jaxpr_decoder.py +293 -0
  43. intel_npu_acceleration_library/external/openvino/frontend/jax/passes.py +65 -0
  44. intel_npu_acceleration_library/external/openvino/frontend/jax/utils.py +182 -0
  45. intel_npu_acceleration_library/external/openvino/frontend/onnx/__init__.py +15 -0
  46. intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp310-win_amd64.pyd +0 -0
  47. intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp311-win_amd64.pyd +0 -0
  48. intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp312-win_amd64.pyd +0 -0
  49. intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp38-win_amd64.pyd +0 -0
  50. intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp39-win_amd64.pyd +0 -0
  51. intel_npu_acceleration_library/external/openvino/frontend/paddle/__init__.py +15 -0
  52. intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp310-win_amd64.pyd +0 -0
  53. intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp311-win_amd64.pyd +0 -0
  54. intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp312-win_amd64.pyd +0 -0
  55. intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp38-win_amd64.pyd +0 -0
  56. intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp39-win_amd64.pyd +0 -0
  57. intel_npu_acceleration_library/external/openvino/frontend/pytorch/__init__.py +19 -0
  58. intel_npu_acceleration_library/external/openvino/frontend/pytorch/fx_decoder.py +370 -0
  59. intel_npu_acceleration_library/external/openvino/frontend/pytorch/gptq.py +180 -0
  60. intel_npu_acceleration_library/external/openvino/frontend/pytorch/module_extension.py +39 -0
  61. intel_npu_acceleration_library/external/openvino/frontend/pytorch/patch_model.py +118 -0
  62. intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp310-win_amd64.pyd +0 -0
  63. intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp311-win_amd64.pyd +0 -0
  64. intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp312-win_amd64.pyd +0 -0
  65. intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp38-win_amd64.pyd +0 -0
  66. intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp39-win_amd64.pyd +0 -0
  67. intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/backend.py +131 -0
  68. intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/backend_utils.py +85 -0
  69. intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/compile.py +141 -0
  70. intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/decompositions.py +116 -0
  71. intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/execute.py +189 -0
  72. intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/op_support.py +290 -0
  73. intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/partition.py +126 -0
  74. intel_npu_acceleration_library/external/openvino/frontend/pytorch/ts_decoder.py +568 -0
  75. intel_npu_acceleration_library/external/openvino/frontend/pytorch/utils.py +258 -0
  76. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/__init__.py +16 -0
  77. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/graph_iterator.py +116 -0
  78. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/node_decoder.py +219 -0
  79. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp310-win_amd64.pyd +0 -0
  80. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp311-win_amd64.pyd +0 -0
  81. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp312-win_amd64.pyd +0 -0
  82. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp38-win_amd64.pyd +0 -0
  83. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp39-win_amd64.pyd +0 -0
  84. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/utils.py +481 -0
  85. intel_npu_acceleration_library/external/openvino/helpers/__init__.py +6 -0
  86. intel_npu_acceleration_library/external/openvino/helpers/packing.py +87 -0
  87. intel_npu_acceleration_library/external/openvino/preprocess/README.md +60 -0
  88. intel_npu_acceleration_library/external/openvino/preprocess/__init__.py +28 -0
  89. intel_npu_acceleration_library/external/openvino/preprocess/torchvision/__init__.py +15 -0
  90. intel_npu_acceleration_library/external/openvino/preprocess/torchvision/preprocess_converter.py +47 -0
  91. intel_npu_acceleration_library/external/openvino/preprocess/torchvision/requirements.txt +5 -0
  92. intel_npu_acceleration_library/external/openvino/preprocess/torchvision/torchvision_preprocessing.py +347 -0
  93. intel_npu_acceleration_library/external/openvino/properties/__init__.py +22 -0
  94. intel_npu_acceleration_library/external/openvino/properties/_properties.py +55 -0
  95. intel_npu_acceleration_library/external/openvino/properties/device/__init__.py +14 -0
  96. intel_npu_acceleration_library/external/openvino/properties/hint/__init__.py +15 -0
  97. intel_npu_acceleration_library/external/openvino/properties/intel_auto/__init__.py +12 -0
  98. intel_npu_acceleration_library/external/openvino/properties/intel_cpu/__init__.py +8 -0
  99. intel_npu_acceleration_library/external/openvino/properties/intel_gpu/__init__.py +12 -0
  100. intel_npu_acceleration_library/external/openvino/properties/intel_gpu/hint/__init__.py +11 -0
  101. intel_npu_acceleration_library/external/openvino/properties/log/__init__.py +11 -0
  102. intel_npu_acceleration_library/external/openvino/properties/streams/__init__.py +11 -0
  103. intel_npu_acceleration_library/external/openvino/runtime/__init__.py +85 -0
  104. intel_npu_acceleration_library/external/openvino/runtime/exceptions.py +17 -0
  105. intel_npu_acceleration_library/external/openvino/runtime/ie_api.py +631 -0
  106. intel_npu_acceleration_library/external/openvino/runtime/op/__init__.py +19 -0
  107. intel_npu_acceleration_library/external/openvino/runtime/op/util/__init__.py +22 -0
  108. intel_npu_acceleration_library/external/openvino/runtime/opset1/__init__.py +112 -0
  109. intel_npu_acceleration_library/external/openvino/runtime/opset1/ops.py +3068 -0
  110. intel_npu_acceleration_library/external/openvino/runtime/opset10/__init__.py +179 -0
  111. intel_npu_acceleration_library/external/openvino/runtime/opset10/ops.py +173 -0
  112. intel_npu_acceleration_library/external/openvino/runtime/opset11/__init__.py +179 -0
  113. intel_npu_acceleration_library/external/openvino/runtime/opset11/ops.py +107 -0
  114. intel_npu_acceleration_library/external/openvino/runtime/opset12/__init__.py +180 -0
  115. intel_npu_acceleration_library/external/openvino/runtime/opset12/ops.py +120 -0
  116. intel_npu_acceleration_library/external/openvino/runtime/opset13/__init__.py +188 -0
  117. intel_npu_acceleration_library/external/openvino/runtime/opset13/ops.py +398 -0
  118. intel_npu_acceleration_library/external/openvino/runtime/opset14/__init__.py +190 -0
  119. intel_npu_acceleration_library/external/openvino/runtime/opset14/ops.py +171 -0
  120. intel_npu_acceleration_library/external/openvino/runtime/opset15/__init__.py +17 -0
  121. intel_npu_acceleration_library/external/openvino/runtime/opset15/ops.py +276 -0
  122. intel_npu_acceleration_library/external/openvino/runtime/opset2/__init__.py +118 -0
  123. intel_npu_acceleration_library/external/openvino/runtime/opset2/ops.py +216 -0
  124. intel_npu_acceleration_library/external/openvino/runtime/opset3/__init__.py +134 -0
  125. intel_npu_acceleration_library/external/openvino/runtime/opset3/ops.py +638 -0
  126. intel_npu_acceleration_library/external/openvino/runtime/opset4/__init__.py +145 -0
  127. intel_npu_acceleration_library/external/openvino/runtime/opset4/ops.py +464 -0
  128. intel_npu_acceleration_library/external/openvino/runtime/opset5/__init__.py +152 -0
  129. intel_npu_acceleration_library/external/openvino/runtime/opset5/ops.py +372 -0
  130. intel_npu_acceleration_library/external/openvino/runtime/opset6/__init__.py +154 -0
  131. intel_npu_acceleration_library/external/openvino/runtime/opset6/ops.py +215 -0
  132. intel_npu_acceleration_library/external/openvino/runtime/opset7/__init__.py +158 -0
  133. intel_npu_acceleration_library/external/openvino/runtime/opset7/ops.py +169 -0
  134. intel_npu_acceleration_library/external/openvino/runtime/opset8/__init__.py +169 -0
  135. intel_npu_acceleration_library/external/openvino/runtime/opset8/ops.py +787 -0
  136. intel_npu_acceleration_library/external/openvino/runtime/opset9/__init__.py +175 -0
  137. intel_npu_acceleration_library/external/openvino/runtime/opset9/ops.py +341 -0
  138. intel_npu_acceleration_library/external/openvino/runtime/opset_utils.py +22 -0
  139. intel_npu_acceleration_library/external/openvino/runtime/passes/__init__.py +19 -0
  140. intel_npu_acceleration_library/external/openvino/runtime/passes/graph_rewrite.py +33 -0
  141. intel_npu_acceleration_library/external/openvino/runtime/passes/manager.py +26 -0
  142. intel_npu_acceleration_library/external/openvino/runtime/properties/__init__.py +40 -0
  143. intel_npu_acceleration_library/external/openvino/runtime/properties/hint/__init__.py +25 -0
  144. intel_npu_acceleration_library/external/openvino/runtime/utils/__init__.py +7 -0
  145. intel_npu_acceleration_library/external/openvino/runtime/utils/broadcasting.py +44 -0
  146. intel_npu_acceleration_library/external/openvino/runtime/utils/data_helpers/__init__.py +8 -0
  147. intel_npu_acceleration_library/external/openvino/runtime/utils/data_helpers/data_dispatcher.py +447 -0
  148. intel_npu_acceleration_library/external/openvino/runtime/utils/data_helpers/wrappers.py +148 -0
  149. intel_npu_acceleration_library/external/openvino/runtime/utils/decorators.py +156 -0
  150. intel_npu_acceleration_library/external/openvino/runtime/utils/input_validation.py +133 -0
  151. intel_npu_acceleration_library/external/openvino/runtime/utils/node_factory.py +127 -0
  152. intel_npu_acceleration_library/external/openvino/runtime/utils/reduction.py +25 -0
  153. intel_npu_acceleration_library/external/openvino/runtime/utils/types.py +175 -0
  154. intel_npu_acceleration_library/external/openvino/tools/__init__.py +4 -0
  155. intel_npu_acceleration_library/external/openvino/tools/benchmark/__init__.py +3 -0
  156. intel_npu_acceleration_library/external/openvino/tools/benchmark/benchmark.py +186 -0
  157. intel_npu_acceleration_library/external/openvino/tools/benchmark/main.py +695 -0
  158. intel_npu_acceleration_library/external/openvino/tools/benchmark/parameters.py +199 -0
  159. intel_npu_acceleration_library/external/openvino/tools/benchmark/utils/__init__.py +3 -0
  160. intel_npu_acceleration_library/external/openvino/tools/benchmark/utils/constants.py +26 -0
  161. intel_npu_acceleration_library/external/openvino/tools/benchmark/utils/inputs_filling.py +482 -0
  162. intel_npu_acceleration_library/external/openvino/tools/benchmark/utils/logging.py +8 -0
  163. intel_npu_acceleration_library/external/openvino/tools/benchmark/utils/statistics_report.py +296 -0
  164. intel_npu_acceleration_library/external/openvino/tools/benchmark/utils/utils.py +836 -0
  165. intel_npu_acceleration_library/external/openvino/tools/ovc/__init__.py +20 -0
  166. intel_npu_acceleration_library/external/openvino/tools/ovc/__main__.py +10 -0
  167. intel_npu_acceleration_library/external/openvino/tools/ovc/cli_parser.py +633 -0
  168. intel_npu_acceleration_library/external/openvino/tools/ovc/convert.py +102 -0
  169. intel_npu_acceleration_library/external/openvino/tools/ovc/convert_data_type.py +82 -0
  170. intel_npu_acceleration_library/external/openvino/tools/ovc/convert_impl.py +550 -0
  171. intel_npu_acceleration_library/external/openvino/tools/ovc/environment_setup_utils.py +50 -0
  172. intel_npu_acceleration_library/external/openvino/tools/ovc/error.py +49 -0
  173. intel_npu_acceleration_library/external/openvino/tools/ovc/get_ov_update_message.py +16 -0
  174. intel_npu_acceleration_library/external/openvino/tools/ovc/help.py +45 -0
  175. intel_npu_acceleration_library/external/openvino/tools/ovc/logger.py +91 -0
  176. intel_npu_acceleration_library/external/openvino/tools/ovc/main.py +40 -0
  177. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/__init__.py +2 -0
  178. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/analysis.py +46 -0
  179. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/check_config.py +57 -0
  180. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/extractor.py +447 -0
  181. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/jax_frontend_utils.py +19 -0
  182. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/layout_utils.py +73 -0
  183. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/moc_emit_ir.py +32 -0
  184. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/offline_transformations.py +107 -0
  185. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/paddle_frontend_utils.py +83 -0
  186. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/pipeline.py +298 -0
  187. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/preprocessing.py +220 -0
  188. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py +214 -0
  189. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/shape_utils.py +109 -0
  190. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/type_utils.py +82 -0
  191. intel_npu_acceleration_library/external/openvino/tools/ovc/ovc.py +13 -0
  192. intel_npu_acceleration_library/external/openvino/tools/ovc/telemetry_params.py +6 -0
  193. intel_npu_acceleration_library/external/openvino/tools/ovc/telemetry_stub.py +28 -0
  194. intel_npu_acceleration_library/external/openvino/tools/ovc/telemetry_utils.py +118 -0
  195. intel_npu_acceleration_library/external/openvino/tools/ovc/utils.py +196 -0
  196. intel_npu_acceleration_library/external/openvino/tools/ovc/version.py +80 -0
  197. intel_npu_acceleration_library/external/openvino/torch/__init__.py +5 -0
  198. intel_npu_acceleration_library/external/openvino/utils.py +115 -0
  199. intel_npu_acceleration_library/functional/__init__.py +8 -0
  200. intel_npu_acceleration_library/functional/scaled_dot_product_attention.py +47 -0
  201. intel_npu_acceleration_library/lib/Release/cache.json +113732 -0
  202. intel_npu_acceleration_library/lib/Release/intel_npu_acceleration_library.dll +0 -0
  203. intel_npu_acceleration_library/lib/Release/openvino.dll +0 -0
  204. intel_npu_acceleration_library/lib/Release/openvino_auto_batch_plugin.dll +0 -0
  205. intel_npu_acceleration_library/lib/Release/openvino_auto_plugin.dll +0 -0
  206. intel_npu_acceleration_library/lib/Release/openvino_c.dll +0 -0
  207. intel_npu_acceleration_library/lib/Release/openvino_hetero_plugin.dll +0 -0
  208. intel_npu_acceleration_library/lib/Release/openvino_intel_cpu_plugin.dll +0 -0
  209. intel_npu_acceleration_library/lib/Release/openvino_intel_gpu_plugin.dll +0 -0
  210. intel_npu_acceleration_library/lib/Release/openvino_intel_npu_plugin.dll +0 -0
  211. intel_npu_acceleration_library/lib/Release/openvino_ir_frontend.dll +0 -0
  212. intel_npu_acceleration_library/lib/Release/openvino_onnx_frontend.dll +0 -0
  213. intel_npu_acceleration_library/lib/Release/openvino_paddle_frontend.dll +0 -0
  214. intel_npu_acceleration_library/lib/Release/openvino_pytorch_frontend.dll +0 -0
  215. intel_npu_acceleration_library/lib/Release/openvino_tensorflow_frontend.dll +0 -0
  216. intel_npu_acceleration_library/lib/Release/openvino_tensorflow_lite_frontend.dll +0 -0
  217. intel_npu_acceleration_library/lib/Release/tbb12.dll +0 -0
  218. intel_npu_acceleration_library/lib/Release/tbb12_debug.dll +0 -0
  219. intel_npu_acceleration_library/lib/Release/tbbbind_2_5.dll +0 -0
  220. intel_npu_acceleration_library/lib/Release/tbbbind_2_5_debug.dll +0 -0
  221. intel_npu_acceleration_library/lib/Release/tbbmalloc.dll +0 -0
  222. intel_npu_acceleration_library/lib/Release/tbbmalloc_debug.dll +0 -0
  223. intel_npu_acceleration_library/lib/Release/tbbmalloc_proxy.dll +0 -0
  224. intel_npu_acceleration_library/lib/Release/tbbmalloc_proxy_debug.dll +0 -0
  225. intel_npu_acceleration_library/modelling.py +150 -0
  226. intel_npu_acceleration_library/nn/__init__.py +20 -0
  227. intel_npu_acceleration_library/nn/autograd.py +68 -0
  228. intel_npu_acceleration_library/nn/conv.py +257 -0
  229. intel_npu_acceleration_library/nn/functional.py +1207 -0
  230. intel_npu_acceleration_library/nn/linear.py +162 -0
  231. intel_npu_acceleration_library/nn/llm.py +417 -0
  232. intel_npu_acceleration_library/nn/module.py +393 -0
  233. intel_npu_acceleration_library/optimizations.py +157 -0
  234. intel_npu_acceleration_library/quantization.py +174 -0
@@ -0,0 +1,1207 @@
1
+ #
2
+ # Copyright © 2024 Intel Corporation
3
+ # SPDX-License-Identifier: Apache 2.0
4
+ #
5
+
6
+
7
+ from intel_npu_acceleration_library.backend.tensor import (
8
+ implements,
9
+ generate_op,
10
+ Tensor,
11
+ )
12
+ from typing import Optional, Sequence, Union
13
+ import torch
14
+
15
+
16
+ def __generate_activation(x: Tensor, op: str, out: Optional[Tensor] = None) -> Tensor:
17
+ """Generate an activation function.
18
+
19
+ Args:
20
+ x (Tensor): The input tensor.
21
+ op (str): The operation to perform.
22
+ out (Optional[Tensor], optional): The output tensor. Defaults to None.
23
+
24
+ Returns:
25
+ Tensor: The output tensor
26
+ """
27
+ out = generate_op(x, op)
28
+ return out
29
+
30
+
31
+ @implements(torch.log)
32
+ def log(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
33
+ """Return the natural logarithm of a tensor element-wise.
34
+
35
+ Args:
36
+ x (Tensor): The input tensor.
37
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
38
+
39
+ Returns:
40
+ Tensor: Output tensor.
41
+ """
42
+ return __generate_activation(x, "log", out)
43
+
44
+
45
+ @implements(torch.cos)
46
+ def cos(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
47
+ """Return the cosine of a tensor element-wise.
48
+
49
+ Args:
50
+ x (Tensor): The input tensor.
51
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
52
+
53
+ Returns:
54
+ Tensor: Output tensor.
55
+ """
56
+ return __generate_activation(x, "cos", out)
57
+
58
+
59
+ @implements(torch.tanh)
60
+ def tanh(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
61
+ """Return the hyperbolic tangent of a tensor element-wise.
62
+
63
+ Args:
64
+ x (Tensor): The input tensor.
65
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
66
+
67
+ Returns:
68
+ Tensor: Output tensor.
69
+ """
70
+ return __generate_activation(x, "tanh", out)
71
+
72
+
73
+ @implements(torch.sqrt)
74
+ def sqrt(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
75
+ """Return the square root of a tensor element-wise.
76
+
77
+ Args:
78
+ x (Tensor): The input tensor.
79
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
80
+
81
+ Returns:
82
+ Tensor: Output tensor.
83
+ """
84
+ return __generate_activation(x, "sqrt", out)
85
+
86
+
87
+ @implements(torch.abs)
88
+ def abs(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
89
+ """Return the absolute value of a tensor element-wise.
90
+
91
+ Args:
92
+ x (Tensor): The input tensor.
93
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
94
+
95
+ Returns:
96
+ Tensor: Output tensor.
97
+ """
98
+ return __generate_activation(x, "abs", out)
99
+
100
+
101
+ @implements(torch.acos)
102
+ def acos(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
103
+ """Return the arc-cosine value of a tensor element-wise.
104
+
105
+ Args:
106
+ x (Tensor): The input tensor.
107
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
108
+
109
+ Returns:
110
+ Tensor: Output tensor.
111
+ """
112
+ return __generate_activation(x, "acos", out)
113
+
114
+
115
+ @implements(torch.asin)
116
+ def asin(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
117
+ """Return the arc-sine value of a tensor element-wise.
118
+
119
+ Args:
120
+ x (Tensor): The input tensor.
121
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
122
+
123
+ Returns:
124
+ Tensor: Output tensor.
125
+ """
126
+ return __generate_activation(x, "asin", out)
127
+
128
+
129
+ @implements(torch.atan)
130
+ def atan(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
131
+ """Return the arc-tangent value of a tensor element-wise.
132
+
133
+ Args:
134
+ x (Tensor): The input tensor.
135
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
136
+
137
+ Returns:
138
+ Tensor: Output tensor.
139
+ """
140
+ return __generate_activation(x, "atan", out)
141
+
142
+
143
+ @implements(torch.cosh)
144
+ def cosh(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
145
+ """Return the hyperbolic-cosine value of a tensor element-wise.
146
+
147
+ Args:
148
+ x (Tensor): The input tensor.
149
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
150
+
151
+ Returns:
152
+ Tensor: Output tensor.
153
+ """
154
+ return __generate_activation(x, "cosh", out)
155
+
156
+
157
+ @implements(torch.erf)
158
+ def erf(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
159
+ """Return the erf function of a tensor element-wise.
160
+
161
+ Args:
162
+ x (Tensor): The input tensor.
163
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
164
+
165
+ Returns:
166
+ Tensor: Output tensor.
167
+ """
168
+ return __generate_activation(x, "erf", out)
169
+
170
+
171
+ @implements(torch.exp)
172
+ def exp(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
173
+ """Return the exp function of a tensor element-wise.
174
+
175
+ Args:
176
+ x (Tensor): The input tensor.
177
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
178
+
179
+ Returns:
180
+ Tensor: Output tensor.
181
+ """
182
+ return __generate_activation(x, "exp", out)
183
+
184
+
185
+ @implements(torch.floor)
186
+ def floor(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
187
+ """Return the floor function of a tensor element-wise.
188
+
189
+ Args:
190
+ x (Tensor): The input tensor.
191
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
192
+
193
+ Returns:
194
+ Tensor: Output tensor.
195
+ """
196
+ return __generate_activation(x, "floor", out)
197
+
198
+
199
+ @implements(torch.sin)
200
+ def sin(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
201
+ """Return the sine of a tensor element-wise.
202
+
203
+ Args:
204
+ x (Tensor): The input tensor.
205
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
206
+
207
+ Returns:
208
+ Tensor: Output tensor.
209
+ """
210
+ return __generate_activation(x, "sin", out)
211
+
212
+
213
+ @implements(torch.sinh)
214
+ def sinh(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
215
+ """Return the hyperbolic sine of a tensor element-wise.
216
+
217
+ Args:
218
+ x (Tensor): The input tensor.
219
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
220
+
221
+ Returns:
222
+ Tensor: Output tensor.
223
+ """
224
+ return __generate_activation(x, "sinh", out)
225
+
226
+
227
+ @implements(torch.tan)
228
+ def tan(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
229
+ """Return the tangent of a tensor element-wise.
230
+
231
+ Args:
232
+ x (Tensor): The input tensor.
233
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
234
+
235
+ Returns:
236
+ Tensor: Output tensor.
237
+ """
238
+ return __generate_activation(x, "tan", out)
239
+
240
+
241
+ @implements(torch.acosh)
242
+ def acosh(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
243
+ """Return the hyperbolic arc-cosine of a tensor element-wise.
244
+
245
+ Args:
246
+ x (Tensor): The input tensor.
247
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
248
+
249
+ Returns:
250
+ Tensor: Output tensor.
251
+ """
252
+ return __generate_activation(x, "acosh", out)
253
+
254
+
255
+ @implements(torch.asinh)
256
+ def asinh(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
257
+ """Return the hyperbolic arc-sine of a tensor element-wise.
258
+
259
+ Args:
260
+ x (Tensor): The input tensor.
261
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
262
+
263
+ Returns:
264
+ Tensor: Output tensor.
265
+ """
266
+ return __generate_activation(x, "asinh", out)
267
+
268
+
269
+ @implements(torch.atanh)
270
+ def atanh(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
271
+ """Return the hyperbolic arc-tangent of a tensor element-wise.
272
+
273
+ Args:
274
+ x (Tensor): The input tensor.
275
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
276
+
277
+ Returns:
278
+ Tensor: Output tensor.
279
+ """
280
+ return __generate_activation(x, "atanh", out)
281
+
282
+
283
+ @implements(torch.round)
284
+ def round(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
285
+ """Return the round value of a tensor element-wise.
286
+
287
+ Args:
288
+ x (Tensor): The input tensor.
289
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
290
+
291
+ Returns:
292
+ Tensor: Output tensor.
293
+ """
294
+ return __generate_activation(x, "round", out)
295
+
296
+
297
+ @implements(torch.sign)
298
+ def sign(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
299
+ """Return the sign of a tensor element-wise.
300
+
301
+ Args:
302
+ x (Tensor): The input tensor.
303
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
304
+
305
+ Returns:
306
+ Tensor: Output tensor.
307
+ """
308
+ return __generate_activation(x, "sign", out)
309
+
310
+
311
+ @implements(torch.nn.functional.linear)
312
+ def linear(input: Tensor, weight: Tensor, bias: Optional[Tensor] = None) -> Tensor:
313
+ """Apply a linear transformation to the incoming data: y = x * A^T + b.
314
+
315
+ Args:
316
+ input (Tensor): The input tensor.
317
+ weight (Tensor): The weight tensor.
318
+ bias (Optional[Tensor], optional): the bias tensor. Defaults to None.
319
+
320
+ Returns:
321
+ Tensor: Output tensor.
322
+ """
323
+ mm = generate_op([input, weight], "matmul", False, True)
324
+ if bias is not None:
325
+ return generate_op([mm, bias], "eltwise_add")
326
+ return mm
327
+
328
+
329
+ @implements(torch.addmm)
330
+ def addmm(
331
+ input: Tensor,
332
+ mat1: Tensor,
333
+ mat2: Tensor,
334
+ beta: float = 1,
335
+ alpha: float = 1,
336
+ out: Optional[Tensor] = None,
337
+ ) -> Tensor:
338
+ """Return the addmm of a tensor element-wise.
339
+
340
+ Args:
341
+ input (Tensor): The input tensor.
342
+ mat1 (Tensor): The first matrix tensor.
343
+ mat2 (Tensor): The second matrix tensor.
344
+ beta (float): The beta value. Defaults to 1.
345
+ alpha (float): The alpha value. Defaults to 1.
346
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
347
+
348
+ Returns:
349
+ Tensor: Output tensor.
350
+ """
351
+ out = beta * input + alpha * (mat1 @ mat2)
352
+ return out
353
+
354
+
355
+ @implements(torch.nn.functional.scaled_dot_product_attention)
356
+ def scaled_dot_product_attention(
357
+ query: torch.Tensor,
358
+ key: torch.Tensor,
359
+ value: torch.Tensor,
360
+ attn_mask: torch.Tensor = None,
361
+ dropout_p: float = 0.0,
362
+ is_causal: bool = False,
363
+ scale: Optional[float] = None,
364
+ ) -> torch.Tensor:
365
+ """Compute scaled dot product attention on query, key and value tensors, using an optional attention mask if passed, and applying dropout if a probability greater than 0.0 is specified.
366
+
367
+ Args:
368
+ query (Tensor): query tensor
369
+ key (Tensor): key tensor
370
+ value (Tensor): value tensor
371
+ attn_mask (Tensor, optional): attention mask tensor. Defaults to None.
372
+ dropout_p (float, optional): optional dropout. Defaults to 0.0.
373
+ is_causal (bool, optional): enable causal mask. Defaults to False.
374
+ scale (Optional[float], optional): custom scale. Defaults to None.
375
+
376
+ Raises:
377
+ RuntimeError: dropout_p != 0 is not supported yet, scale != 0 is not supported yet
378
+
379
+ Returns:
380
+ Tensor: output tensor
381
+ """
382
+ if dropout_p != 0:
383
+ raise RuntimeError("dropout_p != 0 is not supported yet")
384
+ if scale is not None:
385
+ raise RuntimeError("scale != 0 is not supported yet")
386
+
387
+ if attn_mask is None:
388
+ return generate_op(
389
+ [query, key, value], "scaled_dot_product_attention_simple", is_causal
390
+ )
391
+
392
+ return generate_op(
393
+ [query, key, value, attn_mask], "scaled_dot_product_attention", is_causal
394
+ )
395
+
396
+
397
+ @implements(torch.nn.functional.dropout)
398
+ def dropout(
399
+ input: Tensor, p: float = 0.5, training: bool = True, inplace: bool = False
400
+ ) -> Tensor:
401
+ """Return dropout operation.
402
+
403
+ Args:
404
+ input (Tensor): The input tensor.
405
+ p (float): The probability of an element to be zeroed. Defaults to 0.5.
406
+ training (bool): apply dropout if True. Defaults to True.
407
+ inplace (bool): apply dropout in place. Defaults to False.
408
+
409
+ Raises:
410
+ NotImplementedError: Training mode is not supported yet, Inplace mode is not supported yet
411
+
412
+ Returns:
413
+ Tensor: Output tensor.
414
+ """
415
+ if training:
416
+ raise NotImplementedError("Training mode is not supported yet")
417
+
418
+ if inplace:
419
+ raise NotImplementedError("Inplace mode is not supported yet")
420
+
421
+ return input
422
+
423
+
424
+ @implements(torch.nn.functional.layer_norm)
425
+ def layer_norm(
426
+ input: Tensor,
427
+ normalized_shape: Sequence[int],
428
+ weight: Optional[Tensor] = None,
429
+ bias: Optional[Tensor] = None,
430
+ eps: float = 1e-05,
431
+ ) -> Tensor:
432
+ """Return layer normalization operation.
433
+
434
+ Args:
435
+ input (Tensor): The input tensor.
436
+ normalized_shape (Sequence[int]): The shape of the normalized tensor.
437
+ weight (Optional[Tensor], optional): The weight tensor. Defaults to None.
438
+ bias (Optional[Tensor], optional): The bias tensor. Defaults to None.
439
+ eps (float): The epsilon value. Defaults to 1e-05.
440
+
441
+ Returns:
442
+ Tensor: Output tensor.
443
+ """
444
+ axis = input.shape.index(normalized_shape[0])
445
+ ln = generate_op([input], "normL2", axis, eps)
446
+ if weight:
447
+ ln = ln * weight
448
+
449
+ if bias:
450
+ ln = ln + bias
451
+
452
+ return ln
453
+
454
+
455
+ @implements(torch.ceil)
456
+ def ceil(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
457
+ """Return the ceil of a tensor element-wise.
458
+
459
+ Args:
460
+ x (Tensor): The input tensor.
461
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
462
+
463
+ Returns:
464
+ Tensor: Output tensor.
465
+ """
466
+ return __generate_activation(x, "ceiling", out)
467
+
468
+
469
+ @implements(torch.clamp)
470
+ def clamp(
471
+ x: Tensor,
472
+ min: Optional[float] = None,
473
+ max: Optional[float] = None,
474
+ out: Optional[Tensor] = None,
475
+ ) -> Tensor:
476
+ """Return the clamp of a tensor element-wise.
477
+
478
+ Args:
479
+ x (Tensor): The input tensor.
480
+ min (Optional[float], optional): The minimum value. Defaults to None.
481
+ max (Optional[float], optional): The maximum value. Defaults to None.
482
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
483
+
484
+ Returns:
485
+ Tensor: Output tensor.
486
+ """
487
+ if min is None:
488
+ min = x.dtype.min
489
+ if max is None:
490
+ max = x.dtype.max
491
+ out = generate_op(x, "clamp", min, max)
492
+ return out
493
+
494
+
495
+ @implements(torch.neg)
496
+ def neg(x: Tensor, out: Optional[Tensor] = None) -> Tensor:
497
+ """Return the negative of a tensor element-wise.
498
+
499
+ Args:
500
+ x (Tensor): The input tensor.
501
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
502
+
503
+ Returns:
504
+ Tensor: Output tensor.
505
+ """
506
+ return __generate_activation(x, "negative", out)
507
+
508
+
509
+ @implements(torch.add)
510
+ def add(
511
+ x: Tensor,
512
+ other: Union[Tensor, torch.Tensor, int, float],
513
+ alpha: float = 1,
514
+ out: Optional[Tensor] = None,
515
+ ) -> Tensor:
516
+ """Return the sum of two tensors element-wise.
517
+
518
+ Args:
519
+ x (Tensor): The input tensor.
520
+ other (Union[Tensor, torch.Tensor, int, float]): The other tensor.
521
+ alpha (float): The alpha value. Defaults to 1.
522
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
523
+
524
+ Returns:
525
+ Tensor: Output tensor.
526
+ """
527
+ if alpha != 1:
528
+ other = torch.mul(other, alpha)
529
+
530
+ out = generate_op([x, other], "eltwise_add")
531
+ return out
532
+
533
+
534
+ @implements(torch.sub)
535
+ def sub(
536
+ x: Tensor,
537
+ other: Union[Tensor, torch.Tensor, int, float],
538
+ alpha: float = 1,
539
+ out: Optional[Tensor] = None,
540
+ ) -> Tensor:
541
+ """Return the subtraction of two tensors element-wise.
542
+
543
+ Args:
544
+ x (Tensor): The input tensor.
545
+ other (Union[Tensor, torch.Tensor, int, float]): The other tensor.
546
+ alpha (float): The alpha value. Defaults to 1.
547
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
548
+
549
+ Returns:
550
+ Tensor: Output tensor.
551
+ """
552
+ if alpha != 1:
553
+ other = torch.mul(other, alpha)
554
+
555
+ return torch.add(x, torch.neg(other), out=out)
556
+
557
+
558
+ @implements(torch.mul)
559
+ def mul(
560
+ x: Tensor,
561
+ other: Union[Tensor, torch.Tensor, int, float],
562
+ out: Optional[Tensor] = None,
563
+ ) -> Tensor:
564
+ """Return the elementwise multiplication of two tensors.
565
+
566
+ Args:
567
+ x (Tensor): The input tensor.
568
+ other (Union[Tensor, torch.Tensor, int, float]): The other tensor.
569
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
570
+
571
+ Returns:
572
+ Tensor: Output tensor.
573
+ """
574
+ out = generate_op([x, other], "eltwise_mul")
575
+ return out
576
+
577
+
578
+ @implements(torch.div)
579
+ def div(
580
+ x: Tensor,
581
+ other: Union[Tensor, torch.Tensor, int, float],
582
+ rounding_mode: Optional[str] = None,
583
+ out: Optional[Tensor] = None,
584
+ ) -> Tensor:
585
+ """Return the elementwise division of two tensors.
586
+
587
+ Args:
588
+ x (Tensor): The input tensor.
589
+ other (Union[Tensor, torch.Tensor, int, float]): The other tensor.
590
+ rounding_mode (Optional[str]): The rounding mode. Defaults to None. Options are 'trunc', 'floor'.
591
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
592
+
593
+ Raises:
594
+ NotImplementedError: trunc is not supported yet
595
+
596
+ Returns:
597
+ Tensor: Output tensor.
598
+ """
599
+ out = generate_op([x, other], "eltwise_div")
600
+
601
+ if rounding_mode == "trunc":
602
+ raise NotImplementedError("trunc is not supported yet")
603
+ elif rounding_mode == "floor":
604
+ return torch.floor(out)
605
+ else:
606
+ return out
607
+
608
+
609
+ @implements(torch.unsqueeze)
610
+ def unsqueeze(x, dim: int) -> Tensor:
611
+ """Return the unsqueezed tensor.
612
+
613
+ Args:
614
+ x (Tensor): The input tensor.
615
+ dim (int): The dim to unsqueeze
616
+
617
+ Returns:
618
+ Tensor: The squeezed tensor.
619
+ """
620
+ return generate_op([x], "unsqueeze", dim)
621
+
622
+
623
+ @implements(torch.flatten)
624
+ def flatten(x, start_dim=0, end_dim=-1) -> "Tensor":
625
+ """
626
+ Flatten the tensor.
627
+
628
+ Args:
629
+ x (Tensor): The input tensor.
630
+ start_dim (int): The first dim to flatten. Defaults to 0.
631
+ end_dim (int): The last dim to flatten. Defaults to -1.
632
+
633
+ Returns:
634
+ Tensor: The flattened tensor.
635
+ """
636
+ return x.flatten(start_dim, end_dim)
637
+
638
+
639
+ @implements(torch.cat)
640
+ def cat(input: Sequence[Tensor], dim: int, out: Optional[Tensor] = None) -> Tensor:
641
+ """Return the concatenation of tensors given the desired output shape.
642
+
643
+ Args:
644
+ input (Sequence[Tensor]): The input tensors.
645
+ dim (int): The dimension to concatenation tensors along.
646
+ out (Optional[Tensor], optional): Output tensor. Defaults to None.
647
+
648
+ Returns:
649
+ Tensor: Output tensor.
650
+ """
651
+ if len(input) == 2:
652
+ tensor = generate_op([input[0], input[1]], "concat", axis=dim)
653
+ else:
654
+ tensor = torch.cat([input[0], input[1]], dim=dim)
655
+ for x in range(2, len(input)):
656
+ tensor = torch.cat([tensor, input[x]], dim=dim)
657
+ return tensor
658
+
659
+
660
+ @implements(torch.max)
661
+ def max(x, dim: Optional[int] = None, keep_dims: Optional[bool] = False) -> Tensor:
662
+ """Return the reduced max tensor.
663
+
664
+ Args:
665
+ x (Tensor): The input tensor.
666
+ dim (Optional[int], optional): The dim to reduce. Default is None, and all dimensions are reduced.
667
+ keep_dims (Optional[bool], optional): If set to 1 it holds axes that are used for reduction. Defaults to False.
668
+
669
+ Returns:
670
+ Tensor: The the reduced max tensor.
671
+ """
672
+ return generate_op(x, "reduce_max", reduction_axes=dim, keep_dims=keep_dims)
673
+
674
+
675
+ @implements(torch.mean)
676
+ def mean(
677
+ x,
678
+ dim: Optional[Union[int, Sequence[int]]] = None,
679
+ keep_dims: Optional[bool] = False,
680
+ dtype: Optional[torch.dtype] = None,
681
+ ) -> Tensor:
682
+ """Return the reduced mean tensor.
683
+
684
+ Args:
685
+ x (Tensor): The input tensor.
686
+ dim (Optional[Union[int, Sequence[int]]], optional): The dim(s) to reduce. Default is None, and all dimensions are reduced.
687
+ keep_dims (Optional[bool], optional): If set to 1 it holds axes that are used for reduction. Defaults to False.
688
+ dtype (Optional[torch.dtype], optional): The data type. Defaults to None.
689
+
690
+ Returns:
691
+ Tensor: The the reduced mean tensor.
692
+ """
693
+ mean = generate_op(x, "reduce_mean", reduction_axes=dim, keep_dims=keep_dims)
694
+ if dtype:
695
+ mean = mean.to(dtype)
696
+ return mean
697
+
698
+
699
+ @implements(torch.min)
700
+ def min(x, dim: Optional[int] = None, keep_dims: Optional[bool] = False) -> Tensor:
701
+ """Return the reduced min tensor.
702
+
703
+ Args:
704
+ x (Tensor): The input tensor.
705
+ dim (Optional[int], optional): The dim to reduce. Default is None, and all dimensions are reduced.
706
+ keep_dims (Optional[bool], optional): If set to 1 it holds axes that are used for reduction. Defaults to False.
707
+
708
+ Returns:
709
+ Tensor: The the reduced min tensor.
710
+ """
711
+ return generate_op(x, "reduce_min", reduction_axes=dim, keep_dims=keep_dims)
712
+
713
+
714
+ @implements(torch.prod)
715
+ def prod(
716
+ x,
717
+ dim: Optional[int] = None,
718
+ keep_dims: Optional[bool] = False,
719
+ dtype: Optional[torch.dtype] = None,
720
+ ) -> Tensor:
721
+ """Return the reduced product tensor.
722
+
723
+ Args:
724
+ x (Tensor): The input tensor.
725
+ dim (Optional[int], optional): The dim to reduce. Default is None, and all dimensions are reduced.
726
+ keep_dims (Optional[bool], optional): If set to 1 it holds axes that are used for reduction. Defaults to False.
727
+ dtype (Optional[torch.dtype], optional): The data type. Defaults to None.
728
+
729
+ Returns:
730
+ Tensor: The the reduced product tensor.
731
+ """
732
+ prod = generate_op(x, "reduce_prod", reduction_axes=dim, keep_dims=keep_dims)
733
+ if dtype:
734
+ prod = prod.to(dtype)
735
+ return prod
736
+
737
+
738
+ @implements(torch.sum)
739
+ def sum(
740
+ x,
741
+ dim: Optional[Union[int, Sequence[int]]] = None,
742
+ keep_dims: Optional[bool] = False,
743
+ dtype: Optional[torch.dtype] = None,
744
+ ) -> Tensor:
745
+ """Return the reduced sum tensor.
746
+
747
+ Args:
748
+ x (Tensor): The input tensor.
749
+ dim (Optional[Union[int, Sequence[int]]], optional): The dim(s) to reduce. Default is None, and all dimensions are reduced.
750
+ keep_dims (Optional[bool], optional): If set to 1 it holds axes that are used for reduction. Defaults to False.
751
+ dtype (Optional[torch.dtype], optional): The data type. Defaults to None.
752
+
753
+ Returns:
754
+ Tensor: The the reduced sum tensor.
755
+ """
756
+ sum = generate_op(x, "reduce_sum", reduction_axes=dim, keep_dims=keep_dims)
757
+ if dtype:
758
+ sum = sum.to(dtype)
759
+ return sum
760
+
761
+
762
+ # Functional activations
763
+
764
+
765
+ @implements(torch.nn.functional.elu)
766
+ def elu(x: Tensor, alpha: float = 1.0, inplace=False) -> Tensor:
767
+ """Return the elu of a tensor element-wise.
768
+
769
+ Args:
770
+ x (Tensor): The input tensor.
771
+ alpha (float): The alpha value. Defaults to 1.0.
772
+ inplace (bool): apply elu in place. Defaults to False.
773
+
774
+ Returns:
775
+ Tensor: Output tensor.
776
+ """
777
+ out = generate_op(x, "elu", alpha)
778
+ if inplace:
779
+ x = out
780
+ return out
781
+
782
+
783
+ @implements(torch.nn.functional.gelu)
784
+ def gelu(x: Tensor, approximate: str = "none") -> Tensor:
785
+ """Return the gelu of a tensor element-wise.
786
+
787
+ Args:
788
+ x (Tensor): The input tensor.
789
+ approximate (str): The approximation method. Defaults to 'none'. When the approximate argument is 'tanh', Gelu is estimated with tanh approximation. When the approximate argument is 'erf', Gelu is estimated with erf approximation. When the approximate argument is 'none', Gelu is estimated with the original formula.
790
+
791
+ Returns:
792
+ Tensor: Output tensor.
793
+ """
794
+ if approximate == "tanh":
795
+ return __generate_activation(x, "gelu")
796
+ else:
797
+ return __generate_activation(x, "gelu_erf")
798
+
799
+
800
+ @implements(torch.nn.functional.relu)
801
+ def relu(x: Tensor, inplace=False) -> Tensor:
802
+ """Return the relu of a tensor element-wise.
803
+
804
+ Args:
805
+ x (Tensor): The input tensor.
806
+ inplace (bool): apply elu in place. Defaults to False.
807
+
808
+ Returns:
809
+ Tensor: Output tensor.
810
+ """
811
+ out = generate_op(x, "relu")
812
+ if inplace:
813
+ x = out
814
+ return out
815
+
816
+
817
+ @implements(torch.nn.functional.sigmoid)
818
+ def sigmoid(x: Tensor) -> Tensor:
819
+ """Return the sigmoid of a tensor element-wise.
820
+
821
+ Args:
822
+ x (Tensor): The input tensor.
823
+
824
+ Returns:
825
+ Tensor: Output tensor.
826
+ """
827
+ return generate_op(x, "sigmoid")
828
+
829
+
830
+ @implements(torch.nn.functional.hardswish)
831
+ def hardswish(x: Tensor, inplace=False) -> Tensor:
832
+ """Return the hardswish of a tensor element-wise.
833
+
834
+ Args:
835
+ x (Tensor): The input tensor.
836
+ inplace (bool): apply elu in place. Defaults to False.
837
+
838
+ Returns:
839
+ Tensor: Output tensor.
840
+ """
841
+ out = generate_op(x, "hswish")
842
+ if inplace:
843
+ x = out
844
+ return out
845
+
846
+
847
+ @implements(torch.nn.functional.mish)
848
+ def mish(x: Tensor, inplace=False) -> Tensor:
849
+ """Return the mish of a tensor element-wise.
850
+
851
+ Args:
852
+ x (Tensor): The input tensor.
853
+ inplace (bool): apply elu in place. Defaults to False.
854
+
855
+ Returns:
856
+ Tensor: Output tensor.
857
+ """
858
+ out = generate_op(x, "mish")
859
+ if inplace:
860
+ x = out
861
+ return out
862
+
863
+
864
+ @implements(torch.nn.functional.softmax)
865
+ def softmax(
866
+ x: Tensor,
867
+ dim: Optional[int] = None,
868
+ _stacklevel=3,
869
+ dtype: Optional[torch.dtype] = None,
870
+ ) -> Tensor:
871
+ """Return the softmax of a tensor element-wise.
872
+
873
+ Args:
874
+ x (Tensor): The input tensor.
875
+ dim (int): The dim to apply softmax. Defaults to -1.
876
+ _stacklevel (int): The stack level. Defaults to 3.
877
+ dtype (torch.dtype): The data type. Defaults to None.
878
+
879
+
880
+ Returns:
881
+ Tensor: Output tensor.
882
+ """
883
+ if dim is None:
884
+ dim = -1
885
+ smax = generate_op(x, "softmax", dim)
886
+
887
+ if dtype:
888
+ smax = smax.to(dtype)
889
+ return smax
890
+
891
+
892
+ @implements(torch.nn.functional.softplus)
893
+ def softplus(x: Tensor, beta: float = 1, threshold: float = 20) -> Tensor:
894
+ """Return the softplus of a tensor element-wise.
895
+
896
+ Args:
897
+ x (Tensor): The input tensor.
898
+ beta (float): The beta value. Defaults to 1.
899
+ threshold (float): The threshold value. Defaults to 20.
900
+
901
+ Raises:
902
+ NotImplementedError: Only default values are supported
903
+
904
+ Returns:
905
+ Tensor: Output tensor.
906
+ """
907
+ if beta == 1 and threshold == 20:
908
+ return generate_op(x, "softplus")
909
+ else:
910
+ raise NotImplementedError("Only default values are supported")
911
+
912
+
913
+ @implements(torch.nn.functional.hardsigmoid)
914
+ def hardsigmoid(x: Tensor, inplace=False) -> Tensor:
915
+ """Return the hardsigmoid of a tensor element-wise.
916
+
917
+ Args:
918
+ x (Tensor): The input tensor.
919
+ inplace (bool): apply elu in place. Defaults to False.
920
+
921
+ Returns:
922
+ Tensor: Output tensor.
923
+ """
924
+ out = generate_op(x, "hsigmoid")
925
+ if inplace:
926
+ x = out
927
+ return out
928
+
929
+
930
+ @implements(torch.nn.functional.silu)
931
+ def silu(x: Tensor, inplace=False) -> Tensor:
932
+ """Return the silu of a tensor element-wise.
933
+
934
+ Args:
935
+ x (Tensor): The input tensor.
936
+ inplace (bool): apply elu in place. Defaults to False.
937
+
938
+ Returns:
939
+ Tensor: Output tensor.
940
+ """
941
+ out = x * generate_op(x, "sigmoid")
942
+ if inplace:
943
+ x = out
944
+ return out
945
+
946
+
947
+ @implements(torch.nn.functional.adaptive_avg_pool2d)
948
+ def adaptive_avg_pool2d(input: Tensor, output_size: Sequence[int]):
949
+ """Return the adaptive average pool2d of a tensor given the desired output shape.
950
+
951
+ Args:
952
+ input (Tensor): The input tensor.
953
+ output_size (Sequence[int]): The desired output shape.
954
+
955
+ Returns:
956
+ Tensor: Output tensor.
957
+ """
958
+ return generate_op([input, output_size], "adaptive_avg_pool")
959
+
960
+
961
+ @implements(torch.nn.functional.adaptive_max_pool2d)
962
+ def adaptive_max_pool2d(
963
+ input: Tensor, output_size: Sequence[int], return_indices: bool = False
964
+ ):
965
+ """Return the adaptive_max_pool2d of a tensor given the desired output shape.
966
+
967
+ Args:
968
+ input (Tensor): The input tensor.
969
+ output_size (Sequence[int]): The desired output shape.
970
+ return_indices (bool): Not supported yet. Defaults to False.
971
+
972
+ Raises:
973
+ NotImplementedError: return_indices is not supported yet
974
+
975
+ Returns:
976
+ Tensor: Output tensor.
977
+ """
978
+ if return_indices:
979
+ raise NotImplementedError("return_indices is not supported yet")
980
+ return generate_op([input, output_size], "adaptive_max_pool")
981
+
982
+
983
+ @implements(torch.nn.functional.avg_pool2d)
984
+ def avg_pool2d(
985
+ input: Tensor,
986
+ kernel_size: Union[int, Sequence[int]],
987
+ stride: Optional[Union[int, Sequence[int]]] = None,
988
+ padding: int = 0,
989
+ ceil_mode: bool = False,
990
+ count_include_pad: bool = True,
991
+ divisor_override: Optional[int] = None,
992
+ ):
993
+ """Generate an average pooling layer.
994
+
995
+ Args:
996
+ input (Tensor): layer input node
997
+ kernel_size (Sequence[int]): kernel size
998
+ stride (Sequence[int]): strides
999
+ padding (int): padding
1000
+ ceil_mode (bool): ceil mode
1001
+ count_include_pad (bool): count include pad
1002
+ divisor_override (int): divisor override
1003
+
1004
+ Returns:
1005
+ Tensor: output node
1006
+ """
1007
+ return generate_op(
1008
+ [input],
1009
+ "avg_pooling",
1010
+ kernel_size,
1011
+ stride,
1012
+ padding,
1013
+ ceil_mode,
1014
+ count_include_pad,
1015
+ divisor_override,
1016
+ 2,
1017
+ )
1018
+
1019
+
1020
+ @implements(torch.nn.functional.max_pool2d)
1021
+ def max_pool2d(
1022
+ input,
1023
+ kernel_size,
1024
+ stride=None,
1025
+ padding=0,
1026
+ dilation=1,
1027
+ ceil_mode=False,
1028
+ return_indices=False,
1029
+ ):
1030
+ """Generate an average pooling layer.
1031
+
1032
+ Args:
1033
+ input (ctypes._Pointer): layer input node
1034
+ kernel_size (Sequence[int]): kernel size
1035
+ stride (Sequence[int]): strides
1036
+ padding (int): padding
1037
+ dilation (int): dilation
1038
+ ceil_mode (bool): ceil mode
1039
+ return_indices (bool): return indices
1040
+
1041
+ Raises:
1042
+ NotImplementedError: return_indices and dilation are not supported
1043
+
1044
+ Returns:
1045
+ ctypes._Pointer: output node
1046
+ """
1047
+ if return_indices:
1048
+ raise NotImplementedError("return_indices is not supported yet")
1049
+
1050
+ if dilation != 1:
1051
+ raise NotImplementedError("dilation is not supported yet")
1052
+
1053
+ return generate_op(
1054
+ [input],
1055
+ "max_pooling",
1056
+ kernel_size,
1057
+ stride,
1058
+ padding,
1059
+ ceil_mode,
1060
+ 2,
1061
+ )
1062
+
1063
+
1064
+ @implements(torch.nn.functional.batch_norm)
1065
+ def batch_norm(
1066
+ input,
1067
+ running_mean,
1068
+ running_var,
1069
+ weight=None,
1070
+ bias=None,
1071
+ training=False,
1072
+ momentum=0.1,
1073
+ eps=1e-05,
1074
+ ):
1075
+ """Generate a batch normalization layer.
1076
+
1077
+ Args:
1078
+ input (Tensor): layer input node
1079
+ running_mean (Tensor): running mean
1080
+ running_var (Tensor): running variance
1081
+ weight (Tensor): weight
1082
+ bias (Tensor): bias
1083
+ training (bool): training
1084
+ momentum (float): momentum
1085
+ eps (float): epsilon
1086
+
1087
+ Raises:
1088
+ NotImplementedError: Training mode is not supported yet
1089
+
1090
+ Returns:
1091
+ Tensor: output node
1092
+ """
1093
+ if training:
1094
+ raise NotImplementedError("Training mode is not supported yet")
1095
+
1096
+ dtype = input.dtype.torch_dtype
1097
+ running_mean = running_mean.view(1, -1, 1, 1).to(dtype)
1098
+ running_var = running_var.view(1, -1, 1, 1).to(dtype)
1099
+
1100
+ result = (input - running_mean) / torch.sqrt(
1101
+ running_var + torch.tensor([eps]).to(dtype)
1102
+ ).to(dtype)
1103
+
1104
+ if weight is not None:
1105
+ result = result * weight.view(1, -1, 1, 1)
1106
+
1107
+ if bias is not None:
1108
+ result = result + bias.view(1, -1, 1, 1)
1109
+
1110
+ return result
1111
+
1112
+
1113
+ @implements(torch.nn.functional.conv2d)
1114
+ def conv2d(
1115
+ input: Tensor,
1116
+ weight: Union[Tensor, torch.Tensor],
1117
+ bias: Optional[Union[Tensor, torch.Tensor]] = None,
1118
+ stride: int = 1,
1119
+ padding: Union[int, str] = 0,
1120
+ dilation: int = 1,
1121
+ groups: int = 1,
1122
+ ) -> Tensor:
1123
+ """Generate a convolution layer.
1124
+
1125
+ Args:
1126
+ input (Tensor): layer input node
1127
+ weight (Union[Tensor, torch.Tensor]): weight
1128
+ bias (Union[Tensor, torch.Tensor]): bias
1129
+ stride (int): stride
1130
+ padding (Union[int, str]): padding
1131
+ dilation (int): dilation
1132
+ groups (int): groups
1133
+
1134
+ Raises:
1135
+ ValueError: Padding mode not supported
1136
+
1137
+ Returns:
1138
+ Tensor: output node
1139
+ """
1140
+ if isinstance(padding, str):
1141
+ if padding == "valid":
1142
+ padding = 0
1143
+ elif padding == "same":
1144
+ padding = weight.shape[2] // 2
1145
+ else:
1146
+ raise ValueError(f"Padding mode {padding} not supported")
1147
+
1148
+ if bias is not None:
1149
+ bias = bias.view((1, weight.shape[0], 1, 1))
1150
+
1151
+ if groups > 1:
1152
+ new_shape = [groups, weight.shape[0] // groups] + list(weight.shape[1:])
1153
+ weight = weight.view(new_shape)
1154
+
1155
+ conv = generate_op(
1156
+ [input, weight, bias],
1157
+ "convolution",
1158
+ strides=stride,
1159
+ padding=padding,
1160
+ dilation=dilation,
1161
+ groups=groups,
1162
+ )
1163
+
1164
+ return conv
1165
+
1166
+
1167
+ @implements(torch.pow)
1168
+ def pow(input: Tensor, exponent: Union[Tensor, torch.Tensor, float]):
1169
+ """Return the tensor raised to the power of the exponent.
1170
+
1171
+ Args:
1172
+ input (Tensor): The input tensor.
1173
+ exponent (Union[Tensor, torch.Tensor, float]): The exponent value.
1174
+
1175
+ Returns:
1176
+ Tensor: Output tensor.
1177
+ """
1178
+ if isinstance(exponent, float):
1179
+ exponent = torch.full(input.shape, exponent).to(torch.float16)
1180
+ return generate_op([input], "power", exponent=exponent)
1181
+
1182
+
1183
+ @implements(torch.nn.functional.log_softmax)
1184
+ def log_softmax(
1185
+ input: Tensor,
1186
+ dim: Optional[int] = None,
1187
+ _stacklevel=3,
1188
+ dtype: Optional[torch.dtype] = None,
1189
+ ) -> Tensor:
1190
+ """Return the log softmax of a tensor element-wise.
1191
+
1192
+ Args:
1193
+ input (Tensor): The input tensor.
1194
+ dim (int): The dimension along which log_softmax will be computed. Defaults to -1.
1195
+ _stacklevel (int): The stack level. Defaults to 3.
1196
+ dtype (torch.dtype): The data type. Defaults to None.
1197
+
1198
+ Returns:
1199
+ Tensor: Output tensor.
1200
+ """
1201
+ if dim is None:
1202
+ dim = -1
1203
+ log_smax = generate_op([input], "log_softmax", dim)
1204
+
1205
+ if dtype:
1206
+ log_smax = log_smax.to(dtype)
1207
+ return log_smax