bigdl-core-npu 2.5.0__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.
- bigdl_core_npu-2.5.0.dist-info/METADATA +35 -0
- bigdl_core_npu-2.5.0.dist-info/RECORD +223 -0
- bigdl_core_npu-2.5.0.dist-info/WHEEL +5 -0
- bigdl_core_npu-2.5.0.dist-info/top_level.txt +1 -0
- intel_npu_acceleration_library/__init__.py +24 -0
- intel_npu_acceleration_library/_version.py +6 -0
- intel_npu_acceleration_library/backend/__init__.py +37 -0
- intel_npu_acceleration_library/backend/base.py +215 -0
- intel_npu_acceleration_library/backend/bindings.py +279 -0
- intel_npu_acceleration_library/backend/compression.py +24 -0
- intel_npu_acceleration_library/backend/convolution.py +58 -0
- intel_npu_acceleration_library/backend/factory.py +944 -0
- intel_npu_acceleration_library/backend/linear.py +60 -0
- intel_npu_acceleration_library/backend/matmul.py +59 -0
- intel_npu_acceleration_library/backend/mlp.py +58 -0
- intel_npu_acceleration_library/backend/ops.py +141 -0
- intel_npu_acceleration_library/backend/qlinear.py +71 -0
- intel_npu_acceleration_library/backend/qmatmul.py +66 -0
- intel_npu_acceleration_library/backend/runtime.py +210 -0
- intel_npu_acceleration_library/backend/sdpa.py +107 -0
- intel_npu_acceleration_library/backend/tensor.py +1050 -0
- intel_npu_acceleration_library/backend/utils.py +70 -0
- intel_npu_acceleration_library/compiler.py +194 -0
- intel_npu_acceleration_library/device.py +230 -0
- intel_npu_acceleration_library/dtypes.py +122 -0
- intel_npu_acceleration_library/external/openvino/__init__.py +71 -0
- intel_npu_acceleration_library/external/openvino/_offline_transformations/__init__.py +20 -0
- intel_npu_acceleration_library/external/openvino/_pyopenvino.cp310-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/_pyopenvino.cp311-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/_pyopenvino.cp312-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/_pyopenvino.cp38-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/_pyopenvino.cp39-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/__init__.py +34 -0
- intel_npu_acceleration_library/external/openvino/frontend/frontend.py +44 -0
- intel_npu_acceleration_library/external/openvino/frontend/onnx/__init__.py +15 -0
- intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp310-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp311-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp312-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp38-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp39-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/paddle/__init__.py +15 -0
- intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp310-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp311-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp312-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp38-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp39-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/__init__.py +19 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/fx_decoder.py +352 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/gptq.py +139 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/module_extension.py +39 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/patch_model.py +98 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp310-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp311-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp312-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp38-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp39-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/backend.py +119 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/backend_utils.py +85 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/compile.py +141 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/decompositions.py +116 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/execute.py +189 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/op_support.py +289 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/partition.py +118 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/ts_decoder.py +536 -0
- intel_npu_acceleration_library/external/openvino/frontend/pytorch/utils.py +256 -0
- intel_npu_acceleration_library/external/openvino/frontend/tensorflow/__init__.py +16 -0
- intel_npu_acceleration_library/external/openvino/frontend/tensorflow/graph_iterator.py +116 -0
- intel_npu_acceleration_library/external/openvino/frontend/tensorflow/node_decoder.py +219 -0
- intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp310-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp311-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp312-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp38-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp39-win_amd64.pyd +0 -0
- intel_npu_acceleration_library/external/openvino/frontend/tensorflow/utils.py +460 -0
- intel_npu_acceleration_library/external/openvino/helpers/__init__.py +6 -0
- intel_npu_acceleration_library/external/openvino/helpers/packing.py +87 -0
- intel_npu_acceleration_library/external/openvino/preprocess/README.md +60 -0
- intel_npu_acceleration_library/external/openvino/preprocess/__init__.py +26 -0
- intel_npu_acceleration_library/external/openvino/preprocess/torchvision/__init__.py +15 -0
- intel_npu_acceleration_library/external/openvino/preprocess/torchvision/preprocess_converter.py +47 -0
- intel_npu_acceleration_library/external/openvino/preprocess/torchvision/requirements.txt +4 -0
- intel_npu_acceleration_library/external/openvino/preprocess/torchvision/torchvision_preprocessing.py +347 -0
- intel_npu_acceleration_library/external/openvino/properties/__init__.py +21 -0
- intel_npu_acceleration_library/external/openvino/properties/_properties.py +55 -0
- intel_npu_acceleration_library/external/openvino/properties/device/__init__.py +14 -0
- intel_npu_acceleration_library/external/openvino/properties/hint/__init__.py +15 -0
- intel_npu_acceleration_library/external/openvino/properties/intel_auto/__init__.py +12 -0
- intel_npu_acceleration_library/external/openvino/properties/intel_cpu/__init__.py +8 -0
- intel_npu_acceleration_library/external/openvino/properties/intel_gpu/__init__.py +12 -0
- intel_npu_acceleration_library/external/openvino/properties/intel_gpu/hint/__init__.py +11 -0
- intel_npu_acceleration_library/external/openvino/properties/log/__init__.py +11 -0
- intel_npu_acceleration_library/external/openvino/properties/streams/__init__.py +11 -0
- intel_npu_acceleration_library/external/openvino/runtime/__init__.py +85 -0
- intel_npu_acceleration_library/external/openvino/runtime/exceptions.py +17 -0
- intel_npu_acceleration_library/external/openvino/runtime/ie_api.py +631 -0
- intel_npu_acceleration_library/external/openvino/runtime/op/__init__.py +18 -0
- intel_npu_acceleration_library/external/openvino/runtime/op/util/__init__.py +22 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset1/__init__.py +112 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset1/ops.py +3067 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset10/__init__.py +179 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset10/ops.py +173 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset11/__init__.py +179 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset11/ops.py +107 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset12/__init__.py +180 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset12/ops.py +120 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset13/__init__.py +188 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset13/ops.py +399 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset14/__init__.py +190 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset14/ops.py +171 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset15/__init__.py +10 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset15/ops.py +85 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset2/__init__.py +118 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset2/ops.py +216 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset3/__init__.py +134 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset3/ops.py +638 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset4/__init__.py +145 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset4/ops.py +464 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset5/__init__.py +152 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset5/ops.py +372 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset6/__init__.py +154 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset6/ops.py +189 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset7/__init__.py +158 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset7/ops.py +169 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset8/__init__.py +169 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset8/ops.py +783 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset9/__init__.py +175 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset9/ops.py +341 -0
- intel_npu_acceleration_library/external/openvino/runtime/opset_utils.py +22 -0
- intel_npu_acceleration_library/external/openvino/runtime/passes/__init__.py +19 -0
- intel_npu_acceleration_library/external/openvino/runtime/passes/graph_rewrite.py +33 -0
- intel_npu_acceleration_library/external/openvino/runtime/passes/manager.py +26 -0
- intel_npu_acceleration_library/external/openvino/runtime/properties/__init__.py +38 -0
- intel_npu_acceleration_library/external/openvino/runtime/properties/hint/__init__.py +25 -0
- intel_npu_acceleration_library/external/openvino/runtime/utils/__init__.py +7 -0
- intel_npu_acceleration_library/external/openvino/runtime/utils/broadcasting.py +44 -0
- intel_npu_acceleration_library/external/openvino/runtime/utils/data_helpers/__init__.py +8 -0
- intel_npu_acceleration_library/external/openvino/runtime/utils/data_helpers/data_dispatcher.py +429 -0
- intel_npu_acceleration_library/external/openvino/runtime/utils/data_helpers/wrappers.py +148 -0
- intel_npu_acceleration_library/external/openvino/runtime/utils/decorators.py +70 -0
- intel_npu_acceleration_library/external/openvino/runtime/utils/input_validation.py +133 -0
- intel_npu_acceleration_library/external/openvino/runtime/utils/node_factory.py +127 -0
- intel_npu_acceleration_library/external/openvino/runtime/utils/reduction.py +25 -0
- intel_npu_acceleration_library/external/openvino/runtime/utils/types.py +175 -0
- intel_npu_acceleration_library/external/openvino/tools/__init__.py +4 -0
- intel_npu_acceleration_library/external/openvino/tools/benchmark/__init__.py +3 -0
- intel_npu_acceleration_library/external/openvino/tools/benchmark/benchmark.py +186 -0
- intel_npu_acceleration_library/external/openvino/tools/benchmark/main.py +695 -0
- intel_npu_acceleration_library/external/openvino/tools/benchmark/parameters.py +199 -0
- intel_npu_acceleration_library/external/openvino/tools/benchmark/utils/__init__.py +3 -0
- intel_npu_acceleration_library/external/openvino/tools/benchmark/utils/constants.py +26 -0
- intel_npu_acceleration_library/external/openvino/tools/benchmark/utils/inputs_filling.py +482 -0
- intel_npu_acceleration_library/external/openvino/tools/benchmark/utils/logging.py +8 -0
- intel_npu_acceleration_library/external/openvino/tools/benchmark/utils/statistics_report.py +296 -0
- intel_npu_acceleration_library/external/openvino/tools/benchmark/utils/utils.py +836 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/__init__.py +20 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/__main__.py +10 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/cli_parser.py +633 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/convert.py +102 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/convert_data_type.py +82 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/convert_impl.py +536 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/environment_setup_utils.py +50 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/error.py +49 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/get_ov_update_message.py +16 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/help.py +45 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/logger.py +91 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/main.py +35 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/__init__.py +2 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/analysis.py +46 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/check_config.py +57 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/extractor.py +447 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/layout_utils.py +73 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/moc_emit_ir.py +32 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/offline_transformations.py +107 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/paddle_frontend_utils.py +83 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/pipeline.py +246 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/preprocessing.py +220 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py +205 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/shape_utils.py +109 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/type_utils.py +82 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/ovc.py +13 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/telemetry_params.py +6 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/telemetry_stub.py +28 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/telemetry_utils.py +118 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/utils.py +109 -0
- intel_npu_acceleration_library/external/openvino/tools/ovc/version.py +80 -0
- intel_npu_acceleration_library/external/openvino/torch/__init__.py +5 -0
- intel_npu_acceleration_library/external/openvino/utils.py +98 -0
- intel_npu_acceleration_library/functional/__init__.py +8 -0
- intel_npu_acceleration_library/functional/scaled_dot_product_attention.py +47 -0
- intel_npu_acceleration_library/lib/Release/cache.json +113732 -0
- intel_npu_acceleration_library/lib/Release/intel_npu_acceleration_library.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino_auto_batch_plugin.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino_auto_plugin.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino_c.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino_hetero_plugin.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino_intel_cpu_plugin.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino_intel_gpu_plugin.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino_intel_npu_plugin.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino_ir_frontend.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino_onnx_frontend.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino_paddle_frontend.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino_pytorch_frontend.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino_tensorflow_frontend.dll +0 -0
- intel_npu_acceleration_library/lib/Release/openvino_tensorflow_lite_frontend.dll +0 -0
- intel_npu_acceleration_library/lib/Release/tbb12.dll +0 -0
- intel_npu_acceleration_library/lib/Release/tbb12_debug.dll +0 -0
- intel_npu_acceleration_library/lib/Release/tbbbind_2_5.dll +0 -0
- intel_npu_acceleration_library/lib/Release/tbbbind_2_5_debug.dll +0 -0
- intel_npu_acceleration_library/lib/Release/tbbmalloc.dll +0 -0
- intel_npu_acceleration_library/lib/Release/tbbmalloc_debug.dll +0 -0
- intel_npu_acceleration_library/lib/Release/tbbmalloc_proxy.dll +0 -0
- intel_npu_acceleration_library/lib/Release/tbbmalloc_proxy_debug.dll +0 -0
- intel_npu_acceleration_library/modelling.py +150 -0
- intel_npu_acceleration_library/nn/__init__.py +20 -0
- intel_npu_acceleration_library/nn/autograd.py +68 -0
- intel_npu_acceleration_library/nn/conv.py +257 -0
- intel_npu_acceleration_library/nn/functional.py +1207 -0
- intel_npu_acceleration_library/nn/linear.py +162 -0
- intel_npu_acceleration_library/nn/llm.py +417 -0
- intel_npu_acceleration_library/nn/module.py +393 -0
- intel_npu_acceleration_library/optimizations.py +157 -0
- intel_npu_acceleration_library/quantization.py +174 -0
@@ -0,0 +1,393 @@
|
|
1
|
+
#
|
2
|
+
# Copyright © 2024 Intel Corporation
|
3
|
+
# SPDX-License-Identifier: Apache 2.0
|
4
|
+
#
|
5
|
+
from intel_npu_acceleration_library.backend import NNFactory, Tensor
|
6
|
+
from typing import MutableMapping, Sequence, Any, List
|
7
|
+
import numpy as np
|
8
|
+
import torch
|
9
|
+
|
10
|
+
|
11
|
+
def pt_to_np_dtype(torch_dtype: torch.dtype) -> np.dtype:
|
12
|
+
"""Convert a PyTorch dtype to a NumPy dtype.
|
13
|
+
|
14
|
+
Args:
|
15
|
+
torch_dtype (torch.dtype): The PyTorch dtype to convert.
|
16
|
+
|
17
|
+
Raises:
|
18
|
+
ValueError: If the PyTorch dtype is not supported.
|
19
|
+
|
20
|
+
Returns:
|
21
|
+
np.dtype: The NumPy dtype.
|
22
|
+
"""
|
23
|
+
if torch_dtype == torch.float16:
|
24
|
+
return np.float16
|
25
|
+
elif torch_dtype == torch.float32:
|
26
|
+
return np.float32
|
27
|
+
elif torch_dtype == torch.float64:
|
28
|
+
return np.float64
|
29
|
+
elif torch_dtype == torch.int8:
|
30
|
+
return np.int8
|
31
|
+
elif torch_dtype == torch.int16:
|
32
|
+
return np.int16
|
33
|
+
elif torch_dtype == torch.int32:
|
34
|
+
return np.int32
|
35
|
+
elif torch_dtype == torch.int64:
|
36
|
+
return np.int64
|
37
|
+
else:
|
38
|
+
raise ValueError(f"Unsupported dtype {torch_dtype}")
|
39
|
+
|
40
|
+
|
41
|
+
def compute_input_signature(
|
42
|
+
args: Sequence[Any], kwargs: MutableMapping[str, Any]
|
43
|
+
) -> str:
|
44
|
+
"""Compute the input signature of a function call.
|
45
|
+
|
46
|
+
Args:
|
47
|
+
args (Sequence[Any]): The positional arguments.
|
48
|
+
kwargs (MutableMapping[str, Any]): The keyword arguments.
|
49
|
+
|
50
|
+
Returns:
|
51
|
+
str: The input signature.
|
52
|
+
"""
|
53
|
+
signature = []
|
54
|
+
for arg in args:
|
55
|
+
if isinstance(arg, torch.Tensor):
|
56
|
+
signature.append("_".join(str(dim) for dim in arg.shape))
|
57
|
+
signature.append(str(arg.dtype))
|
58
|
+
else:
|
59
|
+
signature.append(str(arg))
|
60
|
+
for k, arg in kwargs.items():
|
61
|
+
if isinstance(arg, torch.Tensor):
|
62
|
+
signature.append(str(k))
|
63
|
+
signature.append("_".join(str(dim) for dim in arg.shape))
|
64
|
+
signature.append(str(arg.dtype))
|
65
|
+
else:
|
66
|
+
signature.append(str(arg))
|
67
|
+
return "_".join(signature)
|
68
|
+
|
69
|
+
|
70
|
+
def patch_parameters(module: torch.nn.Module, model: NNFactory, recurse: bool = False):
|
71
|
+
"""Patch the parameters of a PyTorch module with constants.
|
72
|
+
|
73
|
+
Args:
|
74
|
+
module (torch.nn.Module): The PyTorch module.
|
75
|
+
model (NNFactory): The NNFactory instance.
|
76
|
+
recurse (bool, optional): Recurse over all submodules. Defaults to False.
|
77
|
+
"""
|
78
|
+
elements = list(module.named_parameters(recurse=recurse))
|
79
|
+
for name, param in elements:
|
80
|
+
del module._parameters[name]
|
81
|
+
setattr(module, name, model.constant(param.data.detach().numpy()))
|
82
|
+
|
83
|
+
buffers = list(module.named_buffers(recurse=recurse))
|
84
|
+
for name, param in buffers:
|
85
|
+
del module._buffers[name]
|
86
|
+
setattr(module, name, model.constant(param.data.detach().numpy()))
|
87
|
+
|
88
|
+
|
89
|
+
def patch_modules(module: torch.nn.Module, model: NNFactory):
|
90
|
+
"""Patch the modules of a PyTorch module with constants.
|
91
|
+
|
92
|
+
Args:
|
93
|
+
module (torch.nn.Module): The PyTorch module.
|
94
|
+
model (NNFactory): The NNFactory instance.
|
95
|
+
"""
|
96
|
+
modules = list(module.named_children())
|
97
|
+
for _, module in modules:
|
98
|
+
if isinstance(module, Module):
|
99
|
+
module.npu_top_level_module = False
|
100
|
+
# patch_parameters(module, model)
|
101
|
+
patch_modules(module, model)
|
102
|
+
|
103
|
+
|
104
|
+
class Module(torch.nn.Module):
|
105
|
+
"""A PyTorch module that runs on the NPU."""
|
106
|
+
|
107
|
+
def __init__(self) -> None:
|
108
|
+
"""Initialize the module."""
|
109
|
+
super().__init__()
|
110
|
+
self._nn_factory_cache: MutableMapping[str, NNFactory] = {}
|
111
|
+
self._npu_inference = False
|
112
|
+
self.npu_top_level_module = True
|
113
|
+
|
114
|
+
def extract_tensors_from_arguments(
|
115
|
+
self, args: Sequence[Any]
|
116
|
+
) -> Sequence[torch.Tensor]:
|
117
|
+
"""Extract the tensors from the arguments.
|
118
|
+
|
119
|
+
Args:
|
120
|
+
args (Sequence[Any]): The positional arguments.
|
121
|
+
|
122
|
+
Returns:
|
123
|
+
Sequence[torch.Tensor]: The tensors.
|
124
|
+
"""
|
125
|
+
tensors, non_tensors = [], []
|
126
|
+
for arg in args:
|
127
|
+
if isinstance(arg, torch.Tensor):
|
128
|
+
tensors.append(arg)
|
129
|
+
elif isinstance(arg, (list, tuple)):
|
130
|
+
tensor_list, non_tensor_list = self.extract_tensors_from_arguments(arg)
|
131
|
+
tensors.extend(tensor_list)
|
132
|
+
non_tensors.extend(non_tensor_list)
|
133
|
+
elif isinstance(arg, dict):
|
134
|
+
tensor_list, non_tensor_list = self.extract_tensors_from_arguments(
|
135
|
+
list(arg.values())
|
136
|
+
)
|
137
|
+
tensors.extend(tensor_list)
|
138
|
+
non_tensors.extend(non_tensor_list)
|
139
|
+
return tensors, non_tensors
|
140
|
+
|
141
|
+
def factory_forward(self, *args: Any, **kwargs: Any):
|
142
|
+
"""Run the model using the factory.
|
143
|
+
|
144
|
+
Args:
|
145
|
+
args (Any): The positional arguments.
|
146
|
+
kwargs (Any): The keyword arguments.
|
147
|
+
|
148
|
+
Returns:
|
149
|
+
torch.Tensor: The output tensor.
|
150
|
+
"""
|
151
|
+
signature = compute_input_signature(args, kwargs)
|
152
|
+
model = self._nn_factory_cache[signature]
|
153
|
+
|
154
|
+
tensor_args, non_tensor_args = self.extract_tensors_from_arguments(args)
|
155
|
+
tensor_args.extend(
|
156
|
+
self.extract_tensors_from_arguments(list(kwargs.values()))[0]
|
157
|
+
)
|
158
|
+
|
159
|
+
return model(*tensor_args, *non_tensor_args, **kwargs)
|
160
|
+
|
161
|
+
def create_model(
|
162
|
+
self, args: Sequence[Any], kwargs: MutableMapping[str, Any]
|
163
|
+
) -> NNFactory:
|
164
|
+
"""Create a model from the module.
|
165
|
+
|
166
|
+
Args:
|
167
|
+
args (Sequence[Any]): positional arguments
|
168
|
+
kwargs (MutableMapping[str, Any]): keyword arguments
|
169
|
+
|
170
|
+
Returns:
|
171
|
+
NNFactory: The model.
|
172
|
+
"""
|
173
|
+
model = NNFactory()
|
174
|
+
|
175
|
+
def create_args_from_list(args: Sequence[Any]) -> Sequence[Any]:
|
176
|
+
"""Create arguments from a list.
|
177
|
+
|
178
|
+
Args:
|
179
|
+
args (Sequence[Any]): The arguments.
|
180
|
+
|
181
|
+
Returns:
|
182
|
+
Sequence[Any]: The npu converted arguments.
|
183
|
+
"""
|
184
|
+
npu_args: List[Any] = []
|
185
|
+
for arg in args:
|
186
|
+
if isinstance(arg, torch.Tensor):
|
187
|
+
npu_args.append(
|
188
|
+
model.parameter(arg.shape, pt_to_np_dtype(arg.dtype))
|
189
|
+
)
|
190
|
+
elif isinstance(arg, (list, tuple)):
|
191
|
+
npu_args.append(create_args_from_list(arg))
|
192
|
+
elif isinstance(arg, dict):
|
193
|
+
npu_args.append(create_kwargs_from_list(arg))
|
194
|
+
else:
|
195
|
+
npu_args.append(arg)
|
196
|
+
return npu_args
|
197
|
+
|
198
|
+
def create_kwargs_from_list(
|
199
|
+
kwargs: MutableMapping[str, Any]
|
200
|
+
) -> MutableMapping[str, Any]:
|
201
|
+
"""Create keyword arguments from a list.
|
202
|
+
|
203
|
+
Args:
|
204
|
+
kwargs (MutableMapping[str, Any]): The keyword arguments.
|
205
|
+
|
206
|
+
Returns:
|
207
|
+
MutableMapping[str, Any]: The npu converted keyword arguments.
|
208
|
+
"""
|
209
|
+
npu_kwargs: MutableMapping[str, Any] = {}
|
210
|
+
for k, arg in kwargs.items():
|
211
|
+
if isinstance(arg, torch.Tensor):
|
212
|
+
npu_kwargs[k] = model.parameter(
|
213
|
+
arg.shape, pt_to_np_dtype(arg.dtype)
|
214
|
+
)
|
215
|
+
elif isinstance(arg, (list, tuple)):
|
216
|
+
npu_kwargs[k] = create_args_from_list(arg)
|
217
|
+
elif isinstance(arg, dict):
|
218
|
+
npu_kwargs[k] = create_kwargs_from_list(arg)
|
219
|
+
else:
|
220
|
+
npu_kwargs[k] = arg
|
221
|
+
return npu_kwargs
|
222
|
+
|
223
|
+
npu_args = create_args_from_list(args)
|
224
|
+
npu_kwargs = create_kwargs_from_list(kwargs)
|
225
|
+
|
226
|
+
patch_modules(self, model)
|
227
|
+
# patch_parameters(self, model)
|
228
|
+
|
229
|
+
_ = self.forward(*npu_args, **npu_kwargs)
|
230
|
+
model.compile()
|
231
|
+
return model
|
232
|
+
|
233
|
+
def _call_impl(self, *args: Any, **kwargs: Any) -> Any:
|
234
|
+
"""Call the module.
|
235
|
+
|
236
|
+
Args:
|
237
|
+
args (Any): The positional arguments.
|
238
|
+
kwargs (Any): The keyword arguments.
|
239
|
+
|
240
|
+
Returns:
|
241
|
+
Any: The output of the module.
|
242
|
+
"""
|
243
|
+
if self._npu_inference and self.npu_top_level_module:
|
244
|
+
|
245
|
+
signature = compute_input_signature(args, kwargs)
|
246
|
+
if signature not in self._nn_factory_cache:
|
247
|
+
self._nn_factory_cache[signature] = self.create_model(args, kwargs)
|
248
|
+
|
249
|
+
# Run the model by replacing the forward method with the factory_forward
|
250
|
+
old_forward = self.forward
|
251
|
+
self.forward = self.factory_forward # type: ignore
|
252
|
+
out = super()._call_impl(*args, **kwargs)
|
253
|
+
|
254
|
+
# Restore the original forward method
|
255
|
+
self.forward = old_forward # type: ignore
|
256
|
+
|
257
|
+
return out
|
258
|
+
else:
|
259
|
+
return super()._call_impl(*args, **kwargs)
|
260
|
+
|
261
|
+
def to(self, *args, **kwargs):
|
262
|
+
"""Move the module to a device or to a different dtype.
|
263
|
+
|
264
|
+
Args:
|
265
|
+
args (Any): The positional arguments.
|
266
|
+
kwargs (Any): The keyword arguments.
|
267
|
+
|
268
|
+
Returns:
|
269
|
+
torch.Tensor: The output tensor.
|
270
|
+
"""
|
271
|
+
device = kwargs.get("device", None)
|
272
|
+
args = list(args)
|
273
|
+
if device is None:
|
274
|
+
for idx, arg in enumerate(args):
|
275
|
+
if isinstance(arg, str) and arg.lower() in ["npu"]:
|
276
|
+
device = "npu"
|
277
|
+
args[idx] = "cpu"
|
278
|
+
else:
|
279
|
+
kwargs["device"] = "cpu"
|
280
|
+
|
281
|
+
if device.lower() == "npu":
|
282
|
+
self._npu_inference = True
|
283
|
+
|
284
|
+
return super().to(*args, **kwargs)
|
285
|
+
|
286
|
+
def forward(self, *args, **kwargs) -> torch.Tensor:
|
287
|
+
"""Run the forward pass of the module.
|
288
|
+
|
289
|
+
Args:
|
290
|
+
args (Any): The positional arguments.
|
291
|
+
kwargs (Any): The keyword arguments.
|
292
|
+
|
293
|
+
Raises:
|
294
|
+
NotImplementedError: If the forward method is not implemented.
|
295
|
+
|
296
|
+
Returns:
|
297
|
+
torch.Tensor: The output tensor.
|
298
|
+
"""
|
299
|
+
raise NotImplementedError
|
300
|
+
return torch.empty(0)
|
301
|
+
|
302
|
+
|
303
|
+
class NPUModuleWrapper(Module):
|
304
|
+
"""A PyTorch module that runs on the NPU."""
|
305
|
+
|
306
|
+
def __init__(self, module: torch.nn.Module) -> None:
|
307
|
+
"""Initialize the module.
|
308
|
+
|
309
|
+
Args:
|
310
|
+
module (torch.nn.Module): The PyTorch module.
|
311
|
+
"""
|
312
|
+
super().__init__()
|
313
|
+
self.module = module
|
314
|
+
|
315
|
+
def forward(self, *args, **kwargs) -> torch.Tensor:
|
316
|
+
"""Run the forward pass of the module.
|
317
|
+
|
318
|
+
Args:
|
319
|
+
args (Any): The positional arguments.
|
320
|
+
kwargs (Any): The keyword arguments.
|
321
|
+
|
322
|
+
Returns:
|
323
|
+
torch.Tensor: The output tensor.
|
324
|
+
"""
|
325
|
+
return self.module(*args, **kwargs)
|
326
|
+
|
327
|
+
|
328
|
+
def convert_to_npu_module(module: torch.nn.Module) -> Module:
|
329
|
+
"""Convert a PyTorch module to an NPU Module.
|
330
|
+
|
331
|
+
Args:
|
332
|
+
module (torch.nn.Module): The PyTorch module.
|
333
|
+
|
334
|
+
Returns:
|
335
|
+
Module: The NPU enabled Module.
|
336
|
+
"""
|
337
|
+
return NPUModuleWrapper(module).eval()
|
338
|
+
|
339
|
+
|
340
|
+
class NPUContextManager(NNFactory):
|
341
|
+
"""NPU context manager."""
|
342
|
+
|
343
|
+
def __enter__(self):
|
344
|
+
"""Enter the context.
|
345
|
+
|
346
|
+
Returns:
|
347
|
+
NPUContextManager: self
|
348
|
+
"""
|
349
|
+
return self
|
350
|
+
|
351
|
+
def Constant(self, tensor: torch.Tensor) -> Tensor:
|
352
|
+
"""Create a tensor.
|
353
|
+
|
354
|
+
Args:
|
355
|
+
tensor (torch.Tensor): tensor
|
356
|
+
|
357
|
+
Returns:
|
358
|
+
torch.Tensor: tensor
|
359
|
+
"""
|
360
|
+
return self.constant(tensor) # type: ignore
|
361
|
+
|
362
|
+
def Tensor(
|
363
|
+
self, shape: Sequence[int], dtype: torch.dtype = torch.float16
|
364
|
+
) -> Tensor:
|
365
|
+
"""Create a tensor.
|
366
|
+
|
367
|
+
Args:
|
368
|
+
shape (Sequence[int]): tensor shape
|
369
|
+
dtype (torch.dtype): tensor dtype, default to torch.float16
|
370
|
+
|
371
|
+
Returns:
|
372
|
+
Tensor: tensor
|
373
|
+
"""
|
374
|
+
return self.parameter(shape, dtype=dtype) # type: ignore
|
375
|
+
|
376
|
+
def __exit__(self, exc_type, exc_value, traceback):
|
377
|
+
"""Exit the context.
|
378
|
+
|
379
|
+
Args:
|
380
|
+
exc_type: exception type
|
381
|
+
exc_value: exception value
|
382
|
+
traceback: traceback
|
383
|
+
|
384
|
+
Raises:
|
385
|
+
RuntimeError: If an exception is raised.
|
386
|
+
"""
|
387
|
+
# If there is no exception, call the compile
|
388
|
+
if exc_type is None:
|
389
|
+
self.compile()
|
390
|
+
else:
|
391
|
+
# raise the exception
|
392
|
+
print(exc_type, exc_value, traceback)
|
393
|
+
raise RuntimeError(exc_value) # .with_traceback(traceback)
|
@@ -0,0 +1,157 @@
|
|
1
|
+
#
|
2
|
+
# Copyright © 2024 Intel Corporation
|
3
|
+
# SPDX-License-Identifier: Apache 2.0
|
4
|
+
#
|
5
|
+
from typing import Dict, List, Any
|
6
|
+
import torch.nn as nn
|
7
|
+
import torch.fx as fx
|
8
|
+
import operator
|
9
|
+
import torch
|
10
|
+
|
11
|
+
|
12
|
+
def delattr_recursively(module: nn.Module, target: str):
|
13
|
+
"""Delete attribute recursively by name in a torch.nn.Module.
|
14
|
+
|
15
|
+
Args:
|
16
|
+
module (nn.Module): the nn.Module
|
17
|
+
target (str): the attribute you want to delete
|
18
|
+
"""
|
19
|
+
*root, name = target.rsplit(".", 1)
|
20
|
+
if root:
|
21
|
+
root = root[0].split(".")
|
22
|
+
delattr_recursively(getattr(module, root[0]), ".".join(root[1:] + [name]))
|
23
|
+
else:
|
24
|
+
delattr(module, target)
|
25
|
+
|
26
|
+
|
27
|
+
def fuse_linear_layers(
|
28
|
+
model: nn.Module,
|
29
|
+
modules: Dict[str, nn.Linear],
|
30
|
+
targets: List[str],
|
31
|
+
fused_layer_name: str,
|
32
|
+
) -> None:
|
33
|
+
"""Fuse two linear layers and append them to the nn Module.
|
34
|
+
|
35
|
+
Args:
|
36
|
+
model (nn.Module): Origianl nn.Module object
|
37
|
+
modules (Dict[nn.Linear]): a dictiorany of node name: linear layer
|
38
|
+
targets (List[str]): list of layer node names
|
39
|
+
fused_layer_name (str): fused layer name
|
40
|
+
|
41
|
+
Raises:
|
42
|
+
ValueError: All linear layers must be of type nn.Linear and must have the same input dimension
|
43
|
+
|
44
|
+
"""
|
45
|
+
# Get the attributes
|
46
|
+
layers = [modules[name] for name in targets]
|
47
|
+
|
48
|
+
in_features = list({layer.in_features for layer in layers})
|
49
|
+
|
50
|
+
# ensure both linear layers have the same input dimensions and are not already fused
|
51
|
+
if not all(isinstance(layer, nn.Linear) for layer in layers):
|
52
|
+
raise ValueError("All linear layers must be of type nn.Linear")
|
53
|
+
if len(in_features) != 1:
|
54
|
+
raise ValueError(
|
55
|
+
f"All linear layers must have the same input dimensions. Instead found: {in_features}"
|
56
|
+
)
|
57
|
+
|
58
|
+
# Create the new fused linear layer
|
59
|
+
new_out_features = sum([layer.out_features for layer in layers])
|
60
|
+
has_bias = any(layer.bias is not None for layer in layers)
|
61
|
+
fused_layer = nn.Linear(in_features[0], new_out_features, bias=has_bias)
|
62
|
+
|
63
|
+
# Concatenate the weights and biases
|
64
|
+
with torch.no_grad():
|
65
|
+
start, stop = 0, 0
|
66
|
+
for layer in layers:
|
67
|
+
stop += layer.out_features
|
68
|
+
fused_layer.weight[start:stop, :] = layer.weight
|
69
|
+
|
70
|
+
if has_bias:
|
71
|
+
if layer.bias is not None:
|
72
|
+
fused_layer.bias[start:stop] = layer.bias
|
73
|
+
else:
|
74
|
+
fused_layer.bias[start:stop] = torch.zeros_like(
|
75
|
+
fused_layer.bias[start:stop]
|
76
|
+
)
|
77
|
+
start = stop
|
78
|
+
|
79
|
+
# Replace the two layers in the original model with the new fused layer
|
80
|
+
setattr(model, fused_layer_name, fused_layer)
|
81
|
+
for layer_name in targets:
|
82
|
+
delattr_recursively(model, layer_name)
|
83
|
+
|
84
|
+
|
85
|
+
def horizontal_fusion_linear(model: torch.nn.Module) -> torch.nn.Module:
|
86
|
+
"""Fuze horizontally two or more linear layers that share the same origin. This will increase NPU hw utilization.
|
87
|
+
|
88
|
+
Args:
|
89
|
+
model (torch.nn.Module): The original nn.Module
|
90
|
+
|
91
|
+
Returns:
|
92
|
+
torch.nn.Module: optimize nn.Module where parallel linear operations has been fused into a single bigger one
|
93
|
+
"""
|
94
|
+
fx_model = fx.symbolic_trace(model)
|
95
|
+
modules = dict(fx_model.named_modules())
|
96
|
+
# new_graph = copy.deepcopy(fx_model.graph)
|
97
|
+
|
98
|
+
def node_condition(node: Any) -> bool:
|
99
|
+
"""Return true if the node is a module and is nn.Linear.
|
100
|
+
|
101
|
+
Args:
|
102
|
+
node (Any): A torch fx node
|
103
|
+
|
104
|
+
Returns:
|
105
|
+
bool: return condition
|
106
|
+
"""
|
107
|
+
return node.op == "call_module" and isinstance(modules[node.target], nn.Linear)
|
108
|
+
|
109
|
+
# First, find all node with a linear layer
|
110
|
+
linear_nodes = [node for node in fx_model.graph.nodes if node_condition(node)]
|
111
|
+
|
112
|
+
# Group the linear layers by input node
|
113
|
+
linear_nodes_parents: Dict[str, List[Any]] = {}
|
114
|
+
for node in linear_nodes:
|
115
|
+
linear_nodes_parents.setdefault(node.args[0], []).append(node)
|
116
|
+
|
117
|
+
# Get the ones with size > 1
|
118
|
+
fused_modules = [
|
119
|
+
(source, modules)
|
120
|
+
for source, modules in linear_nodes_parents.items()
|
121
|
+
if len(modules) > 1
|
122
|
+
]
|
123
|
+
|
124
|
+
for source, layers in fused_modules:
|
125
|
+
fused_layer_name = "fused_" + "_".join(node.target for node in layers)
|
126
|
+
fused_layer_name = fused_layer_name.replace(".", "_")
|
127
|
+
fuse_linear_layers(
|
128
|
+
fx_model, modules, [layer.target for layer in layers], fused_layer_name
|
129
|
+
)
|
130
|
+
with fx_model.graph.inserting_after(source):
|
131
|
+
fused_node = fx_model.graph.call_module(fused_layer_name, (source,))
|
132
|
+
|
133
|
+
with fx_model.graph.inserting_after(fused_node):
|
134
|
+
|
135
|
+
start, stop = 0, 0
|
136
|
+
for layer in layers:
|
137
|
+
stop += modules[layer.target].out_features
|
138
|
+
|
139
|
+
layer_slice = fx_model.graph.call_function(
|
140
|
+
operator.getitem,
|
141
|
+
args=(
|
142
|
+
fused_node,
|
143
|
+
(
|
144
|
+
Ellipsis,
|
145
|
+
slice(start, stop, None),
|
146
|
+
),
|
147
|
+
),
|
148
|
+
kwargs={},
|
149
|
+
)
|
150
|
+
layer.replace_all_uses_with(layer_slice)
|
151
|
+
fx_model.graph.erase_node(layer)
|
152
|
+
start = stop
|
153
|
+
|
154
|
+
fx_model.graph.lint()
|
155
|
+
fx_model.recompile()
|
156
|
+
|
157
|
+
return fx_model
|