Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- videollama2/lib/python3.10/site-packages/sklearn/neighbors/_base.py +1361 -0
- videollama2/lib/python3.10/site-packages/sklearn/neighbors/_distance_metric.py +20 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/ATenGeneral.h +3 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/ATen_pch.h +165 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/CheckMemoryFormat.h +25 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/Generator.h +191 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/NamedTensor.h +139 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/TensorAccessor.h +277 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/TensorBody.h +0 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/boxing/BoxedKernel_impl.h +99 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/boxing/KernelFunction_impl.h +229 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/boxing/impl/WrapFunctionIntoRuntimeFunctor.h +39 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/CppSignature.h +65 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/DispatchKeyExtractor.h +242 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/Dispatcher.h +793 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/ObservedOperators.h +17 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/OperatorEntry.h +313 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/RegistrationHandleRAII.h +36 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/function.h +114 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/op_registration/adaption.h +83 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/type_factory.h +108 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/core/type_ptr.h +54 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/functional.h +4 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/functional_base.h +358 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec.h +47 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vec256_complex_double.h +432 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vec256_complex_float.h +469 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vec256_convert.h +308 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vec256_float.h +656 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vec256_float_neon.h +909 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vec256_mask.h +298 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vec256_qint.h +1369 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_bfloat16_vsx.h +73 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_common_vsx.h +246 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_complex_double_vsx.h +584 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_complex_float_vsx.h +660 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_double_vsx.h +477 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_float_vsx.h +499 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_int16_vsx.h +402 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_int32_vsx.h +333 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_int64_vsx.h +286 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_qint32_vsx.h +281 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_qint8_vsx.h +483 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_quint8_vsx.h +501 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vsx_helpers.h +474 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/zarch/vec256_zarch.h +0 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec512/vec512_complex_double.h +513 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec512/vec512_complex_float.h +1019 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec512/vec512_convert.h +262 -0
- vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec512/vec512_double.h +472 -0
videollama2/lib/python3.10/site-packages/sklearn/neighbors/_base.py
ADDED
|
@@ -0,0 +1,1361 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Base and mixin classes for nearest neighbors."""
|
| 2 |
+
# Authors: Jake Vanderplas <vanderplas@astro.washington.edu>
|
| 3 |
+
# Fabian Pedregosa <fabian.pedregosa@inria.fr>
|
| 4 |
+
# Alexandre Gramfort <alexandre.gramfort@inria.fr>
|
| 5 |
+
# Sparseness support by Lars Buitinck
|
| 6 |
+
# Multi-output support by Arnaud Joly <a.joly@ulg.ac.be>
|
| 7 |
+
#
|
| 8 |
+
# License: BSD 3 clause (C) INRIA, University of Amsterdam
|
| 9 |
+
import itertools
|
| 10 |
+
from functools import partial
|
| 11 |
+
|
| 12 |
+
import warnings
|
| 13 |
+
from abc import ABCMeta, abstractmethod
|
| 14 |
+
import numbers
|
| 15 |
+
from numbers import Integral, Real
|
| 16 |
+
|
| 17 |
+
import numpy as np
|
| 18 |
+
from scipy.sparse import csr_matrix, issparse
|
| 19 |
+
from joblib import effective_n_jobs
|
| 20 |
+
|
| 21 |
+
from ._ball_tree import BallTree
|
| 22 |
+
from ._kd_tree import KDTree
|
| 23 |
+
from ..base import BaseEstimator, MultiOutputMixin
|
| 24 |
+
from ..base import is_classifier
|
| 25 |
+
from ..metrics import pairwise_distances_chunked
|
| 26 |
+
from ..metrics.pairwise import PAIRWISE_DISTANCE_FUNCTIONS
|
| 27 |
+
from ..metrics._pairwise_distances_reduction import (
|
| 28 |
+
ArgKmin,
|
| 29 |
+
RadiusNeighbors,
|
| 30 |
+
)
|
| 31 |
+
from ..utils import (
|
| 32 |
+
check_array,
|
| 33 |
+
gen_even_slices,
|
| 34 |
+
_to_object_array,
|
| 35 |
+
)
|
| 36 |
+
from ..utils.multiclass import check_classification_targets
|
| 37 |
+
from ..utils.validation import check_is_fitted
|
| 38 |
+
from ..utils.validation import check_non_negative
|
| 39 |
+
from ..utils._param_validation import Interval, StrOptions
|
| 40 |
+
from ..utils.parallel import delayed, Parallel
|
| 41 |
+
from ..utils.fixes import parse_version, sp_version
|
| 42 |
+
from ..exceptions import DataConversionWarning, EfficiencyWarning
|
| 43 |
+
|
| 44 |
+
VALID_METRICS = dict(
|
| 45 |
+
ball_tree=BallTree.valid_metrics,
|
| 46 |
+
kd_tree=KDTree.valid_metrics,
|
| 47 |
+
# The following list comes from the
|
| 48 |
+
# sklearn.metrics.pairwise doc string
|
| 49 |
+
brute=sorted(
|
| 50 |
+
set(PAIRWISE_DISTANCE_FUNCTIONS).union(
|
| 51 |
+
[
|
| 52 |
+
"braycurtis",
|
| 53 |
+
"canberra",
|
| 54 |
+
"chebyshev",
|
| 55 |
+
"correlation",
|
| 56 |
+
"cosine",
|
| 57 |
+
"dice",
|
| 58 |
+
"hamming",
|
| 59 |
+
"jaccard",
|
| 60 |
+
"kulsinski",
|
| 61 |
+
"mahalanobis",
|
| 62 |
+
"matching",
|
| 63 |
+
"minkowski",
|
| 64 |
+
"rogerstanimoto",
|
| 65 |
+
"russellrao",
|
| 66 |
+
"seuclidean",
|
| 67 |
+
"sokalmichener",
|
| 68 |
+
"sokalsneath",
|
| 69 |
+
"sqeuclidean",
|
| 70 |
+
"yule",
|
| 71 |
+
]
|
| 72 |
+
)
|
| 73 |
+
),
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# TODO: Remove filterwarnings in 1.3 when wminkowski is removed
|
| 77 |
+
if sp_version < parse_version("1.8.0.dev0"):
|
| 78 |
+
# Before scipy 1.8.0.dev0, wminkowski was the key to use
|
| 79 |
+
# the weighted minkowski metric.
|
| 80 |
+
VALID_METRICS["brute"].append("wminkowski")
|
| 81 |
+
|
| 82 |
+
VALID_METRICS_SPARSE = dict(
|
| 83 |
+
ball_tree=[],
|
| 84 |
+
kd_tree=[],
|
| 85 |
+
brute=(PAIRWISE_DISTANCE_FUNCTIONS.keys() - {"haversine", "nan_euclidean"}),
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def _get_weights(dist, weights):
|
| 90 |
+
"""Get the weights from an array of distances and a parameter ``weights``.
|
| 91 |
+
|
| 92 |
+
Assume weights have already been validated.
|
| 93 |
+
|
| 94 |
+
Parameters
|
| 95 |
+
----------
|
| 96 |
+
dist : ndarray
|
| 97 |
+
The input distances.
|
| 98 |
+
|
| 99 |
+
weights : {'uniform', 'distance'}, callable or None
|
| 100 |
+
The kind of weighting used.
|
| 101 |
+
|
| 102 |
+
Returns
|
| 103 |
+
-------
|
| 104 |
+
weights_arr : array of the same shape as ``dist``
|
| 105 |
+
If ``weights == 'uniform'``, then returns None.
|
| 106 |
+
"""
|
| 107 |
+
if weights in (None, "uniform"):
|
| 108 |
+
return None
|
| 109 |
+
|
| 110 |
+
if weights == "distance":
|
| 111 |
+
# if user attempts to classify a point that was zero distance from one
|
| 112 |
+
# or more training points, those training points are weighted as 1.0
|
| 113 |
+
# and the other points as 0.0
|
| 114 |
+
if dist.dtype is np.dtype(object):
|
| 115 |
+
for point_dist_i, point_dist in enumerate(dist):
|
| 116 |
+
# check if point_dist is iterable
|
| 117 |
+
# (ex: RadiusNeighborClassifier.predict may set an element of
|
| 118 |
+
# dist to 1e-6 to represent an 'outlier')
|
| 119 |
+
if hasattr(point_dist, "__contains__") and 0.0 in point_dist:
|
| 120 |
+
dist[point_dist_i] = point_dist == 0.0
|
| 121 |
+
else:
|
| 122 |
+
dist[point_dist_i] = 1.0 / point_dist
|
| 123 |
+
else:
|
| 124 |
+
with np.errstate(divide="ignore"):
|
| 125 |
+
dist = 1.0 / dist
|
| 126 |
+
inf_mask = np.isinf(dist)
|
| 127 |
+
inf_row = np.any(inf_mask, axis=1)
|
| 128 |
+
dist[inf_row] = inf_mask[inf_row]
|
| 129 |
+
return dist
|
| 130 |
+
|
| 131 |
+
if callable(weights):
|
| 132 |
+
return weights(dist)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def _is_sorted_by_data(graph):
|
| 136 |
+
"""Return whether the graph's non-zero entries are sorted by data.
|
| 137 |
+
|
| 138 |
+
The non-zero entries are stored in graph.data and graph.indices.
|
| 139 |
+
For each row (or sample), the non-zero entries can be either:
|
| 140 |
+
- sorted by indices, as after graph.sort_indices();
|
| 141 |
+
- sorted by data, as after _check_precomputed(graph);
|
| 142 |
+
- not sorted.
|
| 143 |
+
|
| 144 |
+
Parameters
|
| 145 |
+
----------
|
| 146 |
+
graph : sparse matrix of shape (n_samples, n_samples)
|
| 147 |
+
Neighbors graph as given by `kneighbors_graph` or
|
| 148 |
+
`radius_neighbors_graph`. Matrix should be of format CSR format.
|
| 149 |
+
|
| 150 |
+
Returns
|
| 151 |
+
-------
|
| 152 |
+
res : bool
|
| 153 |
+
Whether input graph is sorted by data.
|
| 154 |
+
"""
|
| 155 |
+
assert graph.format == "csr"
|
| 156 |
+
out_of_order = graph.data[:-1] > graph.data[1:]
|
| 157 |
+
line_change = np.unique(graph.indptr[1:-1] - 1)
|
| 158 |
+
line_change = line_change[line_change < out_of_order.shape[0]]
|
| 159 |
+
return out_of_order.sum() == out_of_order[line_change].sum()
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def _check_precomputed(X):
|
| 163 |
+
"""Check precomputed distance matrix.
|
| 164 |
+
|
| 165 |
+
If the precomputed distance matrix is sparse, it checks that the non-zero
|
| 166 |
+
entries are sorted by distances. If not, the matrix is copied and sorted.
|
| 167 |
+
|
| 168 |
+
Parameters
|
| 169 |
+
----------
|
| 170 |
+
X : {sparse matrix, array-like}, (n_samples, n_samples)
|
| 171 |
+
Distance matrix to other samples. X may be a sparse matrix, in which
|
| 172 |
+
case only non-zero elements may be considered neighbors.
|
| 173 |
+
|
| 174 |
+
Returns
|
| 175 |
+
-------
|
| 176 |
+
X : {sparse matrix, array-like}, (n_samples, n_samples)
|
| 177 |
+
Distance matrix to other samples. X may be a sparse matrix, in which
|
| 178 |
+
case only non-zero elements may be considered neighbors.
|
| 179 |
+
"""
|
| 180 |
+
if not issparse(X):
|
| 181 |
+
X = check_array(X)
|
| 182 |
+
check_non_negative(X, whom="precomputed distance matrix.")
|
| 183 |
+
return X
|
| 184 |
+
else:
|
| 185 |
+
graph = X
|
| 186 |
+
|
| 187 |
+
if graph.format not in ("csr", "csc", "coo", "lil"):
|
| 188 |
+
raise TypeError(
|
| 189 |
+
"Sparse matrix in {!r} format is not supported due to "
|
| 190 |
+
"its handling of explicit zeros".format(graph.format)
|
| 191 |
+
)
|
| 192 |
+
copied = graph.format != "csr"
|
| 193 |
+
graph = check_array(graph, accept_sparse="csr")
|
| 194 |
+
check_non_negative(graph, whom="precomputed distance matrix.")
|
| 195 |
+
graph = sort_graph_by_row_values(graph, copy=not copied, warn_when_not_sorted=True)
|
| 196 |
+
|
| 197 |
+
return graph
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
def sort_graph_by_row_values(graph, copy=False, warn_when_not_sorted=True):
|
| 201 |
+
"""Sort a sparse graph such that each row is stored with increasing values.
|
| 202 |
+
|
| 203 |
+
.. versionadded:: 1.2
|
| 204 |
+
|
| 205 |
+
Parameters
|
| 206 |
+
----------
|
| 207 |
+
graph : sparse matrix of shape (n_samples, n_samples)
|
| 208 |
+
Distance matrix to other samples, where only non-zero elements are
|
| 209 |
+
considered neighbors. Matrix is converted to CSR format if not already.
|
| 210 |
+
|
| 211 |
+
copy : bool, default=False
|
| 212 |
+
If True, the graph is copied before sorting. If False, the sorting is
|
| 213 |
+
performed inplace. If the graph is not of CSR format, `copy` must be
|
| 214 |
+
True to allow the conversion to CSR format, otherwise an error is
|
| 215 |
+
raised.
|
| 216 |
+
|
| 217 |
+
warn_when_not_sorted : bool, default=True
|
| 218 |
+
If True, a :class:`~sklearn.exceptions.EfficiencyWarning` is raised
|
| 219 |
+
when the input graph is not sorted by row values.
|
| 220 |
+
|
| 221 |
+
Returns
|
| 222 |
+
-------
|
| 223 |
+
graph : sparse matrix of shape (n_samples, n_samples)
|
| 224 |
+
Distance matrix to other samples, where only non-zero elements are
|
| 225 |
+
considered neighbors. Matrix is in CSR format.
|
| 226 |
+
"""
|
| 227 |
+
if not issparse(graph):
|
| 228 |
+
raise TypeError(f"Input graph must be a sparse matrix, got {graph!r} instead.")
|
| 229 |
+
|
| 230 |
+
if graph.format == "csr" and _is_sorted_by_data(graph):
|
| 231 |
+
return graph
|
| 232 |
+
|
| 233 |
+
if warn_when_not_sorted:
|
| 234 |
+
warnings.warn(
|
| 235 |
+
"Precomputed sparse input was not sorted by row values. Use the function"
|
| 236 |
+
" sklearn.neighbors.sort_graph_by_row_values to sort the input by row"
|
| 237 |
+
" values, with warn_when_not_sorted=False to remove this warning.",
|
| 238 |
+
EfficiencyWarning,
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
if graph.format not in ("csr", "csc", "coo", "lil"):
|
| 242 |
+
raise TypeError(
|
| 243 |
+
f"Sparse matrix in {graph.format!r} format is not supported due to "
|
| 244 |
+
"its handling of explicit zeros"
|
| 245 |
+
)
|
| 246 |
+
elif graph.format != "csr":
|
| 247 |
+
if not copy:
|
| 248 |
+
raise ValueError(
|
| 249 |
+
"The input graph is not in CSR format. Use copy=True to allow "
|
| 250 |
+
"the conversion to CSR format."
|
| 251 |
+
)
|
| 252 |
+
graph = graph.asformat("csr")
|
| 253 |
+
elif copy: # csr format with copy=True
|
| 254 |
+
graph = graph.copy()
|
| 255 |
+
|
| 256 |
+
row_nnz = np.diff(graph.indptr)
|
| 257 |
+
if row_nnz.max() == row_nnz.min():
|
| 258 |
+
# if each sample has the same number of provided neighbors
|
| 259 |
+
n_samples = graph.shape[0]
|
| 260 |
+
distances = graph.data.reshape(n_samples, -1)
|
| 261 |
+
|
| 262 |
+
order = np.argsort(distances, kind="mergesort")
|
| 263 |
+
order += np.arange(n_samples)[:, None] * row_nnz[0]
|
| 264 |
+
order = order.ravel()
|
| 265 |
+
graph.data = graph.data[order]
|
| 266 |
+
graph.indices = graph.indices[order]
|
| 267 |
+
|
| 268 |
+
else:
|
| 269 |
+
for start, stop in zip(graph.indptr, graph.indptr[1:]):
|
| 270 |
+
order = np.argsort(graph.data[start:stop], kind="mergesort")
|
| 271 |
+
graph.data[start:stop] = graph.data[start:stop][order]
|
| 272 |
+
graph.indices[start:stop] = graph.indices[start:stop][order]
|
| 273 |
+
|
| 274 |
+
return graph
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
def _kneighbors_from_graph(graph, n_neighbors, return_distance):
|
| 278 |
+
"""Decompose a nearest neighbors sparse graph into distances and indices.
|
| 279 |
+
|
| 280 |
+
Parameters
|
| 281 |
+
----------
|
| 282 |
+
graph : sparse matrix of shape (n_samples, n_samples)
|
| 283 |
+
Neighbors graph as given by `kneighbors_graph` or
|
| 284 |
+
`radius_neighbors_graph`. Matrix should be of format CSR format.
|
| 285 |
+
|
| 286 |
+
n_neighbors : int
|
| 287 |
+
Number of neighbors required for each sample.
|
| 288 |
+
|
| 289 |
+
return_distance : bool
|
| 290 |
+
Whether or not to return the distances.
|
| 291 |
+
|
| 292 |
+
Returns
|
| 293 |
+
-------
|
| 294 |
+
neigh_dist : ndarray of shape (n_samples, n_neighbors)
|
| 295 |
+
Distances to nearest neighbors. Only present if `return_distance=True`.
|
| 296 |
+
|
| 297 |
+
neigh_ind : ndarray of shape (n_samples, n_neighbors)
|
| 298 |
+
Indices of nearest neighbors.
|
| 299 |
+
"""
|
| 300 |
+
n_samples = graph.shape[0]
|
| 301 |
+
assert graph.format == "csr"
|
| 302 |
+
|
| 303 |
+
# number of neighbors by samples
|
| 304 |
+
row_nnz = np.diff(graph.indptr)
|
| 305 |
+
row_nnz_min = row_nnz.min()
|
| 306 |
+
if n_neighbors is not None and row_nnz_min < n_neighbors:
|
| 307 |
+
raise ValueError(
|
| 308 |
+
"%d neighbors per samples are required, but some samples have only"
|
| 309 |
+
" %d neighbors in precomputed graph matrix. Decrease number of "
|
| 310 |
+
"neighbors used or recompute the graph with more neighbors."
|
| 311 |
+
% (n_neighbors, row_nnz_min)
|
| 312 |
+
)
|
| 313 |
+
|
| 314 |
+
def extract(a):
|
| 315 |
+
# if each sample has the same number of provided neighbors
|
| 316 |
+
if row_nnz.max() == row_nnz_min:
|
| 317 |
+
return a.reshape(n_samples, -1)[:, :n_neighbors]
|
| 318 |
+
else:
|
| 319 |
+
idx = np.tile(np.arange(n_neighbors), (n_samples, 1))
|
| 320 |
+
idx += graph.indptr[:-1, None]
|
| 321 |
+
return a.take(idx, mode="clip").reshape(n_samples, n_neighbors)
|
| 322 |
+
|
| 323 |
+
if return_distance:
|
| 324 |
+
return extract(graph.data), extract(graph.indices)
|
| 325 |
+
else:
|
| 326 |
+
return extract(graph.indices)
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
def _radius_neighbors_from_graph(graph, radius, return_distance):
|
| 330 |
+
"""Decompose a nearest neighbors sparse graph into distances and indices.
|
| 331 |
+
|
| 332 |
+
Parameters
|
| 333 |
+
----------
|
| 334 |
+
graph : sparse matrix of shape (n_samples, n_samples)
|
| 335 |
+
Neighbors graph as given by `kneighbors_graph` or
|
| 336 |
+
`radius_neighbors_graph`. Matrix should be of format CSR format.
|
| 337 |
+
|
| 338 |
+
radius : float
|
| 339 |
+
Radius of neighborhoods which should be strictly positive.
|
| 340 |
+
|
| 341 |
+
return_distance : bool
|
| 342 |
+
Whether or not to return the distances.
|
| 343 |
+
|
| 344 |
+
Returns
|
| 345 |
+
-------
|
| 346 |
+
neigh_dist : ndarray of shape (n_samples,) of arrays
|
| 347 |
+
Distances to nearest neighbors. Only present if `return_distance=True`.
|
| 348 |
+
|
| 349 |
+
neigh_ind : ndarray of shape (n_samples,) of arrays
|
| 350 |
+
Indices of nearest neighbors.
|
| 351 |
+
"""
|
| 352 |
+
assert graph.format == "csr"
|
| 353 |
+
|
| 354 |
+
no_filter_needed = bool(graph.data.max() <= radius)
|
| 355 |
+
|
| 356 |
+
if no_filter_needed:
|
| 357 |
+
data, indices, indptr = graph.data, graph.indices, graph.indptr
|
| 358 |
+
else:
|
| 359 |
+
mask = graph.data <= radius
|
| 360 |
+
if return_distance:
|
| 361 |
+
data = np.compress(mask, graph.data)
|
| 362 |
+
indices = np.compress(mask, graph.indices)
|
| 363 |
+
indptr = np.concatenate(([0], np.cumsum(mask)))[graph.indptr]
|
| 364 |
+
|
| 365 |
+
indices = indices.astype(np.intp, copy=no_filter_needed)
|
| 366 |
+
|
| 367 |
+
if return_distance:
|
| 368 |
+
neigh_dist = _to_object_array(np.split(data, indptr[1:-1]))
|
| 369 |
+
neigh_ind = _to_object_array(np.split(indices, indptr[1:-1]))
|
| 370 |
+
|
| 371 |
+
if return_distance:
|
| 372 |
+
return neigh_dist, neigh_ind
|
| 373 |
+
else:
|
| 374 |
+
return neigh_ind
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
class NeighborsBase(MultiOutputMixin, BaseEstimator, metaclass=ABCMeta):
|
| 378 |
+
"""Base class for nearest neighbors estimators."""
|
| 379 |
+
|
| 380 |
+
_parameter_constraints: dict = {
|
| 381 |
+
"n_neighbors": [Interval(Integral, 1, None, closed="left"), None],
|
| 382 |
+
"radius": [Interval(Real, 0, None, closed="both"), None],
|
| 383 |
+
"algorithm": [StrOptions({"auto", "ball_tree", "kd_tree", "brute"})],
|
| 384 |
+
"leaf_size": [Interval(Integral, 1, None, closed="left")],
|
| 385 |
+
"p": [Interval(Real, 0, None, closed="right"), None],
|
| 386 |
+
"metric": [StrOptions(set(itertools.chain(*VALID_METRICS.values()))), callable],
|
| 387 |
+
"metric_params": [dict, None],
|
| 388 |
+
"n_jobs": [Integral, None],
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
@abstractmethod
|
| 392 |
+
def __init__(
|
| 393 |
+
self,
|
| 394 |
+
n_neighbors=None,
|
| 395 |
+
radius=None,
|
| 396 |
+
algorithm="auto",
|
| 397 |
+
leaf_size=30,
|
| 398 |
+
metric="minkowski",
|
| 399 |
+
p=2,
|
| 400 |
+
metric_params=None,
|
| 401 |
+
n_jobs=None,
|
| 402 |
+
):
|
| 403 |
+
|
| 404 |
+
self.n_neighbors = n_neighbors
|
| 405 |
+
self.radius = radius
|
| 406 |
+
self.algorithm = algorithm
|
| 407 |
+
self.leaf_size = leaf_size
|
| 408 |
+
self.metric = metric
|
| 409 |
+
self.metric_params = metric_params
|
| 410 |
+
self.p = p
|
| 411 |
+
self.n_jobs = n_jobs
|
| 412 |
+
|
| 413 |
+
def _check_algorithm_metric(self):
|
| 414 |
+
if self.algorithm == "auto":
|
| 415 |
+
if self.metric == "precomputed":
|
| 416 |
+
alg_check = "brute"
|
| 417 |
+
elif callable(self.metric) or self.metric in VALID_METRICS["ball_tree"]:
|
| 418 |
+
alg_check = "ball_tree"
|
| 419 |
+
else:
|
| 420 |
+
alg_check = "brute"
|
| 421 |
+
else:
|
| 422 |
+
alg_check = self.algorithm
|
| 423 |
+
|
| 424 |
+
if callable(self.metric):
|
| 425 |
+
if self.algorithm == "kd_tree":
|
| 426 |
+
# callable metric is only valid for brute force and ball_tree
|
| 427 |
+
raise ValueError(
|
| 428 |
+
"kd_tree does not support callable metric '%s'"
|
| 429 |
+
"Function call overhead will result"
|
| 430 |
+
"in very poor performance."
|
| 431 |
+
% self.metric
|
| 432 |
+
)
|
| 433 |
+
elif self.metric not in VALID_METRICS[alg_check]:
|
| 434 |
+
raise ValueError(
|
| 435 |
+
"Metric '%s' not valid. Use "
|
| 436 |
+
"sorted(sklearn.neighbors.VALID_METRICS['%s']) "
|
| 437 |
+
"to get valid options. "
|
| 438 |
+
"Metric can also be a callable function." % (self.metric, alg_check)
|
| 439 |
+
)
|
| 440 |
+
|
| 441 |
+
if self.metric_params is not None and "p" in self.metric_params:
|
| 442 |
+
if self.p is not None:
|
| 443 |
+
warnings.warn(
|
| 444 |
+
"Parameter p is found in metric_params. "
|
| 445 |
+
"The corresponding parameter from __init__ "
|
| 446 |
+
"is ignored.",
|
| 447 |
+
SyntaxWarning,
|
| 448 |
+
stacklevel=3,
|
| 449 |
+
)
|
| 450 |
+
|
| 451 |
+
def _fit(self, X, y=None):
|
| 452 |
+
if self._get_tags()["requires_y"]:
|
| 453 |
+
if not isinstance(X, (KDTree, BallTree, NeighborsBase)):
|
| 454 |
+
X, y = self._validate_data(
|
| 455 |
+
X, y, accept_sparse="csr", multi_output=True, order="C"
|
| 456 |
+
)
|
| 457 |
+
|
| 458 |
+
if is_classifier(self):
|
| 459 |
+
# Classification targets require a specific format
|
| 460 |
+
if y.ndim == 1 or y.ndim == 2 and y.shape[1] == 1:
|
| 461 |
+
if y.ndim != 1:
|
| 462 |
+
warnings.warn(
|
| 463 |
+
"A column-vector y was passed when a "
|
| 464 |
+
"1d array was expected. Please change "
|
| 465 |
+
"the shape of y to (n_samples,), for "
|
| 466 |
+
"example using ravel().",
|
| 467 |
+
DataConversionWarning,
|
| 468 |
+
stacklevel=2,
|
| 469 |
+
)
|
| 470 |
+
|
| 471 |
+
self.outputs_2d_ = False
|
| 472 |
+
y = y.reshape((-1, 1))
|
| 473 |
+
else:
|
| 474 |
+
self.outputs_2d_ = True
|
| 475 |
+
|
| 476 |
+
check_classification_targets(y)
|
| 477 |
+
self.classes_ = []
|
| 478 |
+
self._y = np.empty(y.shape, dtype=int)
|
| 479 |
+
for k in range(self._y.shape[1]):
|
| 480 |
+
classes, self._y[:, k] = np.unique(y[:, k], return_inverse=True)
|
| 481 |
+
self.classes_.append(classes)
|
| 482 |
+
|
| 483 |
+
if not self.outputs_2d_:
|
| 484 |
+
self.classes_ = self.classes_[0]
|
| 485 |
+
self._y = self._y.ravel()
|
| 486 |
+
else:
|
| 487 |
+
self._y = y
|
| 488 |
+
|
| 489 |
+
else:
|
| 490 |
+
if not isinstance(X, (KDTree, BallTree, NeighborsBase)):
|
| 491 |
+
X = self._validate_data(X, accept_sparse="csr", order="C")
|
| 492 |
+
|
| 493 |
+
self._check_algorithm_metric()
|
| 494 |
+
if self.metric_params is None:
|
| 495 |
+
self.effective_metric_params_ = {}
|
| 496 |
+
else:
|
| 497 |
+
self.effective_metric_params_ = self.metric_params.copy()
|
| 498 |
+
|
| 499 |
+
effective_p = self.effective_metric_params_.get("p", self.p)
|
| 500 |
+
if self.metric in ["wminkowski", "minkowski"]:
|
| 501 |
+
self.effective_metric_params_["p"] = effective_p
|
| 502 |
+
|
| 503 |
+
self.effective_metric_ = self.metric
|
| 504 |
+
# For minkowski distance, use more efficient methods where available
|
| 505 |
+
if self.metric == "minkowski":
|
| 506 |
+
p = self.effective_metric_params_.pop("p", 2)
|
| 507 |
+
w = self.effective_metric_params_.pop("w", None)
|
| 508 |
+
|
| 509 |
+
if p == 1 and w is None:
|
| 510 |
+
self.effective_metric_ = "manhattan"
|
| 511 |
+
elif p == 2 and w is None:
|
| 512 |
+
self.effective_metric_ = "euclidean"
|
| 513 |
+
elif p == np.inf and w is None:
|
| 514 |
+
self.effective_metric_ = "chebyshev"
|
| 515 |
+
else:
|
| 516 |
+
# Use the generic minkowski metric, possibly weighted.
|
| 517 |
+
self.effective_metric_params_["p"] = p
|
| 518 |
+
self.effective_metric_params_["w"] = w
|
| 519 |
+
|
| 520 |
+
if isinstance(X, NeighborsBase):
|
| 521 |
+
self._fit_X = X._fit_X
|
| 522 |
+
self._tree = X._tree
|
| 523 |
+
self._fit_method = X._fit_method
|
| 524 |
+
self.n_samples_fit_ = X.n_samples_fit_
|
| 525 |
+
return self
|
| 526 |
+
|
| 527 |
+
elif isinstance(X, BallTree):
|
| 528 |
+
self._fit_X = X.data
|
| 529 |
+
self._tree = X
|
| 530 |
+
self._fit_method = "ball_tree"
|
| 531 |
+
self.n_samples_fit_ = X.data.shape[0]
|
| 532 |
+
return self
|
| 533 |
+
|
| 534 |
+
elif isinstance(X, KDTree):
|
| 535 |
+
self._fit_X = X.data
|
| 536 |
+
self._tree = X
|
| 537 |
+
self._fit_method = "kd_tree"
|
| 538 |
+
self.n_samples_fit_ = X.data.shape[0]
|
| 539 |
+
return self
|
| 540 |
+
|
| 541 |
+
if self.metric == "precomputed":
|
| 542 |
+
X = _check_precomputed(X)
|
| 543 |
+
# Precomputed matrix X must be squared
|
| 544 |
+
if X.shape[0] != X.shape[1]:
|
| 545 |
+
raise ValueError(
|
| 546 |
+
"Precomputed matrix must be square."
|
| 547 |
+
" Input is a {}x{} matrix.".format(X.shape[0], X.shape[1])
|
| 548 |
+
)
|
| 549 |
+
self.n_features_in_ = X.shape[1]
|
| 550 |
+
|
| 551 |
+
n_samples = X.shape[0]
|
| 552 |
+
if n_samples == 0:
|
| 553 |
+
raise ValueError("n_samples must be greater than 0")
|
| 554 |
+
|
| 555 |
+
if issparse(X):
|
| 556 |
+
if self.algorithm not in ("auto", "brute"):
|
| 557 |
+
warnings.warn("cannot use tree with sparse input: using brute force")
|
| 558 |
+
|
| 559 |
+
if self.effective_metric_ not in VALID_METRICS_SPARSE[
|
| 560 |
+
"brute"
|
| 561 |
+
] and not callable(self.effective_metric_):
|
| 562 |
+
raise ValueError(
|
| 563 |
+
"Metric '%s' not valid for sparse input. "
|
| 564 |
+
"Use sorted(sklearn.neighbors."
|
| 565 |
+
"VALID_METRICS_SPARSE['brute']) "
|
| 566 |
+
"to get valid options. "
|
| 567 |
+
"Metric can also be a callable function." % (self.effective_metric_)
|
| 568 |
+
)
|
| 569 |
+
self._fit_X = X.copy()
|
| 570 |
+
self._tree = None
|
| 571 |
+
self._fit_method = "brute"
|
| 572 |
+
self.n_samples_fit_ = X.shape[0]
|
| 573 |
+
return self
|
| 574 |
+
|
| 575 |
+
self._fit_method = self.algorithm
|
| 576 |
+
self._fit_X = X
|
| 577 |
+
self.n_samples_fit_ = X.shape[0]
|
| 578 |
+
|
| 579 |
+
if self._fit_method == "auto":
|
| 580 |
+
# A tree approach is better for small number of neighbors or small
|
| 581 |
+
# number of features, with KDTree generally faster when available
|
| 582 |
+
if (
|
| 583 |
+
self.metric == "precomputed"
|
| 584 |
+
or self._fit_X.shape[1] > 15
|
| 585 |
+
or (
|
| 586 |
+
self.n_neighbors is not None
|
| 587 |
+
and self.n_neighbors >= self._fit_X.shape[0] // 2
|
| 588 |
+
)
|
| 589 |
+
):
|
| 590 |
+
self._fit_method = "brute"
|
| 591 |
+
else:
|
| 592 |
+
if (
|
| 593 |
+
# TODO(1.3): remove "wminkowski"
|
| 594 |
+
self.effective_metric_ in ("wminkowski", "minkowski")
|
| 595 |
+
and self.effective_metric_params_["p"] < 1
|
| 596 |
+
):
|
| 597 |
+
self._fit_method = "brute"
|
| 598 |
+
elif (
|
| 599 |
+
self.effective_metric_ == "minkowski"
|
| 600 |
+
and self.effective_metric_params_.get("w") is not None
|
| 601 |
+
):
|
| 602 |
+
# Be consistent with scipy 1.8 conventions: in scipy 1.8,
|
| 603 |
+
# 'wminkowski' was removed in favor of passing a
|
| 604 |
+
# weight vector directly to 'minkowski'.
|
| 605 |
+
#
|
| 606 |
+
# 'wminkowski' is not part of valid metrics for KDTree but
|
| 607 |
+
# the 'minkowski' without weights is.
|
| 608 |
+
#
|
| 609 |
+
# Hence, we detect this case and choose BallTree
|
| 610 |
+
# which supports 'wminkowski'.
|
| 611 |
+
self._fit_method = "ball_tree"
|
| 612 |
+
elif self.effective_metric_ in VALID_METRICS["kd_tree"]:
|
| 613 |
+
self._fit_method = "kd_tree"
|
| 614 |
+
elif (
|
| 615 |
+
callable(self.effective_metric_)
|
| 616 |
+
or self.effective_metric_ in VALID_METRICS["ball_tree"]
|
| 617 |
+
):
|
| 618 |
+
self._fit_method = "ball_tree"
|
| 619 |
+
else:
|
| 620 |
+
self._fit_method = "brute"
|
| 621 |
+
|
| 622 |
+
if (
|
| 623 |
+
# TODO(1.3): remove "wminkowski"
|
| 624 |
+
self.effective_metric_ in ("wminkowski", "minkowski")
|
| 625 |
+
and self.effective_metric_params_["p"] < 1
|
| 626 |
+
):
|
| 627 |
+
# For 0 < p < 1 Minkowski distances aren't valid distance
|
| 628 |
+
# metric as they do not satisfy triangular inequality:
|
| 629 |
+
# they are semi-metrics.
|
| 630 |
+
# algorithm="kd_tree" and algorithm="ball_tree" can't be used because
|
| 631 |
+
# KDTree and BallTree require a proper distance metric to work properly.
|
| 632 |
+
# However, the brute-force algorithm supports semi-metrics.
|
| 633 |
+
if self._fit_method == "brute":
|
| 634 |
+
warnings.warn(
|
| 635 |
+
"Mind that for 0 < p < 1, Minkowski metrics are not distance"
|
| 636 |
+
" metrics. Continuing the execution with `algorithm='brute'`."
|
| 637 |
+
)
|
| 638 |
+
else: # self._fit_method in ("kd_tree", "ball_tree")
|
| 639 |
+
raise ValueError(
|
| 640 |
+
f'algorithm="{self._fit_method}" does not support 0 < p < 1 for '
|
| 641 |
+
"the Minkowski metric. To resolve this problem either "
|
| 642 |
+
'set p >= 1 or algorithm="brute".'
|
| 643 |
+
)
|
| 644 |
+
|
| 645 |
+
if self._fit_method == "ball_tree":
|
| 646 |
+
self._tree = BallTree(
|
| 647 |
+
X,
|
| 648 |
+
self.leaf_size,
|
| 649 |
+
metric=self.effective_metric_,
|
| 650 |
+
**self.effective_metric_params_,
|
| 651 |
+
)
|
| 652 |
+
elif self._fit_method == "kd_tree":
|
| 653 |
+
if (
|
| 654 |
+
self.effective_metric_ == "minkowski"
|
| 655 |
+
and self.effective_metric_params_.get("w") is not None
|
| 656 |
+
):
|
| 657 |
+
raise ValueError(
|
| 658 |
+
"algorithm='kd_tree' is not valid for "
|
| 659 |
+
"metric='minkowski' with a weight parameter 'w': "
|
| 660 |
+
"try algorithm='ball_tree' "
|
| 661 |
+
"or algorithm='brute' instead."
|
| 662 |
+
)
|
| 663 |
+
self._tree = KDTree(
|
| 664 |
+
X,
|
| 665 |
+
self.leaf_size,
|
| 666 |
+
metric=self.effective_metric_,
|
| 667 |
+
**self.effective_metric_params_,
|
| 668 |
+
)
|
| 669 |
+
elif self._fit_method == "brute":
|
| 670 |
+
self._tree = None
|
| 671 |
+
|
| 672 |
+
return self
|
| 673 |
+
|
| 674 |
+
def _more_tags(self):
|
| 675 |
+
# For cross-validation routines to split data correctly
|
| 676 |
+
return {"pairwise": self.metric == "precomputed"}
|
| 677 |
+
|
| 678 |
+
|
| 679 |
+
def _tree_query_parallel_helper(tree, *args, **kwargs):
|
| 680 |
+
"""Helper for the Parallel calls in KNeighborsMixin.kneighbors.
|
| 681 |
+
|
| 682 |
+
The Cython method tree.query is not directly picklable by cloudpickle
|
| 683 |
+
under PyPy.
|
| 684 |
+
"""
|
| 685 |
+
return tree.query(*args, **kwargs)
|
| 686 |
+
|
| 687 |
+
|
| 688 |
+
class KNeighborsMixin:
|
| 689 |
+
"""Mixin for k-neighbors searches."""
|
| 690 |
+
|
| 691 |
+
def _kneighbors_reduce_func(self, dist, start, n_neighbors, return_distance):
|
| 692 |
+
"""Reduce a chunk of distances to the nearest neighbors.
|
| 693 |
+
|
| 694 |
+
Callback to :func:`sklearn.metrics.pairwise.pairwise_distances_chunked`
|
| 695 |
+
|
| 696 |
+
Parameters
|
| 697 |
+
----------
|
| 698 |
+
dist : ndarray of shape (n_samples_chunk, n_samples)
|
| 699 |
+
The distance matrix.
|
| 700 |
+
|
| 701 |
+
start : int
|
| 702 |
+
The index in X which the first row of dist corresponds to.
|
| 703 |
+
|
| 704 |
+
n_neighbors : int
|
| 705 |
+
Number of neighbors required for each sample.
|
| 706 |
+
|
| 707 |
+
return_distance : bool
|
| 708 |
+
Whether or not to return the distances.
|
| 709 |
+
|
| 710 |
+
Returns
|
| 711 |
+
-------
|
| 712 |
+
dist : array of shape (n_samples_chunk, n_neighbors)
|
| 713 |
+
Returned only if `return_distance=True`.
|
| 714 |
+
|
| 715 |
+
neigh : array of shape (n_samples_chunk, n_neighbors)
|
| 716 |
+
The neighbors indices.
|
| 717 |
+
"""
|
| 718 |
+
sample_range = np.arange(dist.shape[0])[:, None]
|
| 719 |
+
neigh_ind = np.argpartition(dist, n_neighbors - 1, axis=1)
|
| 720 |
+
neigh_ind = neigh_ind[:, :n_neighbors]
|
| 721 |
+
# argpartition doesn't guarantee sorted order, so we sort again
|
| 722 |
+
neigh_ind = neigh_ind[sample_range, np.argsort(dist[sample_range, neigh_ind])]
|
| 723 |
+
if return_distance:
|
| 724 |
+
if self.effective_metric_ == "euclidean":
|
| 725 |
+
result = np.sqrt(dist[sample_range, neigh_ind]), neigh_ind
|
| 726 |
+
else:
|
| 727 |
+
result = dist[sample_range, neigh_ind], neigh_ind
|
| 728 |
+
else:
|
| 729 |
+
result = neigh_ind
|
| 730 |
+
return result
|
| 731 |
+
|
| 732 |
+
def kneighbors(self, X=None, n_neighbors=None, return_distance=True):
|
| 733 |
+
"""Find the K-neighbors of a point.
|
| 734 |
+
|
| 735 |
+
Returns indices of and distances to the neighbors of each point.
|
| 736 |
+
|
| 737 |
+
Parameters
|
| 738 |
+
----------
|
| 739 |
+
X : {array-like, sparse matrix}, shape (n_queries, n_features), \
|
| 740 |
+
or (n_queries, n_indexed) if metric == 'precomputed', default=None
|
| 741 |
+
The query point or points.
|
| 742 |
+
If not provided, neighbors of each indexed point are returned.
|
| 743 |
+
In this case, the query point is not considered its own neighbor.
|
| 744 |
+
|
| 745 |
+
n_neighbors : int, default=None
|
| 746 |
+
Number of neighbors required for each sample. The default is the
|
| 747 |
+
value passed to the constructor.
|
| 748 |
+
|
| 749 |
+
return_distance : bool, default=True
|
| 750 |
+
Whether or not to return the distances.
|
| 751 |
+
|
| 752 |
+
Returns
|
| 753 |
+
-------
|
| 754 |
+
neigh_dist : ndarray of shape (n_queries, n_neighbors)
|
| 755 |
+
Array representing the lengths to points, only present if
|
| 756 |
+
return_distance=True.
|
| 757 |
+
|
| 758 |
+
neigh_ind : ndarray of shape (n_queries, n_neighbors)
|
| 759 |
+
Indices of the nearest points in the population matrix.
|
| 760 |
+
|
| 761 |
+
Examples
|
| 762 |
+
--------
|
| 763 |
+
In the following example, we construct a NearestNeighbors
|
| 764 |
+
class from an array representing our data set and ask who's
|
| 765 |
+
the closest point to [1,1,1]
|
| 766 |
+
|
| 767 |
+
>>> samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]
|
| 768 |
+
>>> from sklearn.neighbors import NearestNeighbors
|
| 769 |
+
>>> neigh = NearestNeighbors(n_neighbors=1)
|
| 770 |
+
>>> neigh.fit(samples)
|
| 771 |
+
NearestNeighbors(n_neighbors=1)
|
| 772 |
+
>>> print(neigh.kneighbors([[1., 1., 1.]]))
|
| 773 |
+
(array([[0.5]]), array([[2]]))
|
| 774 |
+
|
| 775 |
+
As you can see, it returns [[0.5]], and [[2]], which means that the
|
| 776 |
+
element is at distance 0.5 and is the third element of samples
|
| 777 |
+
(indexes start at 0). You can also query for multiple points:
|
| 778 |
+
|
| 779 |
+
>>> X = [[0., 1., 0.], [1., 0., 1.]]
|
| 780 |
+
>>> neigh.kneighbors(X, return_distance=False)
|
| 781 |
+
array([[1],
|
| 782 |
+
[2]]...)
|
| 783 |
+
"""
|
| 784 |
+
check_is_fitted(self)
|
| 785 |
+
|
| 786 |
+
if n_neighbors is None:
|
| 787 |
+
n_neighbors = self.n_neighbors
|
| 788 |
+
elif n_neighbors <= 0:
|
| 789 |
+
raise ValueError("Expected n_neighbors > 0. Got %d" % n_neighbors)
|
| 790 |
+
elif not isinstance(n_neighbors, numbers.Integral):
|
| 791 |
+
raise TypeError(
|
| 792 |
+
"n_neighbors does not take %s value, enter integer value"
|
| 793 |
+
% type(n_neighbors)
|
| 794 |
+
)
|
| 795 |
+
|
| 796 |
+
query_is_train = X is None
|
| 797 |
+
if query_is_train:
|
| 798 |
+
X = self._fit_X
|
| 799 |
+
# Include an extra neighbor to account for the sample itself being
|
| 800 |
+
# returned, which is removed later
|
| 801 |
+
n_neighbors += 1
|
| 802 |
+
else:
|
| 803 |
+
if self.metric == "precomputed":
|
| 804 |
+
X = _check_precomputed(X)
|
| 805 |
+
else:
|
| 806 |
+
X = self._validate_data(X, accept_sparse="csr", reset=False, order="C")
|
| 807 |
+
|
| 808 |
+
n_samples_fit = self.n_samples_fit_
|
| 809 |
+
if n_neighbors > n_samples_fit:
|
| 810 |
+
raise ValueError(
|
| 811 |
+
"Expected n_neighbors <= n_samples, "
|
| 812 |
+
" but n_samples = %d, n_neighbors = %d" % (n_samples_fit, n_neighbors)
|
| 813 |
+
)
|
| 814 |
+
|
| 815 |
+
n_jobs = effective_n_jobs(self.n_jobs)
|
| 816 |
+
chunked_results = None
|
| 817 |
+
use_pairwise_distances_reductions = (
|
| 818 |
+
self._fit_method == "brute"
|
| 819 |
+
and ArgKmin.is_usable_for(
|
| 820 |
+
X if X is not None else self._fit_X, self._fit_X, self.effective_metric_
|
| 821 |
+
)
|
| 822 |
+
)
|
| 823 |
+
if use_pairwise_distances_reductions:
|
| 824 |
+
results = ArgKmin.compute(
|
| 825 |
+
X=X,
|
| 826 |
+
Y=self._fit_X,
|
| 827 |
+
k=n_neighbors,
|
| 828 |
+
metric=self.effective_metric_,
|
| 829 |
+
metric_kwargs=self.effective_metric_params_,
|
| 830 |
+
strategy="auto",
|
| 831 |
+
return_distance=return_distance,
|
| 832 |
+
)
|
| 833 |
+
|
| 834 |
+
elif (
|
| 835 |
+
self._fit_method == "brute" and self.metric == "precomputed" and issparse(X)
|
| 836 |
+
):
|
| 837 |
+
results = _kneighbors_from_graph(
|
| 838 |
+
X, n_neighbors=n_neighbors, return_distance=return_distance
|
| 839 |
+
)
|
| 840 |
+
|
| 841 |
+
elif self._fit_method == "brute":
|
| 842 |
+
# Joblib-based backend, which is used when user-defined callable
|
| 843 |
+
# are passed for metric.
|
| 844 |
+
|
| 845 |
+
# This won't be used in the future once PairwiseDistancesReductions
|
| 846 |
+
# support:
|
| 847 |
+
# - DistanceMetrics which work on supposedly binary data
|
| 848 |
+
# - CSR-dense and dense-CSR case if 'euclidean' in metric.
|
| 849 |
+
reduce_func = partial(
|
| 850 |
+
self._kneighbors_reduce_func,
|
| 851 |
+
n_neighbors=n_neighbors,
|
| 852 |
+
return_distance=return_distance,
|
| 853 |
+
)
|
| 854 |
+
|
| 855 |
+
# for efficiency, use squared euclidean distances
|
| 856 |
+
if self.effective_metric_ == "euclidean":
|
| 857 |
+
kwds = {"squared": True}
|
| 858 |
+
else:
|
| 859 |
+
kwds = self.effective_metric_params_
|
| 860 |
+
|
| 861 |
+
chunked_results = list(
|
| 862 |
+
pairwise_distances_chunked(
|
| 863 |
+
X,
|
| 864 |
+
self._fit_X,
|
| 865 |
+
reduce_func=reduce_func,
|
| 866 |
+
metric=self.effective_metric_,
|
| 867 |
+
n_jobs=n_jobs,
|
| 868 |
+
**kwds,
|
| 869 |
+
)
|
| 870 |
+
)
|
| 871 |
+
|
| 872 |
+
elif self._fit_method in ["ball_tree", "kd_tree"]:
|
| 873 |
+
if issparse(X):
|
| 874 |
+
raise ValueError(
|
| 875 |
+
"%s does not work with sparse matrices. Densify the data, "
|
| 876 |
+
"or set algorithm='brute'"
|
| 877 |
+
% self._fit_method
|
| 878 |
+
)
|
| 879 |
+
chunked_results = Parallel(n_jobs, prefer="threads")(
|
| 880 |
+
delayed(_tree_query_parallel_helper)(
|
| 881 |
+
self._tree, X[s], n_neighbors, return_distance
|
| 882 |
+
)
|
| 883 |
+
for s in gen_even_slices(X.shape[0], n_jobs)
|
| 884 |
+
)
|
| 885 |
+
else:
|
| 886 |
+
raise ValueError("internal: _fit_method not recognized")
|
| 887 |
+
|
| 888 |
+
if chunked_results is not None:
|
| 889 |
+
if return_distance:
|
| 890 |
+
neigh_dist, neigh_ind = zip(*chunked_results)
|
| 891 |
+
results = np.vstack(neigh_dist), np.vstack(neigh_ind)
|
| 892 |
+
else:
|
| 893 |
+
results = np.vstack(chunked_results)
|
| 894 |
+
|
| 895 |
+
if not query_is_train:
|
| 896 |
+
return results
|
| 897 |
+
else:
|
| 898 |
+
# If the query data is the same as the indexed data, we would like
|
| 899 |
+
# to ignore the first nearest neighbor of every sample, i.e
|
| 900 |
+
# the sample itself.
|
| 901 |
+
if return_distance:
|
| 902 |
+
neigh_dist, neigh_ind = results
|
| 903 |
+
else:
|
| 904 |
+
neigh_ind = results
|
| 905 |
+
|
| 906 |
+
n_queries, _ = X.shape
|
| 907 |
+
sample_range = np.arange(n_queries)[:, None]
|
| 908 |
+
sample_mask = neigh_ind != sample_range
|
| 909 |
+
|
| 910 |
+
# Corner case: When the number of duplicates are more
|
| 911 |
+
# than the number of neighbors, the first NN will not
|
| 912 |
+
# be the sample, but a duplicate.
|
| 913 |
+
# In that case mask the first duplicate.
|
| 914 |
+
dup_gr_nbrs = np.all(sample_mask, axis=1)
|
| 915 |
+
sample_mask[:, 0][dup_gr_nbrs] = False
|
| 916 |
+
neigh_ind = np.reshape(neigh_ind[sample_mask], (n_queries, n_neighbors - 1))
|
| 917 |
+
|
| 918 |
+
if return_distance:
|
| 919 |
+
neigh_dist = np.reshape(
|
| 920 |
+
neigh_dist[sample_mask], (n_queries, n_neighbors - 1)
|
| 921 |
+
)
|
| 922 |
+
return neigh_dist, neigh_ind
|
| 923 |
+
return neigh_ind
|
| 924 |
+
|
| 925 |
+
def kneighbors_graph(self, X=None, n_neighbors=None, mode="connectivity"):
|
| 926 |
+
"""Compute the (weighted) graph of k-Neighbors for points in X.
|
| 927 |
+
|
| 928 |
+
Parameters
|
| 929 |
+
----------
|
| 930 |
+
X : {array-like, sparse matrix} of shape (n_queries, n_features), \
|
| 931 |
+
or (n_queries, n_indexed) if metric == 'precomputed', default=None
|
| 932 |
+
The query point or points.
|
| 933 |
+
If not provided, neighbors of each indexed point are returned.
|
| 934 |
+
In this case, the query point is not considered its own neighbor.
|
| 935 |
+
For ``metric='precomputed'`` the shape should be
|
| 936 |
+
(n_queries, n_indexed). Otherwise the shape should be
|
| 937 |
+
(n_queries, n_features).
|
| 938 |
+
|
| 939 |
+
n_neighbors : int, default=None
|
| 940 |
+
Number of neighbors for each sample. The default is the value
|
| 941 |
+
passed to the constructor.
|
| 942 |
+
|
| 943 |
+
mode : {'connectivity', 'distance'}, default='connectivity'
|
| 944 |
+
Type of returned matrix: 'connectivity' will return the
|
| 945 |
+
connectivity matrix with ones and zeros, in 'distance' the
|
| 946 |
+
edges are distances between points, type of distance
|
| 947 |
+
depends on the selected metric parameter in
|
| 948 |
+
NearestNeighbors class.
|
| 949 |
+
|
| 950 |
+
Returns
|
| 951 |
+
-------
|
| 952 |
+
A : sparse-matrix of shape (n_queries, n_samples_fit)
|
| 953 |
+
`n_samples_fit` is the number of samples in the fitted data.
|
| 954 |
+
`A[i, j]` gives the weight of the edge connecting `i` to `j`.
|
| 955 |
+
The matrix is of CSR format.
|
| 956 |
+
|
| 957 |
+
See Also
|
| 958 |
+
--------
|
| 959 |
+
NearestNeighbors.radius_neighbors_graph : Compute the (weighted) graph
|
| 960 |
+
of Neighbors for points in X.
|
| 961 |
+
|
| 962 |
+
Examples
|
| 963 |
+
--------
|
| 964 |
+
>>> X = [[0], [3], [1]]
|
| 965 |
+
>>> from sklearn.neighbors import NearestNeighbors
|
| 966 |
+
>>> neigh = NearestNeighbors(n_neighbors=2)
|
| 967 |
+
>>> neigh.fit(X)
|
| 968 |
+
NearestNeighbors(n_neighbors=2)
|
| 969 |
+
>>> A = neigh.kneighbors_graph(X)
|
| 970 |
+
>>> A.toarray()
|
| 971 |
+
array([[1., 0., 1.],
|
| 972 |
+
[0., 1., 1.],
|
| 973 |
+
[1., 0., 1.]])
|
| 974 |
+
"""
|
| 975 |
+
check_is_fitted(self)
|
| 976 |
+
if n_neighbors is None:
|
| 977 |
+
n_neighbors = self.n_neighbors
|
| 978 |
+
|
| 979 |
+
# check the input only in self.kneighbors
|
| 980 |
+
|
| 981 |
+
# construct CSR matrix representation of the k-NN graph
|
| 982 |
+
if mode == "connectivity":
|
| 983 |
+
A_ind = self.kneighbors(X, n_neighbors, return_distance=False)
|
| 984 |
+
n_queries = A_ind.shape[0]
|
| 985 |
+
A_data = np.ones(n_queries * n_neighbors)
|
| 986 |
+
|
| 987 |
+
elif mode == "distance":
|
| 988 |
+
A_data, A_ind = self.kneighbors(X, n_neighbors, return_distance=True)
|
| 989 |
+
A_data = np.ravel(A_data)
|
| 990 |
+
|
| 991 |
+
else:
|
| 992 |
+
raise ValueError(
|
| 993 |
+
'Unsupported mode, must be one of "connectivity", '
|
| 994 |
+
f'or "distance" but got "{mode}" instead'
|
| 995 |
+
)
|
| 996 |
+
|
| 997 |
+
n_queries = A_ind.shape[0]
|
| 998 |
+
n_samples_fit = self.n_samples_fit_
|
| 999 |
+
n_nonzero = n_queries * n_neighbors
|
| 1000 |
+
A_indptr = np.arange(0, n_nonzero + 1, n_neighbors)
|
| 1001 |
+
|
| 1002 |
+
kneighbors_graph = csr_matrix(
|
| 1003 |
+
(A_data, A_ind.ravel(), A_indptr), shape=(n_queries, n_samples_fit)
|
| 1004 |
+
)
|
| 1005 |
+
|
| 1006 |
+
return kneighbors_graph
|
| 1007 |
+
|
| 1008 |
+
|
| 1009 |
+
def _tree_query_radius_parallel_helper(tree, *args, **kwargs):
|
| 1010 |
+
"""Helper for the Parallel calls in RadiusNeighborsMixin.radius_neighbors.
|
| 1011 |
+
|
| 1012 |
+
The Cython method tree.query_radius is not directly picklable by
|
| 1013 |
+
cloudpickle under PyPy.
|
| 1014 |
+
"""
|
| 1015 |
+
return tree.query_radius(*args, **kwargs)
|
| 1016 |
+
|
| 1017 |
+
|
| 1018 |
+
class RadiusNeighborsMixin:
|
| 1019 |
+
"""Mixin for radius-based neighbors searches."""
|
| 1020 |
+
|
| 1021 |
+
def _radius_neighbors_reduce_func(self, dist, start, radius, return_distance):
|
| 1022 |
+
"""Reduce a chunk of distances to the nearest neighbors.
|
| 1023 |
+
|
| 1024 |
+
Callback to :func:`sklearn.metrics.pairwise.pairwise_distances_chunked`
|
| 1025 |
+
|
| 1026 |
+
Parameters
|
| 1027 |
+
----------
|
| 1028 |
+
dist : ndarray of shape (n_samples_chunk, n_samples)
|
| 1029 |
+
The distance matrix.
|
| 1030 |
+
|
| 1031 |
+
start : int
|
| 1032 |
+
The index in X which the first row of dist corresponds to.
|
| 1033 |
+
|
| 1034 |
+
radius : float
|
| 1035 |
+
The radius considered when making the nearest neighbors search.
|
| 1036 |
+
|
| 1037 |
+
return_distance : bool
|
| 1038 |
+
Whether or not to return the distances.
|
| 1039 |
+
|
| 1040 |
+
Returns
|
| 1041 |
+
-------
|
| 1042 |
+
dist : list of ndarray of shape (n_samples_chunk,)
|
| 1043 |
+
Returned only if `return_distance=True`.
|
| 1044 |
+
|
| 1045 |
+
neigh : list of ndarray of shape (n_samples_chunk,)
|
| 1046 |
+
The neighbors indices.
|
| 1047 |
+
"""
|
| 1048 |
+
neigh_ind = [np.where(d <= radius)[0] for d in dist]
|
| 1049 |
+
|
| 1050 |
+
if return_distance:
|
| 1051 |
+
if self.effective_metric_ == "euclidean":
|
| 1052 |
+
dist = [np.sqrt(d[neigh_ind[i]]) for i, d in enumerate(dist)]
|
| 1053 |
+
else:
|
| 1054 |
+
dist = [d[neigh_ind[i]] for i, d in enumerate(dist)]
|
| 1055 |
+
results = dist, neigh_ind
|
| 1056 |
+
else:
|
| 1057 |
+
results = neigh_ind
|
| 1058 |
+
return results
|
| 1059 |
+
|
| 1060 |
+
def radius_neighbors(
|
| 1061 |
+
self, X=None, radius=None, return_distance=True, sort_results=False
|
| 1062 |
+
):
|
| 1063 |
+
"""Find the neighbors within a given radius of a point or points.
|
| 1064 |
+
|
| 1065 |
+
Return the indices and distances of each point from the dataset
|
| 1066 |
+
lying in a ball with size ``radius`` around the points of the query
|
| 1067 |
+
array. Points lying on the boundary are included in the results.
|
| 1068 |
+
|
| 1069 |
+
The result points are *not* necessarily sorted by distance to their
|
| 1070 |
+
query point.
|
| 1071 |
+
|
| 1072 |
+
Parameters
|
| 1073 |
+
----------
|
| 1074 |
+
X : {array-like, sparse matrix} of (n_samples, n_features), default=None
|
| 1075 |
+
The query point or points.
|
| 1076 |
+
If not provided, neighbors of each indexed point are returned.
|
| 1077 |
+
In this case, the query point is not considered its own neighbor.
|
| 1078 |
+
|
| 1079 |
+
radius : float, default=None
|
| 1080 |
+
Limiting distance of neighbors to return. The default is the value
|
| 1081 |
+
passed to the constructor.
|
| 1082 |
+
|
| 1083 |
+
return_distance : bool, default=True
|
| 1084 |
+
Whether or not to return the distances.
|
| 1085 |
+
|
| 1086 |
+
sort_results : bool, default=False
|
| 1087 |
+
If True, the distances and indices will be sorted by increasing
|
| 1088 |
+
distances before being returned. If False, the results may not
|
| 1089 |
+
be sorted. If `return_distance=False`, setting `sort_results=True`
|
| 1090 |
+
will result in an error.
|
| 1091 |
+
|
| 1092 |
+
.. versionadded:: 0.22
|
| 1093 |
+
|
| 1094 |
+
Returns
|
| 1095 |
+
-------
|
| 1096 |
+
neigh_dist : ndarray of shape (n_samples,) of arrays
|
| 1097 |
+
Array representing the distances to each point, only present if
|
| 1098 |
+
`return_distance=True`. The distance values are computed according
|
| 1099 |
+
to the ``metric`` constructor parameter.
|
| 1100 |
+
|
| 1101 |
+
neigh_ind : ndarray of shape (n_samples,) of arrays
|
| 1102 |
+
An array of arrays of indices of the approximate nearest points
|
| 1103 |
+
from the population matrix that lie within a ball of size
|
| 1104 |
+
``radius`` around the query points.
|
| 1105 |
+
|
| 1106 |
+
Notes
|
| 1107 |
+
-----
|
| 1108 |
+
Because the number of neighbors of each point is not necessarily
|
| 1109 |
+
equal, the results for multiple query points cannot be fit in a
|
| 1110 |
+
standard data array.
|
| 1111 |
+
For efficiency, `radius_neighbors` returns arrays of objects, where
|
| 1112 |
+
each object is a 1D array of indices or distances.
|
| 1113 |
+
|
| 1114 |
+
Examples
|
| 1115 |
+
--------
|
| 1116 |
+
In the following example, we construct a NeighborsClassifier
|
| 1117 |
+
class from an array representing our data set and ask who's
|
| 1118 |
+
the closest point to [1, 1, 1]:
|
| 1119 |
+
|
| 1120 |
+
>>> import numpy as np
|
| 1121 |
+
>>> samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]
|
| 1122 |
+
>>> from sklearn.neighbors import NearestNeighbors
|
| 1123 |
+
>>> neigh = NearestNeighbors(radius=1.6)
|
| 1124 |
+
>>> neigh.fit(samples)
|
| 1125 |
+
NearestNeighbors(radius=1.6)
|
| 1126 |
+
>>> rng = neigh.radius_neighbors([[1., 1., 1.]])
|
| 1127 |
+
>>> print(np.asarray(rng[0][0]))
|
| 1128 |
+
[1.5 0.5]
|
| 1129 |
+
>>> print(np.asarray(rng[1][0]))
|
| 1130 |
+
[1 2]
|
| 1131 |
+
|
| 1132 |
+
The first array returned contains the distances to all points which
|
| 1133 |
+
are closer than 1.6, while the second array returned contains their
|
| 1134 |
+
indices. In general, multiple points can be queried at the same time.
|
| 1135 |
+
"""
|
| 1136 |
+
check_is_fitted(self)
|
| 1137 |
+
|
| 1138 |
+
if sort_results and not return_distance:
|
| 1139 |
+
raise ValueError("return_distance must be True if sort_results is True.")
|
| 1140 |
+
|
| 1141 |
+
query_is_train = X is None
|
| 1142 |
+
if query_is_train:
|
| 1143 |
+
X = self._fit_X
|
| 1144 |
+
else:
|
| 1145 |
+
if self.metric == "precomputed":
|
| 1146 |
+
X = _check_precomputed(X)
|
| 1147 |
+
else:
|
| 1148 |
+
X = self._validate_data(X, accept_sparse="csr", reset=False, order="C")
|
| 1149 |
+
|
| 1150 |
+
if radius is None:
|
| 1151 |
+
radius = self.radius
|
| 1152 |
+
|
| 1153 |
+
use_pairwise_distances_reductions = (
|
| 1154 |
+
self._fit_method == "brute"
|
| 1155 |
+
and RadiusNeighbors.is_usable_for(
|
| 1156 |
+
X if X is not None else self._fit_X, self._fit_X, self.effective_metric_
|
| 1157 |
+
)
|
| 1158 |
+
)
|
| 1159 |
+
|
| 1160 |
+
if use_pairwise_distances_reductions:
|
| 1161 |
+
results = RadiusNeighbors.compute(
|
| 1162 |
+
X=X,
|
| 1163 |
+
Y=self._fit_X,
|
| 1164 |
+
radius=radius,
|
| 1165 |
+
metric=self.effective_metric_,
|
| 1166 |
+
metric_kwargs=self.effective_metric_params_,
|
| 1167 |
+
strategy="auto",
|
| 1168 |
+
return_distance=return_distance,
|
| 1169 |
+
sort_results=sort_results,
|
| 1170 |
+
)
|
| 1171 |
+
|
| 1172 |
+
elif (
|
| 1173 |
+
self._fit_method == "brute" and self.metric == "precomputed" and issparse(X)
|
| 1174 |
+
):
|
| 1175 |
+
results = _radius_neighbors_from_graph(
|
| 1176 |
+
X, radius=radius, return_distance=return_distance
|
| 1177 |
+
)
|
| 1178 |
+
|
| 1179 |
+
elif self._fit_method == "brute":
|
| 1180 |
+
# Joblib-based backend, which is used when user-defined callable
|
| 1181 |
+
# are passed for metric.
|
| 1182 |
+
|
| 1183 |
+
# This won't be used in the future once PairwiseDistancesReductions
|
| 1184 |
+
# support:
|
| 1185 |
+
# - DistanceMetrics which work on supposedly binary data
|
| 1186 |
+
# - CSR-dense and dense-CSR case if 'euclidean' in metric.
|
| 1187 |
+
|
| 1188 |
+
# for efficiency, use squared euclidean distances
|
| 1189 |
+
if self.effective_metric_ == "euclidean":
|
| 1190 |
+
radius *= radius
|
| 1191 |
+
kwds = {"squared": True}
|
| 1192 |
+
else:
|
| 1193 |
+
kwds = self.effective_metric_params_
|
| 1194 |
+
|
| 1195 |
+
reduce_func = partial(
|
| 1196 |
+
self._radius_neighbors_reduce_func,
|
| 1197 |
+
radius=radius,
|
| 1198 |
+
return_distance=return_distance,
|
| 1199 |
+
)
|
| 1200 |
+
|
| 1201 |
+
chunked_results = pairwise_distances_chunked(
|
| 1202 |
+
X,
|
| 1203 |
+
self._fit_X,
|
| 1204 |
+
reduce_func=reduce_func,
|
| 1205 |
+
metric=self.effective_metric_,
|
| 1206 |
+
n_jobs=self.n_jobs,
|
| 1207 |
+
**kwds,
|
| 1208 |
+
)
|
| 1209 |
+
if return_distance:
|
| 1210 |
+
neigh_dist_chunks, neigh_ind_chunks = zip(*chunked_results)
|
| 1211 |
+
neigh_dist_list = sum(neigh_dist_chunks, [])
|
| 1212 |
+
neigh_ind_list = sum(neigh_ind_chunks, [])
|
| 1213 |
+
neigh_dist = _to_object_array(neigh_dist_list)
|
| 1214 |
+
neigh_ind = _to_object_array(neigh_ind_list)
|
| 1215 |
+
results = neigh_dist, neigh_ind
|
| 1216 |
+
else:
|
| 1217 |
+
neigh_ind_list = sum(chunked_results, [])
|
| 1218 |
+
results = _to_object_array(neigh_ind_list)
|
| 1219 |
+
|
| 1220 |
+
if sort_results:
|
| 1221 |
+
for ii in range(len(neigh_dist)):
|
| 1222 |
+
order = np.argsort(neigh_dist[ii], kind="mergesort")
|
| 1223 |
+
neigh_ind[ii] = neigh_ind[ii][order]
|
| 1224 |
+
neigh_dist[ii] = neigh_dist[ii][order]
|
| 1225 |
+
results = neigh_dist, neigh_ind
|
| 1226 |
+
|
| 1227 |
+
elif self._fit_method in ["ball_tree", "kd_tree"]:
|
| 1228 |
+
if issparse(X):
|
| 1229 |
+
raise ValueError(
|
| 1230 |
+
"%s does not work with sparse matrices. Densify the data, "
|
| 1231 |
+
"or set algorithm='brute'"
|
| 1232 |
+
% self._fit_method
|
| 1233 |
+
)
|
| 1234 |
+
|
| 1235 |
+
n_jobs = effective_n_jobs(self.n_jobs)
|
| 1236 |
+
delayed_query = delayed(_tree_query_radius_parallel_helper)
|
| 1237 |
+
chunked_results = Parallel(n_jobs, prefer="threads")(
|
| 1238 |
+
delayed_query(
|
| 1239 |
+
self._tree, X[s], radius, return_distance, sort_results=sort_results
|
| 1240 |
+
)
|
| 1241 |
+
for s in gen_even_slices(X.shape[0], n_jobs)
|
| 1242 |
+
)
|
| 1243 |
+
if return_distance:
|
| 1244 |
+
neigh_ind, neigh_dist = tuple(zip(*chunked_results))
|
| 1245 |
+
results = np.hstack(neigh_dist), np.hstack(neigh_ind)
|
| 1246 |
+
else:
|
| 1247 |
+
results = np.hstack(chunked_results)
|
| 1248 |
+
else:
|
| 1249 |
+
raise ValueError("internal: _fit_method not recognized")
|
| 1250 |
+
|
| 1251 |
+
if not query_is_train:
|
| 1252 |
+
return results
|
| 1253 |
+
else:
|
| 1254 |
+
# If the query data is the same as the indexed data, we would like
|
| 1255 |
+
# to ignore the first nearest neighbor of every sample, i.e
|
| 1256 |
+
# the sample itself.
|
| 1257 |
+
if return_distance:
|
| 1258 |
+
neigh_dist, neigh_ind = results
|
| 1259 |
+
else:
|
| 1260 |
+
neigh_ind = results
|
| 1261 |
+
|
| 1262 |
+
for ind, ind_neighbor in enumerate(neigh_ind):
|
| 1263 |
+
mask = ind_neighbor != ind
|
| 1264 |
+
|
| 1265 |
+
neigh_ind[ind] = ind_neighbor[mask]
|
| 1266 |
+
if return_distance:
|
| 1267 |
+
neigh_dist[ind] = neigh_dist[ind][mask]
|
| 1268 |
+
|
| 1269 |
+
if return_distance:
|
| 1270 |
+
return neigh_dist, neigh_ind
|
| 1271 |
+
return neigh_ind
|
| 1272 |
+
|
| 1273 |
+
def radius_neighbors_graph(
|
| 1274 |
+
self, X=None, radius=None, mode="connectivity", sort_results=False
|
| 1275 |
+
):
|
| 1276 |
+
"""Compute the (weighted) graph of Neighbors for points in X.
|
| 1277 |
+
|
| 1278 |
+
Neighborhoods are restricted the points at a distance lower than
|
| 1279 |
+
radius.
|
| 1280 |
+
|
| 1281 |
+
Parameters
|
| 1282 |
+
----------
|
| 1283 |
+
X : {array-like, sparse matrix} of shape (n_samples, n_features), default=None
|
| 1284 |
+
The query point or points.
|
| 1285 |
+
If not provided, neighbors of each indexed point are returned.
|
| 1286 |
+
In this case, the query point is not considered its own neighbor.
|
| 1287 |
+
|
| 1288 |
+
radius : float, default=None
|
| 1289 |
+
Radius of neighborhoods. The default is the value passed to the
|
| 1290 |
+
constructor.
|
| 1291 |
+
|
| 1292 |
+
mode : {'connectivity', 'distance'}, default='connectivity'
|
| 1293 |
+
Type of returned matrix: 'connectivity' will return the
|
| 1294 |
+
connectivity matrix with ones and zeros, in 'distance' the
|
| 1295 |
+
edges are distances between points, type of distance
|
| 1296 |
+
depends on the selected metric parameter in
|
| 1297 |
+
NearestNeighbors class.
|
| 1298 |
+
|
| 1299 |
+
sort_results : bool, default=False
|
| 1300 |
+
If True, in each row of the result, the non-zero entries will be
|
| 1301 |
+
sorted by increasing distances. If False, the non-zero entries may
|
| 1302 |
+
not be sorted. Only used with mode='distance'.
|
| 1303 |
+
|
| 1304 |
+
.. versionadded:: 0.22
|
| 1305 |
+
|
| 1306 |
+
Returns
|
| 1307 |
+
-------
|
| 1308 |
+
A : sparse-matrix of shape (n_queries, n_samples_fit)
|
| 1309 |
+
`n_samples_fit` is the number of samples in the fitted data.
|
| 1310 |
+
`A[i, j]` gives the weight of the edge connecting `i` to `j`.
|
| 1311 |
+
The matrix is of CSR format.
|
| 1312 |
+
|
| 1313 |
+
See Also
|
| 1314 |
+
--------
|
| 1315 |
+
kneighbors_graph : Compute the (weighted) graph of k-Neighbors for
|
| 1316 |
+
points in X.
|
| 1317 |
+
|
| 1318 |
+
Examples
|
| 1319 |
+
--------
|
| 1320 |
+
>>> X = [[0], [3], [1]]
|
| 1321 |
+
>>> from sklearn.neighbors import NearestNeighbors
|
| 1322 |
+
>>> neigh = NearestNeighbors(radius=1.5)
|
| 1323 |
+
>>> neigh.fit(X)
|
| 1324 |
+
NearestNeighbors(radius=1.5)
|
| 1325 |
+
>>> A = neigh.radius_neighbors_graph(X)
|
| 1326 |
+
>>> A.toarray()
|
| 1327 |
+
array([[1., 0., 1.],
|
| 1328 |
+
[0., 1., 0.],
|
| 1329 |
+
[1., 0., 1.]])
|
| 1330 |
+
"""
|
| 1331 |
+
check_is_fitted(self)
|
| 1332 |
+
|
| 1333 |
+
# check the input only in self.radius_neighbors
|
| 1334 |
+
|
| 1335 |
+
if radius is None:
|
| 1336 |
+
radius = self.radius
|
| 1337 |
+
|
| 1338 |
+
# construct CSR matrix representation of the NN graph
|
| 1339 |
+
if mode == "connectivity":
|
| 1340 |
+
A_ind = self.radius_neighbors(X, radius, return_distance=False)
|
| 1341 |
+
A_data = None
|
| 1342 |
+
elif mode == "distance":
|
| 1343 |
+
dist, A_ind = self.radius_neighbors(
|
| 1344 |
+
X, radius, return_distance=True, sort_results=sort_results
|
| 1345 |
+
)
|
| 1346 |
+
A_data = np.concatenate(list(dist))
|
| 1347 |
+
else:
|
| 1348 |
+
raise ValueError(
|
| 1349 |
+
'Unsupported mode, must be one of "connectivity", '
|
| 1350 |
+
f'or "distance" but got "{mode}" instead'
|
| 1351 |
+
)
|
| 1352 |
+
|
| 1353 |
+
n_queries = A_ind.shape[0]
|
| 1354 |
+
n_samples_fit = self.n_samples_fit_
|
| 1355 |
+
n_neighbors = np.array([len(a) for a in A_ind])
|
| 1356 |
+
A_ind = np.concatenate(list(A_ind))
|
| 1357 |
+
if A_data is None:
|
| 1358 |
+
A_data = np.ones(len(A_ind))
|
| 1359 |
+
A_indptr = np.concatenate((np.zeros(1, dtype=int), np.cumsum(n_neighbors)))
|
| 1360 |
+
|
| 1361 |
+
return csr_matrix((A_data, A_ind, A_indptr), shape=(n_queries, n_samples_fit))
|
videollama2/lib/python3.10/site-packages/sklearn/neighbors/_distance_metric.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# TODO: Remove this file in 1.3
|
| 2 |
+
import warnings
|
| 3 |
+
|
| 4 |
+
from ..metrics import DistanceMetric as _DistanceMetric
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class DistanceMetric(_DistanceMetric):
|
| 8 |
+
@classmethod
|
| 9 |
+
def _warn(cls):
|
| 10 |
+
warnings.warn(
|
| 11 |
+
"sklearn.neighbors.DistanceMetric has been moved "
|
| 12 |
+
"to sklearn.metrics.DistanceMetric in 1.0. "
|
| 13 |
+
"This import path will be removed in 1.3",
|
| 14 |
+
category=FutureWarning,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
@classmethod
|
| 18 |
+
def get_metric(cls, metric, **kwargs):
|
| 19 |
+
DistanceMetric._warn()
|
| 20 |
+
return _DistanceMetric.get_metric(metric, **kwargs)
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/ATenGeneral.h
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <c10/macros/Macros.h>
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/ATen_pch.h
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// This global header must not depend on native_functions.yaml or
|
| 2 |
+
// incremental builds will be next to useless
|
| 3 |
+
#pragma push_macro("TORCH_ASSERT_NO_OPERATORS")
|
| 4 |
+
#define TORCH_ASSERT_NO_OPERATORS
|
| 5 |
+
|
| 6 |
+
// This macro doesn't work if defined after the first time inttypes.h
|
| 7 |
+
// is included, so won't work anywhere if not defined here.
|
| 8 |
+
#ifndef __STDC_FORMAT_MACROS
|
| 9 |
+
#define __STDC_FORMAT_MACROS
|
| 10 |
+
#endif
|
| 11 |
+
#include <cinttypes>
|
| 12 |
+
|
| 13 |
+
// This list of headers was generated using a script that finds
|
| 14 |
+
// high-impact headers and then manually tweaked to remove OS specific
|
| 15 |
+
// or duplicate headers (e.g. <cassert> and <assert.h>) and to remove
|
| 16 |
+
// "impl" headers (e.g BFloat16-inl.h or complex_math.h in c10).
|
| 17 |
+
|
| 18 |
+
// To generate the initial list:
|
| 19 |
+
// 1. Build pytorch from scratch with all build caching disabled
|
| 20 |
+
// 2. Generate a build trace with ninjatracing (https://github.com/nico/ninjatracing)
|
| 21 |
+
// $ ninjatracing /path/to/pytorch/build/.ninja_log > trace_all.json
|
| 22 |
+
// 3. Run pch_gen.py from https://github.com/peterbell10/build_analysis/
|
| 23 |
+
// $ python pch_gen.py --threshold .80 --target torch_cpu --build_dir /path/to/pytorch/build --trace trace_all.json
|
| 24 |
+
// Where the threshold can be tweaked until c10 and some of ATen
|
| 25 |
+
// core are included but TORCH_ASSERT_NO_OPERATORS still passes.
|
| 26 |
+
|
| 27 |
+
#include <cerrno>
|
| 28 |
+
#include <cmath>
|
| 29 |
+
#include <cstddef>
|
| 30 |
+
#include <cstdint>
|
| 31 |
+
#include <cstdlib>
|
| 32 |
+
#include <cstring>
|
| 33 |
+
|
| 34 |
+
#include <algorithm>
|
| 35 |
+
#include <array>
|
| 36 |
+
#include <atomic>
|
| 37 |
+
#include <chrono>
|
| 38 |
+
#include <complex>
|
| 39 |
+
#include <deque>
|
| 40 |
+
#include <exception>
|
| 41 |
+
#include <functional>
|
| 42 |
+
#include <initializer_list>
|
| 43 |
+
#include <iomanip>
|
| 44 |
+
#include <iosfwd>
|
| 45 |
+
#include <iterator>
|
| 46 |
+
#include <limits>
|
| 47 |
+
#include <list>
|
| 48 |
+
#include <map>
|
| 49 |
+
#include <memory>
|
| 50 |
+
#include <mutex>
|
| 51 |
+
#include <new>
|
| 52 |
+
#include <numeric>
|
| 53 |
+
#include <ostream>
|
| 54 |
+
#include <sstream>
|
| 55 |
+
#include <stdexcept>
|
| 56 |
+
#include <string>
|
| 57 |
+
#include <tuple>
|
| 58 |
+
#include <type_traits>
|
| 59 |
+
#include <typeindex>
|
| 60 |
+
#include <typeinfo>
|
| 61 |
+
#include <unordered_map>
|
| 62 |
+
#include <unordered_set>
|
| 63 |
+
#include <utility>
|
| 64 |
+
#include <vector>
|
| 65 |
+
|
| 66 |
+
#include <c10/core/Allocator.h>
|
| 67 |
+
#include <c10/core/AutogradState.h>
|
| 68 |
+
#include <c10/core/Backend.h>
|
| 69 |
+
#include <c10/core/DefaultDtype.h>
|
| 70 |
+
#include <c10/core/Device.h>
|
| 71 |
+
#include <c10/core/DeviceType.h>
|
| 72 |
+
#include <c10/core/DispatchKey.h>
|
| 73 |
+
#include <c10/core/DispatchKeySet.h>
|
| 74 |
+
#include <c10/core/GeneratorImpl.h>
|
| 75 |
+
#include <c10/core/InferenceMode.h>
|
| 76 |
+
#include <c10/core/Layout.h>
|
| 77 |
+
#include <c10/core/MemoryFormat.h>
|
| 78 |
+
#include <c10/core/OptionalRef.h>
|
| 79 |
+
#include <c10/core/QScheme.h>
|
| 80 |
+
#include <c10/core/Scalar.h>
|
| 81 |
+
#include <c10/core/ScalarType.h>
|
| 82 |
+
#include <c10/core/ScalarTypeToTypeMeta.h>
|
| 83 |
+
#include <c10/core/Storage.h>
|
| 84 |
+
#include <c10/core/StorageImpl.h>
|
| 85 |
+
#include <c10/core/SymBool.h>
|
| 86 |
+
#include <c10/core/SymFloat.h>
|
| 87 |
+
#include <c10/core/SymInt.h>
|
| 88 |
+
#include <c10/core/SymIntArrayRef.h>
|
| 89 |
+
#include <c10/core/SymNodeImpl.h>
|
| 90 |
+
#include <c10/core/TensorImpl.h>
|
| 91 |
+
#include <c10/core/TensorOptions.h>
|
| 92 |
+
#include <c10/core/UndefinedTensorImpl.h>
|
| 93 |
+
#include <c10/core/WrapDimMinimal.h>
|
| 94 |
+
#include <c10/core/impl/LocalDispatchKeySet.h>
|
| 95 |
+
#include <c10/core/impl/PyInterpreter.h>
|
| 96 |
+
#include <c10/core/impl/SizesAndStrides.h>
|
| 97 |
+
|
| 98 |
+
#include <c10/macros/Export.h>
|
| 99 |
+
#include <c10/macros/Macros.h>
|
| 100 |
+
|
| 101 |
+
#include <c10/util/AlignOf.h>
|
| 102 |
+
#include <c10/util/ArrayRef.h>
|
| 103 |
+
#include <c10/util/BFloat16.h>
|
| 104 |
+
#include <c10/util/C++17.h>
|
| 105 |
+
#include <c10/util/ConstexprCrc.h>
|
| 106 |
+
#include <c10/util/Deprecated.h>
|
| 107 |
+
#include <c10/util/DimVector.h>
|
| 108 |
+
#include <c10/util/Exception.h>
|
| 109 |
+
#include <c10/util/ExclusivelyOwned.h>
|
| 110 |
+
#include <c10/util/Flags.h>
|
| 111 |
+
#include <c10/util/Float8_e4m3fn.h>
|
| 112 |
+
#include <c10/util/Float8_e5m2.h>
|
| 113 |
+
#include <c10/util/Float8_e4m3fnuz.h>
|
| 114 |
+
#include <c10/util/Float8_e5m2fnuz.h>
|
| 115 |
+
#include <c10/util/FunctionRef.h>
|
| 116 |
+
#include <c10/util/Half.h>
|
| 117 |
+
#include <c10/util/IdWrapper.h>
|
| 118 |
+
#include <c10/util/Logging.h>
|
| 119 |
+
#include <c10/util/MaybeOwned.h>
|
| 120 |
+
#include <c10/util/Metaprogramming.h>
|
| 121 |
+
#include <c10/util/Optional.h>
|
| 122 |
+
#include <c10/util/Registry.h>
|
| 123 |
+
#include <c10/util/SmallVector.h>
|
| 124 |
+
#include <c10/util/StringUtil.h>
|
| 125 |
+
#include <c10/util/ThreadLocalDebugInfo.h>
|
| 126 |
+
#include <c10/util/Type.h>
|
| 127 |
+
#include <c10/util/TypeCast.h>
|
| 128 |
+
#include <c10/util/TypeIndex.h>
|
| 129 |
+
#include <c10/util/TypeList.h>
|
| 130 |
+
#include <c10/util/TypeSafeSignMath.h>
|
| 131 |
+
#include <c10/util/TypeTraits.h>
|
| 132 |
+
#include <c10/util/UniqueVoidPtr.h>
|
| 133 |
+
#include <c10/util/accumulate.h>
|
| 134 |
+
#include <c10/util/bit_cast.h>
|
| 135 |
+
#include <c10/util/bits.h>
|
| 136 |
+
#include <c10/util/complex.h>
|
| 137 |
+
#include <c10/util/floating_point_utils.h>
|
| 138 |
+
#include <c10/util/intrusive_ptr.h>
|
| 139 |
+
#include <c10/util/irange.h>
|
| 140 |
+
#include <c10/util/llvmMathExtras.h>
|
| 141 |
+
#include <c10/util/python_stub.h>
|
| 142 |
+
#include <c10/util/qint32.h>
|
| 143 |
+
#include <c10/util/qint8.h>
|
| 144 |
+
#include <c10/util/quint2x4.h>
|
| 145 |
+
#include <c10/util/quint4x2.h>
|
| 146 |
+
#include <c10/util/quint8.h>
|
| 147 |
+
#include <c10/util/safe_numerics.h>
|
| 148 |
+
#include <c10/util/string_utils.h>
|
| 149 |
+
#include <c10/util/string_view.h>
|
| 150 |
+
#include <c10/util/typeid.h>
|
| 151 |
+
|
| 152 |
+
#include <ATen/StorageUtils.h>
|
| 153 |
+
#include <ATen/core/ATen_fwd.h>
|
| 154 |
+
#include <ATen/core/DeprecatedTypeProperties.h>
|
| 155 |
+
#include <ATen/core/DeprecatedTypePropertiesRegistry.h>
|
| 156 |
+
#include <ATen/core/DimVector.h>
|
| 157 |
+
#include <ATen/core/Dimname.h>
|
| 158 |
+
#include <ATen/core/Generator.h>
|
| 159 |
+
#include <ATen/core/NamedTensor.h>
|
| 160 |
+
#include <ATen/core/QuantizerBase.h>
|
| 161 |
+
#include <ATen/core/TensorAccessor.h>
|
| 162 |
+
#include <ATen/core/TensorBase.h>
|
| 163 |
+
#include <ATen/core/symbol.h>
|
| 164 |
+
|
| 165 |
+
#pragma pop_macro("TORCH_ASSERT_NO_OPERATORS")
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/CheckMemoryFormat.h
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <c10/core/TensorOptions.h>
|
| 2 |
+
|
| 3 |
+
namespace c10::impl {
|
| 4 |
+
|
| 5 |
+
inline std::optional<MemoryFormat>
|
| 6 |
+
check_tensor_options_and_extract_memory_format(
|
| 7 |
+
const TensorOptions& options,
|
| 8 |
+
std::optional<MemoryFormat> memory_format) {
|
| 9 |
+
TORCH_CHECK(
|
| 10 |
+
options.requires_grad_opt() == std::nullopt ||
|
| 11 |
+
options.requires_grad_opt().value() == false,
|
| 12 |
+
"Operators taking TensorOptions cannot take a TensorOptions with "
|
| 13 |
+
"options.requires_grad set as true. This isn't implemented yet.");
|
| 14 |
+
TORCH_CHECK(
|
| 15 |
+
!(options.has_memory_format() && memory_format.has_value()),
|
| 16 |
+
"Cannot set memory_format both in TensorOptions and explicit argument; please delete "
|
| 17 |
+
"the redundant setter.");
|
| 18 |
+
if (memory_format.has_value()) {
|
| 19 |
+
return memory_format;
|
| 20 |
+
} else {
|
| 21 |
+
return options.memory_format_opt();
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
} // namespace impl namespace c10
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/Generator.h
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cstdint>
|
| 4 |
+
#include <deque>
|
| 5 |
+
#include <mutex>
|
| 6 |
+
#include <utility>
|
| 7 |
+
|
| 8 |
+
#include <c10/util/Exception.h>
|
| 9 |
+
#include <c10/util/intrusive_ptr.h>
|
| 10 |
+
#include <c10/core/Device.h>
|
| 11 |
+
#include <c10/core/DispatchKeySet.h>
|
| 12 |
+
|
| 13 |
+
// For the record I don't think this is a correct pimpl idiom.
|
| 14 |
+
// Including Impl header in interface header defeats the purpose
|
| 15 |
+
// because you can't change Impl private members without forcing
|
| 16 |
+
// everything that included the interface to rebuild.
|
| 17 |
+
// Impl should be forward-declared in the interface header instead.
|
| 18 |
+
#include <c10/core/GeneratorImpl.h>
|
| 19 |
+
|
| 20 |
+
/**
|
| 21 |
+
* Note [Generator]
|
| 22 |
+
* ~~~~~~~~~~~~~~~~
|
| 23 |
+
* A Pseudo Random Number Generator (PRNG) is an engine that uses an algorithm to
|
| 24 |
+
* generate a seemingly random sequence of numbers, that may be later be used in creating
|
| 25 |
+
* a random distribution. Such an engine almost always maintains a state and requires a
|
| 26 |
+
* seed to start off the creation of random numbers. Often times, users have
|
| 27 |
+
* found it beneficial to be able to explicitly create, retain, and destroy
|
| 28 |
+
* PRNG states and also be able to have control over the seed value.
|
| 29 |
+
*
|
| 30 |
+
* A Generator in ATen gives users the ability to read, write and modify a PRNG engine.
|
| 31 |
+
* For instance, it does so by letting users seed a PRNG engine, fork the state of the
|
| 32 |
+
* engine, etc.
|
| 33 |
+
*
|
| 34 |
+
* By default, there is one generator per device, and a device's generator is
|
| 35 |
+
* lazily created. A user can use the torch.Generator() api to create their own generator.
|
| 36 |
+
*/
|
| 37 |
+
|
| 38 |
+
/**
|
| 39 |
+
* Note [Acquire lock when using random generators]
|
| 40 |
+
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 41 |
+
* Generator and its derived classes are NOT thread-safe. Please note that most of the
|
| 42 |
+
* places where we have inserted locking for generators are historically based, and we
|
| 43 |
+
* haven't actually checked that everything is truly thread safe (and it probably isn't).
|
| 44 |
+
* Please use the public mutex_ when using any methods from these classes, except for the
|
| 45 |
+
* read-only methods. You can learn about the usage by looking into the unittests
|
| 46 |
+
* (aten/src/ATen/cpu_generator_test.cpp) and other places where we have used lock_guard.
|
| 47 |
+
*
|
| 48 |
+
* TODO: Look into changing the threading semantics of Generators in ATen (e.g., making
|
| 49 |
+
* them non-thread safe and instead making the generator state splittable, to accommodate
|
| 50 |
+
* forks into other threads).
|
| 51 |
+
*/
|
| 52 |
+
|
| 53 |
+
namespace at {
|
| 54 |
+
|
| 55 |
+
class Tensor;
|
| 56 |
+
|
| 57 |
+
struct TORCH_API Generator {
|
| 58 |
+
Generator() = default;
|
| 59 |
+
|
| 60 |
+
explicit Generator(c10::intrusive_ptr<c10::GeneratorImpl> gen_impl)
|
| 61 |
+
: impl_(std::move(gen_impl)) {
|
| 62 |
+
if (impl_.get() == nullptr) {
|
| 63 |
+
throw std::runtime_error("GeneratorImpl with nullptr is not supported");
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
bool operator==(const Generator& rhs) const {
|
| 68 |
+
return this->impl_ == rhs.impl_;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
bool operator!=(const Generator& rhs) const {
|
| 72 |
+
return !((*this) == rhs);
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
bool defined() const {
|
| 76 |
+
return static_cast<bool>(impl_);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
c10::GeneratorImpl* unsafeGetGeneratorImpl() const {
|
| 80 |
+
return impl_.get();
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
c10::GeneratorImpl* unsafeReleaseGeneratorImpl() {
|
| 84 |
+
return impl_.release();
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
const c10::intrusive_ptr<c10::GeneratorImpl>& getIntrusivePtr() const {
|
| 88 |
+
return impl_;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
void set_current_seed(uint64_t seed) { impl_->set_current_seed(seed); }
|
| 92 |
+
// Sets the offset of Generator state to the desired offset. This is currently
|
| 93 |
+
// supported for only Philox based Generators, i.e., CUDA and MPS.
|
| 94 |
+
void set_offset(uint64_t offset) { impl_->set_offset(offset); }
|
| 95 |
+
|
| 96 |
+
// Returns the offset of Generator state. This is currently supported for only
|
| 97 |
+
// Philox based Generators, i.e., CUDA and MPS.
|
| 98 |
+
uint64_t get_offset() const { return impl_->get_offset(); }
|
| 99 |
+
|
| 100 |
+
uint64_t current_seed() const { return impl_->current_seed(); }
|
| 101 |
+
|
| 102 |
+
uint64_t seed() { return impl_->seed(); }
|
| 103 |
+
|
| 104 |
+
// Implementation not inlined to prevent cycle reference between
|
| 105 |
+
// `ATen/core/Generator.h` and `ATen/core/Tensor.h`
|
| 106 |
+
void set_state(const at::Tensor& new_state);
|
| 107 |
+
|
| 108 |
+
at::Tensor get_state() const;
|
| 109 |
+
|
| 110 |
+
void graphsafe_set_state(const Generator& new_state);
|
| 111 |
+
|
| 112 |
+
Generator graphsafe_get_state() const;
|
| 113 |
+
|
| 114 |
+
std::mutex& mutex() {
|
| 115 |
+
return impl_->mutex_;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
DispatchKeySet key_set() const {
|
| 119 |
+
return impl_->key_set();
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
Device device() const { return impl_->device(); }
|
| 123 |
+
|
| 124 |
+
inline void set_pyobj(PyObject* pyobj) const noexcept {
|
| 125 |
+
impl_->set_pyobj(pyobj);
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
inline PyObject* pyobj() const noexcept {
|
| 129 |
+
return impl_->pyobj();
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
template<typename T>
|
| 133 |
+
T* get() const { return static_cast<T*>(impl_.get()); }
|
| 134 |
+
|
| 135 |
+
Generator clone() const {
|
| 136 |
+
return Generator(impl_->clone());
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
private:
|
| 140 |
+
c10::intrusive_ptr<c10::GeneratorImpl> impl_;
|
| 141 |
+
};
|
| 142 |
+
|
| 143 |
+
template<class Impl, class... Args>
|
| 144 |
+
Generator make_generator(Args&&... args) {
|
| 145 |
+
return Generator(c10::make_intrusive<Impl>(std::forward<Args>(args)...));
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
/**
|
| 149 |
+
* Utility function to static cast input Generator* to
|
| 150 |
+
* the backend generator type (CPU/CUDAGeneratorImpl etc.)
|
| 151 |
+
*/
|
| 152 |
+
template <typename T>
|
| 153 |
+
inline T * check_generator(std::optional<Generator> gen) {
|
| 154 |
+
TORCH_CHECK(gen.has_value(), "Expected Generator but received nullopt");
|
| 155 |
+
TORCH_CHECK(gen->defined(), "Generator with undefined implementation is not allowed");
|
| 156 |
+
TORCH_CHECK(T::device_type() == gen->device().type(), "Expected a '", T::device_type(), "' device type for generator but found '", gen->device().type(), "'");
|
| 157 |
+
return gen->get<T>();
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
/**
|
| 161 |
+
* Utility function used in tensor implementations, which
|
| 162 |
+
* supplies the default generator to tensors, if an input generator
|
| 163 |
+
* is not supplied. The input Generator* is also static casted to
|
| 164 |
+
* the backend generator type (CPU/CUDAGeneratorImpl etc.)
|
| 165 |
+
*/
|
| 166 |
+
template <typename T>
|
| 167 |
+
inline T* get_generator_or_default(const std::optional<Generator>& gen, const Generator& default_gen) {
|
| 168 |
+
return gen.has_value() && gen->defined() ? check_generator<T>(gen) : check_generator<T>(default_gen);
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
namespace detail {
|
| 172 |
+
|
| 173 |
+
/**
|
| 174 |
+
* Helper function for checking the validity of new random generator
|
| 175 |
+
* state. Right now following conditions are checked:
|
| 176 |
+
*
|
| 177 |
+
* - The new state tensor must be a torch.ByteTensor
|
| 178 |
+
* - Data of the new state tensor must be contiguous
|
| 179 |
+
*/
|
| 180 |
+
inline void check_rng_state(const c10::TensorImpl& new_state) {
|
| 181 |
+
TORCH_CHECK_TYPE(
|
| 182 |
+
new_state.layout() == kStrided && new_state.device().type() == kCPU && new_state.dtype() == kByte,
|
| 183 |
+
"RNG state must be a torch.ByteTensor"
|
| 184 |
+
);
|
| 185 |
+
|
| 186 |
+
TORCH_CHECK(new_state.is_contiguous(), "RNG state must be contiguous");
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
} // namespace detail
|
| 190 |
+
|
| 191 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/NamedTensor.h
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/core/Dimname.h>
|
| 4 |
+
#include <c10/core/TensorImpl.h>
|
| 5 |
+
|
| 6 |
+
namespace at {
|
| 7 |
+
|
| 8 |
+
class TensorBase;
|
| 9 |
+
|
| 10 |
+
// XXX: This file exists because TensorImpl is in c10, but Dimname is in ATen.
|
| 11 |
+
// Due to the c10/ATen library split, TensorImpl cannot depend on Dimname,
|
| 12 |
+
// so we have a couple of workarounds.
|
| 13 |
+
//
|
| 14 |
+
// In the long term, we'll move Dimname to c10 and everything in this file
|
| 15 |
+
// can be refactored out. The main blocker for that is that "c10::Symbol"
|
| 16 |
+
// actually exists outside of c10 and needs to be moved in.
|
| 17 |
+
|
| 18 |
+
// TensorImpl has a unique_ptr<NamedTensorMetaInterface> field.
|
| 19 |
+
// XXX: Ideally we would just put std::optional<vector<Dimname>> into TensorImpl.
|
| 20 |
+
//
|
| 21 |
+
// This class has an important invariant: there must be at least ONE
|
| 22 |
+
// non-wildcard
|
| 23 |
+
struct TORCH_API NamedTensorMeta final : public c10::NamedTensorMetaInterface {
|
| 24 |
+
// This enum is to remind people that the invariant on constructors is that
|
| 25 |
+
// the list of dimnames must have at least one non-wildcard
|
| 26 |
+
enum HAS_NON_WILDCARD {
|
| 27 |
+
HasNonWildcard
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
+
explicit NamedTensorMeta(HAS_NON_WILDCARD, DimnameList names)
|
| 31 |
+
: names_(names.vec()) {
|
| 32 |
+
check_invariants();
|
| 33 |
+
}
|
| 34 |
+
explicit NamedTensorMeta(HAS_NON_WILDCARD, std::vector<Dimname>&& names)
|
| 35 |
+
: names_(std::move(names)) {
|
| 36 |
+
check_invariants();
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
std::unique_ptr<c10::NamedTensorMetaInterface> clone() const override {
|
| 40 |
+
return std::make_unique<NamedTensorMeta>(HasNonWildcard, names_);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
DimnameList names() const { return names_; }
|
| 44 |
+
|
| 45 |
+
// Used for an assertion in TensorImpl.h
|
| 46 |
+
int64_t slow_dim() const override {
|
| 47 |
+
return static_cast<int64_t>(names_.size());
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
void check_invariants() const {
|
| 51 |
+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
|
| 52 |
+
std::any_of(names_.begin(), names_.end(), [](const Dimname& n) { return !n.isWildcard(); }));
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
void set_names(HAS_NON_WILDCARD, DimnameList new_names) {
|
| 56 |
+
TORCH_INTERNAL_ASSERT(new_names.size() == names_.size());
|
| 57 |
+
std::copy(new_names.begin(), new_names.end(), names_.begin());
|
| 58 |
+
check_invariants();
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
void set_names(HAS_NON_WILDCARD, std::vector<Dimname>&& new_names) {
|
| 62 |
+
TORCH_INTERNAL_ASSERT(new_names.size() == names_.size());
|
| 63 |
+
names_ = std::move(new_names);
|
| 64 |
+
check_invariants();
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
// INVARIANT: at least one Dimname is non-WILDCARD
|
| 68 |
+
std::vector<Dimname> names_;
|
| 69 |
+
};
|
| 70 |
+
|
| 71 |
+
// When NamesMode is disabled, then all operations ignore tensors' names fields.
|
| 72 |
+
// Concretely speaking, all tensors are treated as having nullopt names.
|
| 73 |
+
struct TORCH_API NamesMode {
|
| 74 |
+
static bool is_enabled();
|
| 75 |
+
static void set_enabled(bool enabled);
|
| 76 |
+
};
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
// A RAII, thread local (!) guard that enables or disables names upon
|
| 80 |
+
// construction, and sets it back to the original value upon destruction.
|
| 81 |
+
struct TORCH_API NoNamesGuard {
|
| 82 |
+
NoNamesGuard() : prev_mode(NamesMode::is_enabled()) {
|
| 83 |
+
NamesMode::set_enabled(false);
|
| 84 |
+
}
|
| 85 |
+
~NoNamesGuard() {
|
| 86 |
+
if (initialized) {
|
| 87 |
+
reset();
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
void reset() {
|
| 91 |
+
TORCH_INTERNAL_ASSERT(initialized);
|
| 92 |
+
NamesMode::set_enabled(prev_mode);
|
| 93 |
+
}
|
| 94 |
+
private:
|
| 95 |
+
bool prev_mode;
|
| 96 |
+
bool initialized{true};
|
| 97 |
+
};
|
| 98 |
+
|
| 99 |
+
void check_names_valid_for(const TensorBase& tensor, DimnameList names);
|
| 100 |
+
void check_names_valid_for(size_t tensor_dim, DimnameList names);
|
| 101 |
+
|
| 102 |
+
// Sets the names of `tensor` to be `names`.
|
| 103 |
+
TORCH_API const TensorBase& internal_set_names_inplace(const TensorBase& tensor, std::optional<DimnameList> names);
|
| 104 |
+
TORCH_API const TensorBase& internal_set_names_inplace(const TensorBase& tensor, std::vector<Dimname>&& names, bool validate_names);
|
| 105 |
+
|
| 106 |
+
constexpr size_t kMaxNamedTensorDim = 64;
|
| 107 |
+
|
| 108 |
+
DimnameList default_names(size_t len);
|
| 109 |
+
|
| 110 |
+
namespace impl {
|
| 111 |
+
|
| 112 |
+
// Some helper functions on TensorImpl. Useful for working with names in TH.
|
| 113 |
+
// XXX: Ideally these would exist as methods on TensorImpl
|
| 114 |
+
TORCH_API void internal_set_names_inplace(TensorImpl* impl, std::optional<DimnameList> names, bool validate_names);
|
| 115 |
+
TORCH_API void internal_set_names_inplace(TensorImpl* impl, std::vector<Dimname>&& names, bool validate_names);
|
| 116 |
+
|
| 117 |
+
void check_names_valid_for(TensorImpl* impl, DimnameList names);
|
| 118 |
+
|
| 119 |
+
// Returns true if the tensor's names exist and are not all 'None'.
|
| 120 |
+
// Returns false if the tensor's names don't exist (were not allocated),
|
| 121 |
+
// or if all names are 'None'.
|
| 122 |
+
// We treat not-allocated-names the same as allocated names that are all 'None'.
|
| 123 |
+
TORCH_API bool has_names(const TensorImpl* impl);
|
| 124 |
+
|
| 125 |
+
// Returns the names of the tensor's dimensions.
|
| 126 |
+
// Unnamed tensors are treated as having 'None' in all dimension; this method
|
| 127 |
+
// would return a DimnameList of all 'None's for an unnamed tensor.
|
| 128 |
+
TORCH_API DimnameList get_names(const TensorImpl* impl);
|
| 129 |
+
|
| 130 |
+
// This is more of an implementation detail; one should use impl::get_names /
|
| 131 |
+
// Tensor::names() whenever possible because it provides a cleaner API.
|
| 132 |
+
// Returns the names of the tensor if they have been allocated; returns nullopt
|
| 133 |
+
// instead if the haven't been. The names of a tensor are not allocated if a
|
| 134 |
+
// tensor is constructed with names=None.
|
| 135 |
+
TORCH_API std::optional<DimnameList> get_opt_names(const TensorImpl* impl);
|
| 136 |
+
|
| 137 |
+
} // namespace impl
|
| 138 |
+
|
| 139 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/TensorAccessor.h
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <c10/macros/Macros.h>
|
| 4 |
+
#include <c10/util/ArrayRef.h>
|
| 5 |
+
#include <c10/util/Deprecated.h>
|
| 6 |
+
#include <c10/util/Exception.h>
|
| 7 |
+
#include <c10/util/irange.h>
|
| 8 |
+
#include <cstddef>
|
| 9 |
+
#include <cstdint>
|
| 10 |
+
#include <type_traits>
|
| 11 |
+
|
| 12 |
+
namespace at {
|
| 13 |
+
|
| 14 |
+
// The PtrTraits argument to the TensorAccessor/GenericPackedTensorAccessor
|
| 15 |
+
// is used to enable the __restrict__ keyword/modifier for the data
|
| 16 |
+
// passed to cuda.
|
| 17 |
+
template <typename T>
|
| 18 |
+
struct DefaultPtrTraits {
|
| 19 |
+
typedef T* PtrType;
|
| 20 |
+
};
|
| 21 |
+
|
| 22 |
+
#if defined(__CUDACC__) || defined(__HIPCC__)
|
| 23 |
+
template <typename T>
|
| 24 |
+
struct RestrictPtrTraits {
|
| 25 |
+
typedef T* __restrict__ PtrType;
|
| 26 |
+
};
|
| 27 |
+
#endif
|
| 28 |
+
|
| 29 |
+
// TensorAccessorBase and TensorAccessor are used for both CPU and CUDA tensors.
|
| 30 |
+
// For CUDA tensors it is used in device code (only). This means that we restrict ourselves
|
| 31 |
+
// to functions and types available there (e.g. IntArrayRef isn't).
|
| 32 |
+
|
| 33 |
+
// The PtrTraits argument is only relevant to cuda to support `__restrict__` pointers.
|
| 34 |
+
template<typename T, size_t N, template <typename U> class PtrTraits = DefaultPtrTraits, typename index_t = int64_t>
|
| 35 |
+
class TensorAccessorBase {
|
| 36 |
+
public:
|
| 37 |
+
typedef typename PtrTraits<T>::PtrType PtrType;
|
| 38 |
+
|
| 39 |
+
C10_HOST_DEVICE TensorAccessorBase(
|
| 40 |
+
PtrType data_,
|
| 41 |
+
const index_t* sizes_,
|
| 42 |
+
const index_t* strides_)
|
| 43 |
+
: data_(data_), sizes_(sizes_), strides_(strides_) {}
|
| 44 |
+
C10_HOST IntArrayRef sizes() const {
|
| 45 |
+
return IntArrayRef(sizes_,N);
|
| 46 |
+
}
|
| 47 |
+
C10_HOST IntArrayRef strides() const {
|
| 48 |
+
return IntArrayRef(strides_,N);
|
| 49 |
+
}
|
| 50 |
+
C10_HOST_DEVICE index_t stride(index_t i) const {
|
| 51 |
+
return strides_[i];
|
| 52 |
+
}
|
| 53 |
+
C10_HOST_DEVICE index_t size(index_t i) const {
|
| 54 |
+
return sizes_[i];
|
| 55 |
+
}
|
| 56 |
+
C10_HOST_DEVICE PtrType data() {
|
| 57 |
+
return data_;
|
| 58 |
+
}
|
| 59 |
+
C10_HOST_DEVICE const PtrType data() const {
|
| 60 |
+
return data_;
|
| 61 |
+
}
|
| 62 |
+
protected:
|
| 63 |
+
PtrType data_;
|
| 64 |
+
const index_t* sizes_;
|
| 65 |
+
const index_t* strides_;
|
| 66 |
+
};
|
| 67 |
+
|
| 68 |
+
// The `TensorAccessor` is typically instantiated for CPU `Tensor`s using
|
| 69 |
+
// `Tensor.accessor<T, N>()`.
|
| 70 |
+
// For CUDA `Tensor`s, `GenericPackedTensorAccessor` is used on the host and only
|
| 71 |
+
// indexing on the device uses `TensorAccessor`s.
|
| 72 |
+
template<typename T, size_t N, template <typename U> class PtrTraits = DefaultPtrTraits, typename index_t = int64_t>
|
| 73 |
+
class TensorAccessor : public TensorAccessorBase<T,N,PtrTraits,index_t> {
|
| 74 |
+
public:
|
| 75 |
+
typedef typename PtrTraits<T>::PtrType PtrType;
|
| 76 |
+
|
| 77 |
+
C10_HOST_DEVICE TensorAccessor(
|
| 78 |
+
PtrType data_,
|
| 79 |
+
const index_t* sizes_,
|
| 80 |
+
const index_t* strides_)
|
| 81 |
+
: TensorAccessorBase<T, N, PtrTraits, index_t>(data_,sizes_,strides_) {}
|
| 82 |
+
|
| 83 |
+
C10_HOST_DEVICE TensorAccessor<T, N - 1, PtrTraits, index_t> operator[](index_t i) {
|
| 84 |
+
return TensorAccessor<T,N-1,PtrTraits,index_t>(this->data_ + this->strides_[0]*i,this->sizes_+1,this->strides_+1);
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
C10_HOST_DEVICE const TensorAccessor<T, N-1, PtrTraits, index_t> operator[](index_t i) const {
|
| 88 |
+
return TensorAccessor<T,N-1,PtrTraits,index_t>(this->data_ + this->strides_[0]*i,this->sizes_+1,this->strides_+1);
|
| 89 |
+
}
|
| 90 |
+
};
|
| 91 |
+
|
| 92 |
+
template<typename T, template <typename U> class PtrTraits, typename index_t>
|
| 93 |
+
class TensorAccessor<T,1,PtrTraits,index_t> : public TensorAccessorBase<T,1,PtrTraits,index_t> {
|
| 94 |
+
public:
|
| 95 |
+
typedef typename PtrTraits<T>::PtrType PtrType;
|
| 96 |
+
|
| 97 |
+
C10_HOST_DEVICE TensorAccessor(
|
| 98 |
+
PtrType data_,
|
| 99 |
+
const index_t* sizes_,
|
| 100 |
+
const index_t* strides_)
|
| 101 |
+
: TensorAccessorBase<T, 1, PtrTraits, index_t>(data_,sizes_,strides_) {}
|
| 102 |
+
C10_HOST_DEVICE T & operator[](index_t i) {
|
| 103 |
+
// NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
|
| 104 |
+
return this->data_[this->strides_[0]*i];
|
| 105 |
+
}
|
| 106 |
+
C10_HOST_DEVICE const T & operator[](index_t i) const {
|
| 107 |
+
return this->data_[this->strides_[0]*i];
|
| 108 |
+
}
|
| 109 |
+
};
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
// GenericPackedTensorAccessorBase and GenericPackedTensorAccessor are used on for CUDA `Tensor`s on the host
|
| 113 |
+
// and as
|
| 114 |
+
// In contrast to `TensorAccessor`s, they copy the strides and sizes on instantiation (on the host)
|
| 115 |
+
// in order to transfer them on the device when calling kernels.
|
| 116 |
+
// On the device, indexing of multidimensional tensors gives to `TensorAccessor`s.
|
| 117 |
+
// Use RestrictPtrTraits as PtrTraits if you want the tensor's data pointer to be marked as __restrict__.
|
| 118 |
+
// Instantiation from data, sizes, strides is only needed on the host and std::copy isn't available
|
| 119 |
+
// on the device, so those functions are host only.
|
| 120 |
+
template<typename T, size_t N, template <typename U> class PtrTraits = DefaultPtrTraits, typename index_t = int64_t>
|
| 121 |
+
class GenericPackedTensorAccessorBase {
|
| 122 |
+
public:
|
| 123 |
+
typedef typename PtrTraits<T>::PtrType PtrType;
|
| 124 |
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
|
| 125 |
+
C10_HOST GenericPackedTensorAccessorBase(
|
| 126 |
+
PtrType data_,
|
| 127 |
+
const index_t* sizes_,
|
| 128 |
+
const index_t* strides_)
|
| 129 |
+
: data_(data_) {
|
| 130 |
+
std::copy(sizes_, sizes_ + N, std::begin(this->sizes_));
|
| 131 |
+
std::copy(strides_, strides_ + N, std::begin(this->strides_));
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
// if index_t is not int64_t, we want to have an int64_t constructor
|
| 135 |
+
template <typename source_index_t, class = std::enable_if_t<std::is_same_v<source_index_t, int64_t>>>
|
| 136 |
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
|
| 137 |
+
C10_HOST GenericPackedTensorAccessorBase(
|
| 138 |
+
PtrType data_,
|
| 139 |
+
const source_index_t* sizes_,
|
| 140 |
+
const source_index_t* strides_)
|
| 141 |
+
: data_(data_) {
|
| 142 |
+
for (const auto i : c10::irange(N)) {
|
| 143 |
+
this->sizes_[i] = sizes_[i];
|
| 144 |
+
this->strides_[i] = strides_[i];
|
| 145 |
+
}
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
C10_HOST_DEVICE index_t stride(index_t i) const {
|
| 149 |
+
return strides_[i];
|
| 150 |
+
}
|
| 151 |
+
C10_HOST_DEVICE index_t size(index_t i) const {
|
| 152 |
+
return sizes_[i];
|
| 153 |
+
}
|
| 154 |
+
C10_HOST_DEVICE PtrType data() {
|
| 155 |
+
return data_;
|
| 156 |
+
}
|
| 157 |
+
C10_HOST_DEVICE const PtrType data() const {
|
| 158 |
+
return data_;
|
| 159 |
+
}
|
| 160 |
+
protected:
|
| 161 |
+
PtrType data_;
|
| 162 |
+
// NOLINTNEXTLINE(*c-arrays*)
|
| 163 |
+
index_t sizes_[N];
|
| 164 |
+
// NOLINTNEXTLINE(*c-arrays*)
|
| 165 |
+
index_t strides_[N];
|
| 166 |
+
C10_HOST void bounds_check_(index_t i) const {
|
| 167 |
+
TORCH_CHECK_INDEX(
|
| 168 |
+
0 <= i && i < index_t{N},
|
| 169 |
+
"Index ",
|
| 170 |
+
i,
|
| 171 |
+
" is not within bounds of a tensor of dimension ",
|
| 172 |
+
N);
|
| 173 |
+
}
|
| 174 |
+
};
|
| 175 |
+
|
| 176 |
+
template<typename T, size_t N, template <typename U> class PtrTraits = DefaultPtrTraits, typename index_t = int64_t>
|
| 177 |
+
class GenericPackedTensorAccessor : public GenericPackedTensorAccessorBase<T,N,PtrTraits,index_t> {
|
| 178 |
+
public:
|
| 179 |
+
typedef typename PtrTraits<T>::PtrType PtrType;
|
| 180 |
+
|
| 181 |
+
C10_HOST GenericPackedTensorAccessor(
|
| 182 |
+
PtrType data_,
|
| 183 |
+
const index_t* sizes_,
|
| 184 |
+
const index_t* strides_)
|
| 185 |
+
: GenericPackedTensorAccessorBase<T, N, PtrTraits, index_t>(data_, sizes_, strides_) {}
|
| 186 |
+
|
| 187 |
+
// if index_t is not int64_t, we want to have an int64_t constructor
|
| 188 |
+
template <typename source_index_t, class = std::enable_if_t<std::is_same_v<source_index_t, int64_t>>>
|
| 189 |
+
C10_HOST GenericPackedTensorAccessor(
|
| 190 |
+
PtrType data_,
|
| 191 |
+
const source_index_t* sizes_,
|
| 192 |
+
const source_index_t* strides_)
|
| 193 |
+
: GenericPackedTensorAccessorBase<T, N, PtrTraits, index_t>(data_, sizes_, strides_) {}
|
| 194 |
+
|
| 195 |
+
C10_DEVICE TensorAccessor<T, N - 1, PtrTraits, index_t> operator[](index_t i) {
|
| 196 |
+
index_t* new_sizes = this->sizes_ + 1;
|
| 197 |
+
index_t* new_strides = this->strides_ + 1;
|
| 198 |
+
return TensorAccessor<T,N-1,PtrTraits,index_t>(this->data_ + this->strides_[0]*i, new_sizes, new_strides);
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
C10_DEVICE const TensorAccessor<T, N - 1, PtrTraits, index_t> operator[](index_t i) const {
|
| 202 |
+
const index_t* new_sizes = this->sizes_ + 1;
|
| 203 |
+
const index_t* new_strides = this->strides_ + 1;
|
| 204 |
+
return TensorAccessor<T,N-1,PtrTraits,index_t>(this->data_ + this->strides_[0]*i, new_sizes, new_strides);
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
/// Returns a PackedTensorAccessor of the same dimension after transposing the
|
| 208 |
+
/// two dimensions given. Does not actually move elements; transposition is
|
| 209 |
+
/// made by permuting the size/stride arrays. If the dimensions are not valid,
|
| 210 |
+
/// asserts.
|
| 211 |
+
C10_HOST GenericPackedTensorAccessor<T, N, PtrTraits, index_t> transpose(
|
| 212 |
+
index_t dim1,
|
| 213 |
+
index_t dim2) const {
|
| 214 |
+
this->bounds_check_(dim1);
|
| 215 |
+
this->bounds_check_(dim2);
|
| 216 |
+
GenericPackedTensorAccessor<T, N, PtrTraits, index_t> result(
|
| 217 |
+
this->data_, this->sizes_, this->strides_);
|
| 218 |
+
std::swap(result.strides_[dim1], result.strides_[dim2]);
|
| 219 |
+
std::swap(result.sizes_[dim1], result.sizes_[dim2]);
|
| 220 |
+
return result;
|
| 221 |
+
}
|
| 222 |
+
};
|
| 223 |
+
|
| 224 |
+
template<typename T, template <typename U> class PtrTraits, typename index_t>
|
| 225 |
+
class GenericPackedTensorAccessor<T,1,PtrTraits,index_t> : public GenericPackedTensorAccessorBase<T,1,PtrTraits,index_t> {
|
| 226 |
+
public:
|
| 227 |
+
typedef typename PtrTraits<T>::PtrType PtrType;
|
| 228 |
+
C10_HOST GenericPackedTensorAccessor(
|
| 229 |
+
PtrType data_,
|
| 230 |
+
const index_t* sizes_,
|
| 231 |
+
const index_t* strides_)
|
| 232 |
+
: GenericPackedTensorAccessorBase<T, 1, PtrTraits, index_t>(data_, sizes_, strides_) {}
|
| 233 |
+
|
| 234 |
+
// if index_t is not int64_t, we want to have an int64_t constructor
|
| 235 |
+
template <typename source_index_t, class = std::enable_if_t<std::is_same_v<source_index_t, int64_t>>>
|
| 236 |
+
C10_HOST GenericPackedTensorAccessor(
|
| 237 |
+
PtrType data_,
|
| 238 |
+
const source_index_t* sizes_,
|
| 239 |
+
const source_index_t* strides_)
|
| 240 |
+
: GenericPackedTensorAccessorBase<T, 1, PtrTraits, index_t>(data_, sizes_, strides_) {}
|
| 241 |
+
|
| 242 |
+
C10_DEVICE T & operator[](index_t i) {
|
| 243 |
+
return this->data_[this->strides_[0] * i];
|
| 244 |
+
}
|
| 245 |
+
C10_DEVICE const T& operator[](index_t i) const {
|
| 246 |
+
return this->data_[this->strides_[0]*i];
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
// Same as in the general N-dimensional case, but note that in the
|
| 250 |
+
// 1-dimensional case the returned PackedTensorAccessor will always be an
|
| 251 |
+
// identical copy of the original
|
| 252 |
+
C10_HOST GenericPackedTensorAccessor<T, 1, PtrTraits, index_t> transpose(
|
| 253 |
+
index_t dim1,
|
| 254 |
+
index_t dim2) const {
|
| 255 |
+
this->bounds_check_(dim1);
|
| 256 |
+
this->bounds_check_(dim2);
|
| 257 |
+
return GenericPackedTensorAccessor<T, 1, PtrTraits, index_t>(
|
| 258 |
+
this->data_, this->sizes_, this->strides_);
|
| 259 |
+
}
|
| 260 |
+
};
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
// Can't put this directly into the macro function args because of commas
|
| 264 |
+
#define AT_X GenericPackedTensorAccessor<T, N, PtrTraits, index_t>
|
| 265 |
+
|
| 266 |
+
// Old name for `GenericPackedTensorAccessor`
|
| 267 |
+
template <typename T, size_t N, template <typename U> class PtrTraits = DefaultPtrTraits, typename index_t = int64_t>
|
| 268 |
+
C10_DEFINE_DEPRECATED_USING(PackedTensorAccessor, AT_X)
|
| 269 |
+
|
| 270 |
+
#undef AT_X
|
| 271 |
+
|
| 272 |
+
template <typename T, size_t N, template <typename U> class PtrTraits = DefaultPtrTraits>
|
| 273 |
+
using PackedTensorAccessor32 = GenericPackedTensorAccessor<T, N, PtrTraits, int32_t>;
|
| 274 |
+
|
| 275 |
+
template <typename T, size_t N, template <typename U> class PtrTraits = DefaultPtrTraits>
|
| 276 |
+
using PackedTensorAccessor64 = GenericPackedTensorAccessor<T, N, PtrTraits, int64_t>;
|
| 277 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/TensorBody.h
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/boxing/BoxedKernel_impl.h
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
namespace c10 {
|
| 4 |
+
|
| 5 |
+
inline BoxedKernel::BoxedKernel()
|
| 6 |
+
: functor_()
|
| 7 |
+
, boxed_kernel_func_(nullptr)
|
| 8 |
+
{}
|
| 9 |
+
|
| 10 |
+
inline BoxedKernel::BoxedKernel(std::unique_ptr<OperatorKernel> functor, InternalBoxedKernelFunction* boxed_kernel_func)
|
| 11 |
+
: functor_(std::move(functor))
|
| 12 |
+
, boxed_kernel_func_(boxed_kernel_func)
|
| 13 |
+
{}
|
| 14 |
+
|
| 15 |
+
template<BoxedKernel::BoxedKernelFunction* func>
|
| 16 |
+
inline void BoxedKernel::make_boxed_function(OperatorKernel*, const OperatorHandle& opHandle, DispatchKeySet, Stack* stack) {
|
| 17 |
+
// Note that we're dropping the DispatchKeySet argument.
|
| 18 |
+
// See Note [Plumbing Keys Through The Dispatcher 2] for details.
|
| 19 |
+
func(opHandle, stack);
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
template<BoxedKernel::BoxedKernelFunction_withDispatchKeys* func>
|
| 23 |
+
inline void BoxedKernel::make_boxed_function(OperatorKernel*, const OperatorHandle& opHandle, DispatchKeySet ks, Stack* stack) {
|
| 24 |
+
// See Note [Plumbing Keys Through The Dispatcher 2] for details.
|
| 25 |
+
func(opHandle, ks, stack);
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
inline bool BoxedKernel::isValid() const {
|
| 29 |
+
return boxed_kernel_func_ != nullptr;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
inline bool BoxedKernel::isFallthrough() const {
|
| 33 |
+
return boxed_kernel_func_ == &fallthrough_kernel;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
inline void BoxedKernel::callBoxed(const OperatorHandle& opHandle, DispatchKeySet dispatchKeySet, Stack* stack) const {
|
| 37 |
+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
|
| 38 |
+
boxed_kernel_func_ != nullptr,
|
| 39 |
+
"Tried to call BoxedKernel::callBoxed() on an uninitialized BoxedKernel."
|
| 40 |
+
);
|
| 41 |
+
(*boxed_kernel_func_)(functor_.get(), opHandle, dispatchKeySet, stack);
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
template<BoxedKernel::BoxedKernelFunction* func>
|
| 45 |
+
inline BoxedKernel BoxedKernel::makeFromFunction() {
|
| 46 |
+
return BoxedKernel(
|
| 47 |
+
nullptr, // no functor_ object
|
| 48 |
+
&make_boxed_function<func>
|
| 49 |
+
);
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
template<BoxedKernel::BoxedKernelFunction_withDispatchKeys* func>
|
| 53 |
+
inline BoxedKernel BoxedKernel::makeFromFunction() {
|
| 54 |
+
return BoxedKernel(
|
| 55 |
+
nullptr, // no functor_ object
|
| 56 |
+
&make_boxed_function<func>
|
| 57 |
+
);
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
inline BoxedKernel BoxedKernel::makeFallthrough() {
|
| 61 |
+
return BoxedKernel(
|
| 62 |
+
nullptr, // no functor_ object
|
| 63 |
+
&fallthrough_kernel
|
| 64 |
+
);
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
inline BoxedKernel BoxedKernel::makeAmbiguousAutogradOther() {
|
| 68 |
+
return BoxedKernel(
|
| 69 |
+
nullptr, // no functor_ object
|
| 70 |
+
&ambiguous_autogradother_kernel
|
| 71 |
+
);
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
inline BoxedKernel BoxedKernel::makeNamedNotSupported() {
|
| 75 |
+
return BoxedKernel(
|
| 76 |
+
nullptr, // no functor_ object
|
| 77 |
+
&named_not_supported_kernel
|
| 78 |
+
);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
template<class KernelFunctor>
|
| 82 |
+
inline BoxedKernel BoxedKernel::makeFromFunctor(std::unique_ptr<KernelFunctor> kernelFunctor) {
|
| 83 |
+
static_assert(std::is_base_of<OperatorKernel, KernelFunctor>::value, "Tried to call BoxedKernel::makeFromFunctor<KernelFunctor>, but the functor doesn't inherit from c10::OperatorKernel. Please have the functor inherit from it.");
|
| 84 |
+
return BoxedKernel(
|
| 85 |
+
std::move(kernelFunctor),
|
| 86 |
+
[](OperatorKernel* kernel, const OperatorHandle& op, DispatchKeySet ks, Stack* stack) {
|
| 87 |
+
(*static_cast<KernelFunctor*>(kernel))(op, ks, stack);
|
| 88 |
+
}
|
| 89 |
+
);
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
inline OperatorKernel* BoxedKernel::getFunctor() const {
|
| 93 |
+
return functor_.get();
|
| 94 |
+
}
|
| 95 |
+
inline BoxedKernel::InternalBoxedKernelFunction* BoxedKernel::getFnPtr() const {
|
| 96 |
+
return boxed_kernel_func_;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
} // namespace c10
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/boxing/KernelFunction_impl.h
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <ATen/core/boxing/impl/boxing.h>
|
| 2 |
+
#include <ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h>
|
| 3 |
+
#include <ATen/core/boxing/impl/WrapFunctionIntoFunctor.h>
|
| 4 |
+
#include <ATen/core/boxing/impl/WrapFunctionIntoRuntimeFunctor.h>
|
| 5 |
+
|
| 6 |
+
#include <c10/util/C++17.h>
|
| 7 |
+
#include <type_traits>
|
| 8 |
+
|
| 9 |
+
namespace c10 {
|
| 10 |
+
|
| 11 |
+
inline KernelFunction::KernelFunction()
|
| 12 |
+
: boxed_kernel_func_()
|
| 13 |
+
, unboxed_kernel_func_(nullptr)
|
| 14 |
+
, sym_unboxed_kernel_func_(nullptr)
|
| 15 |
+
{}
|
| 16 |
+
|
| 17 |
+
inline KernelFunction::KernelFunction(std::unique_ptr<OperatorKernel> functor, InternalBoxedKernelFunction* boxed_kernel_func, void* unboxed_kernel_func, void* sym_unboxed_kernel_func = nullptr)
|
| 18 |
+
: boxed_kernel_func_(std::move(functor), boxed_kernel_func)
|
| 19 |
+
, unboxed_kernel_func_(unboxed_kernel_func)
|
| 20 |
+
, sym_unboxed_kernel_func_(sym_unboxed_kernel_func)
|
| 21 |
+
{}
|
| 22 |
+
|
| 23 |
+
inline KernelFunction::KernelFunction(BoxedKernel boxed_fn, void* unboxed_kernel_func, void* sym_unboxed_kernel_func = nullptr)
|
| 24 |
+
: boxed_kernel_func_(std::move(boxed_fn))
|
| 25 |
+
, unboxed_kernel_func_(unboxed_kernel_func)
|
| 26 |
+
, sym_unboxed_kernel_func_(sym_unboxed_kernel_func)
|
| 27 |
+
{}
|
| 28 |
+
|
| 29 |
+
inline bool KernelFunction::isValidUnboxed() const {
|
| 30 |
+
return unboxed_kernel_func_ != nullptr;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
inline bool KernelFunction::isValidSymUnboxed() const {
|
| 34 |
+
return sym_unboxed_kernel_func_ != nullptr;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
inline bool KernelFunction::isValid() const {
|
| 38 |
+
return boxed_kernel_func_.isValid();
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
inline bool KernelFunction::isFallthrough() const {
|
| 42 |
+
return boxed_kernel_func_.isFallthrough();
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
inline void KernelFunction::callBoxed(const OperatorHandle& opHandle, DispatchKeySet dispatchKeySet, Stack* stack) const {
|
| 46 |
+
boxed_kernel_func_.callBoxed(opHandle, dispatchKeySet, stack);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
template<class Return, class... Args>
|
| 50 |
+
inline Return callUnboxedKernelFunction(void* unboxed_kernel_func, OperatorKernel* functor, DispatchKeySet dispatchKeySet, Args&&... args) {
|
| 51 |
+
using ActualSignature = Return (OperatorKernel*, DispatchKeySet, Args...);
|
| 52 |
+
ActualSignature* func = reinterpret_cast<ActualSignature*>(unboxed_kernel_func);
|
| 53 |
+
return (*func)(functor, dispatchKeySet, std::forward<Args>(args)...);
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
// This template requires you to explicitly specify the argument you want to
|
| 57 |
+
// forward; it doesn't work if you try to deduce it
|
| 58 |
+
// NB: keep this in sync with cloneWithRealTypes in function_schema.cpp
|
| 59 |
+
|
| 60 |
+
template <typename T>
|
| 61 |
+
inline typename remove_symint<T>::type unpackSymInt(T x) { return x; }
|
| 62 |
+
|
| 63 |
+
template <>
|
| 64 |
+
inline typename remove_symint<c10::SymInt>::type unpackSymInt(c10::SymInt x) {
|
| 65 |
+
return x.guard_int(__FILE__, __LINE__);
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
template <>
|
| 69 |
+
inline typename remove_symint<c10::SymIntArrayRef>::type unpackSymInt(c10::SymIntArrayRef x) {
|
| 70 |
+
return C10_AS_INTARRAYREF_SLOW(x);
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
template <>
|
| 74 |
+
inline typename remove_symint<std::optional<c10::SymInt>>::type unpackSymInt(std::optional<c10::SymInt> x) {
|
| 75 |
+
return x.has_value() ? std::make_optional(x->guard_int(__FILE__, __LINE__)) : std::nullopt;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
template <>
|
| 79 |
+
inline typename remove_symint<at::OptionalSymIntArrayRef>::type unpackSymInt(at::OptionalSymIntArrayRef x) {
|
| 80 |
+
return x.has_value() ? std::make_optional(C10_AS_INTARRAYREF_SLOW(*x)) : std::nullopt;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
template<class Return, class... Args>
|
| 84 |
+
C10_ALWAYS_INLINE Return KernelFunction::call(const OperatorHandle& opHandle, DispatchKeySet dispatchKeySet, Args... args) const {
|
| 85 |
+
// note: Args above is intentionally not Args&&. We don't want perfect
|
| 86 |
+
// forwarding, which would require Args to be deduced, but instead we
|
| 87 |
+
// want callers to explicitly specify the Args.
|
| 88 |
+
|
| 89 |
+
if constexpr (std::disjunction_v<has_symint<Args>...>) {
|
| 90 |
+
if (sym_unboxed_kernel_func_ != nullptr) {
|
| 91 |
+
auto *functor = boxed_kernel_func_.getFunctor();
|
| 92 |
+
return callUnboxedKernelFunction<Return, Args...>(
|
| 93 |
+
sym_unboxed_kernel_func_, functor, dispatchKeySet, std::forward<Args>(args)...);
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
if (unboxed_kernel_func_ != nullptr) {
|
| 97 |
+
auto *functor = boxed_kernel_func_.getFunctor();
|
| 98 |
+
return callUnboxedKernelFunction<Return, typename remove_symint<Args>::type...>(
|
| 99 |
+
unboxed_kernel_func_, functor, dispatchKeySet, unpackSymInt<Args>(args)...);
|
| 100 |
+
}
|
| 101 |
+
} else {
|
| 102 |
+
if (C10_LIKELY(unboxed_kernel_func_ != nullptr)) {
|
| 103 |
+
auto *functor = boxed_kernel_func_.getFunctor();
|
| 104 |
+
return callUnboxedKernelFunction<Return, Args...>(
|
| 105 |
+
unboxed_kernel_func_, functor, dispatchKeySet, std::forward<Args>(args)...);
|
| 106 |
+
}
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
return impl::BoxedKernelWrapper<Return(Args...)>::call(
|
| 110 |
+
boxed_kernel_func_,
|
| 111 |
+
opHandle,
|
| 112 |
+
dispatchKeySet,
|
| 113 |
+
std::forward<Args>(args)...
|
| 114 |
+
);
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
inline KernelFunction KernelFunction::makeFromBoxedKernel(BoxedKernel boxed_fn) {
|
| 118 |
+
return KernelFunction(std::move(boxed_fn), nullptr); // no unboxed function pointer
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
template<KernelFunction::BoxedKernelFunction* func>
|
| 122 |
+
inline KernelFunction KernelFunction::makeFromBoxedFunction() {
|
| 123 |
+
return KernelFunction::makeFromBoxedKernel(
|
| 124 |
+
BoxedKernel::makeFromFunction<func>());
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
template<KernelFunction::BoxedKernelFunction_withDispatchKeys* func>
|
| 128 |
+
inline KernelFunction KernelFunction::makeFromBoxedFunction() {
|
| 129 |
+
return KernelFunction::makeFromBoxedKernel(
|
| 130 |
+
BoxedKernel::makeFromFunction<func>());
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
inline KernelFunction KernelFunction::makeFallthrough() {
|
| 134 |
+
return KernelFunction::makeFromBoxedKernel(
|
| 135 |
+
BoxedKernel::makeFallthrough());
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
inline KernelFunction KernelFunction::makeAmbiguousAutogradOther() {
|
| 139 |
+
return KernelFunction::makeFromBoxedKernel(
|
| 140 |
+
BoxedKernel::makeAmbiguousAutogradOther());
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
inline KernelFunction KernelFunction::makeNamedNotSupported() {
|
| 144 |
+
return KernelFunction::makeFromBoxedKernel(
|
| 145 |
+
BoxedKernel::makeNamedNotSupported());
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
template<bool AllowLegacyTypes, class KernelFunctor>
|
| 149 |
+
inline KernelFunction KernelFunction::makeFromUnboxedFunctor(std::unique_ptr<OperatorKernel> kernelFunctor) {
|
| 150 |
+
#ifndef NDEBUG
|
| 151 |
+
// This assertion is costly for build time so it's debug-gated.
|
| 152 |
+
static_assert(guts::is_functor<KernelFunctor>::value, "Tried to call KernelFunction::makeFromUnboxedFunctor<KernelFunctor> but the argument is not a functor.");
|
| 153 |
+
#endif
|
| 154 |
+
static_assert(std::is_base_of<OperatorKernel, KernelFunctor>::value, "Tried to call KernelFunction::makeFromUnboxedFunctor<KernelFunctor>, but the functor doesn't inherit from c10::OperatorKernel. Please have the functor inherit from it.");
|
| 155 |
+
|
| 156 |
+
auto* unboxed_fn = &impl::wrap_kernel_functor_unboxed<KernelFunctor>::call;
|
| 157 |
+
void* void_unboxed_fn = reinterpret_cast<void*>(unboxed_fn);
|
| 158 |
+
bool is_symint = fn_has_symint<decltype(unboxed_fn)>::value;
|
| 159 |
+
return KernelFunction(
|
| 160 |
+
std::move(kernelFunctor),
|
| 161 |
+
&impl::make_boxed_from_unboxed_functor<KernelFunctor, AllowLegacyTypes>::call,
|
| 162 |
+
is_symint ? nullptr : void_unboxed_fn,
|
| 163 |
+
is_symint ? void_unboxed_fn : nullptr
|
| 164 |
+
);
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
template<class KernelFunctor>
|
| 168 |
+
inline KernelFunction KernelFunction::makeFromBoxedFunctor(std::unique_ptr<KernelFunctor> kernelFunctor) {
|
| 169 |
+
return KernelFunction::makeFromBoxedKernel(
|
| 170 |
+
BoxedKernel::makeFromFunctor(std::move(kernelFunctor)));
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
template<class FuncPtr, bool AllowLegacyTypes>
|
| 174 |
+
inline KernelFunction KernelFunction::makeFromUnboxedFunction(FuncPtr func_ptr) {
|
| 175 |
+
static_assert(is_compile_time_function_pointer<FuncPtr>::value, "Tried to call KernelFunction::makeFromUnboxedFunction with an invalid parameter. It must be a function pointer created with TORCH_FN.");
|
| 176 |
+
static_assert(!std::is_same<typename FuncPtr::FuncType, BoxedKernelFunction>::value, "Tried to call KernelFunction::makeFromUnboxedFunction with a boxed function pointer. Please use KernelFunction::makeFromBoxedFunction instead.");
|
| 177 |
+
static_assert(FuncPtr::func_ptr() != nullptr, "Kernel function cannot be nullptr");
|
| 178 |
+
|
| 179 |
+
#if !defined(C10_MOBILE)
|
| 180 |
+
(void)func_ptr; // Suppress unused variable warning
|
| 181 |
+
return makeFromUnboxedFunctor<AllowLegacyTypes, typename impl::WrapFunctionIntoFunctor<FuncPtr>::type>(
|
| 182 |
+
guts::make_unique_base<OperatorKernel, typename impl::WrapFunctionIntoFunctor<FuncPtr>::type>()
|
| 183 |
+
);
|
| 184 |
+
#else
|
| 185 |
+
// On mobile, we rather want to optimize for binary size than for performance,
|
| 186 |
+
// so let's not inline the kernel into the wrapper but use makeFromUnboxedRuntimeFunction
|
| 187 |
+
// instead.
|
| 188 |
+
return makeFromUnboxedRuntimeFunction(func_ptr.func_ptr());
|
| 189 |
+
#endif
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
template<bool AllowLegacyTypes, class FuncType>
|
| 193 |
+
inline KernelFunction KernelFunction::makeFromUnboxedRuntimeFunction(FuncType* func) {
|
| 194 |
+
static_assert(guts::is_function_type<FuncType>::value, "Tried to call KernelFunction::makeFromUnboxedRuntimeFunction with a non-function type.");
|
| 195 |
+
static_assert(!std::is_same<FuncType, BoxedKernelFunction>::value, "Tried to call KernelFunction::makeFromUnboxedRuntimeFunction with a boxed function pointer. Please use KernelFunction::makeFromBoxedFunction instead.");
|
| 196 |
+
TORCH_INTERNAL_ASSERT(func != nullptr, "Kernel function cannot be nullptr");
|
| 197 |
+
|
| 198 |
+
return makeFromUnboxedFunctor<AllowLegacyTypes, impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<FuncType>>>(
|
| 199 |
+
guts::make_unique_base<OperatorKernel, impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<FuncType>>>(func)
|
| 200 |
+
);
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
template<bool AllowLegacyTypes, class Lambda>
|
| 204 |
+
inline std::enable_if_t<guts::is_stateless_lambda<std::decay_t<Lambda>>::value, KernelFunction> KernelFunction::makeFromUnboxedLambda(Lambda&& lambda) {
|
| 205 |
+
static_assert(guts::is_functor<std::decay_t<Lambda>>::value, "Tried to call KernelFunction::makeFromUnboxedLambda with a non-lambda type.");
|
| 206 |
+
|
| 207 |
+
#if !defined(C10_MOBILE)
|
| 208 |
+
return makeFromUnboxedFunctor<AllowLegacyTypes, impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<Lambda>>>(
|
| 209 |
+
guts::make_unique_base<OperatorKernel, impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<Lambda>>>(std::forward<Lambda>(lambda))
|
| 210 |
+
);
|
| 211 |
+
#else
|
| 212 |
+
// On mobile, we rather want to optimize for binary size than for performance,
|
| 213 |
+
// so let's not inline the kernel into the wrapper but use makeFromUnboxedRuntimeFunction
|
| 214 |
+
// instead.
|
| 215 |
+
using FuncType = typename guts::infer_function_traits_t<std::decay_t<Lambda>>::func_type;
|
| 216 |
+
return makeFromUnboxedRuntimeFunction<AllowLegacyTypes, FuncType>(lambda);
|
| 217 |
+
#endif
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
template<bool AllowLegacyTypes, class Lambda>
|
| 221 |
+
inline std::enable_if_t<!guts::is_stateless_lambda<std::decay_t<Lambda>>::value, KernelFunction> KernelFunction::makeFromUnboxedLambda(Lambda&& lambda) {
|
| 222 |
+
static_assert(guts::is_functor<std::decay_t<Lambda>>::value, "Tried to call KernelFunction::makeFromUnboxedLambda with a non-lambda type.");
|
| 223 |
+
|
| 224 |
+
return makeFromUnboxedFunctor<AllowLegacyTypes, impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<Lambda>>>(
|
| 225 |
+
guts::make_unique_base<OperatorKernel, impl::WrapFunctionIntoRuntimeFunctor<std::decay_t<Lambda>>>(std::forward<Lambda>(lambda))
|
| 226 |
+
);
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
}
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/boxing/impl/WrapFunctionIntoRuntimeFunctor.h
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <c10/util/TypeTraits.h>
|
| 4 |
+
|
| 5 |
+
namespace c10 {
|
| 6 |
+
|
| 7 |
+
namespace impl {
|
| 8 |
+
namespace detail {
|
| 9 |
+
template<class FuncType, class ReturnType, class ParameterList> class WrapFunctionIntoRuntimeFunctor_ {};
|
| 10 |
+
template<class FuncType, class ReturnType, class... Parameters>
|
| 11 |
+
class WrapFunctionIntoRuntimeFunctor_<FuncType, ReturnType, guts::typelist::typelist<Parameters...>> final : public c10::OperatorKernel {
|
| 12 |
+
public:
|
| 13 |
+
template<class FuncType_>
|
| 14 |
+
explicit WrapFunctionIntoRuntimeFunctor_(FuncType_&& kernel_func)
|
| 15 |
+
: kernel_func_(std::forward<FuncType_>(kernel_func)) {}
|
| 16 |
+
|
| 17 |
+
decltype(auto) operator()(Parameters... args) {
|
| 18 |
+
return kernel_func_(std::forward<Parameters>(args)...);
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
private:
|
| 22 |
+
FuncType kernel_func_;
|
| 23 |
+
};
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// WrapFunctionIntoRuntimeFunctor: Wraps any runtime functor into a functor that
|
| 27 |
+
// inherits from c10::OperatorKernel, so it can be used as a c10 kernel.
|
| 28 |
+
// This can, for example, be used for lambdas, functors or even function pointers.
|
| 29 |
+
// In the case of function pointers, since it is a runtime function pointer,
|
| 30 |
+
// there is an overhead for calling it whenever the kernel is invoked.
|
| 31 |
+
template<class FuncType>
|
| 32 |
+
using WrapFunctionIntoRuntimeFunctor = detail::WrapFunctionIntoRuntimeFunctor_<
|
| 33 |
+
FuncType,
|
| 34 |
+
typename guts::infer_function_traits_t<FuncType>::return_type,
|
| 35 |
+
typename guts::infer_function_traits_t<FuncType>::parameter_types
|
| 36 |
+
>;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
}
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/CppSignature.h
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <typeindex>
|
| 4 |
+
#include <c10/core/DispatchKeySet.h>
|
| 5 |
+
#include <c10/macros/Macros.h>
|
| 6 |
+
#include <c10/util/Metaprogramming.h>
|
| 7 |
+
#include <c10/util/Type.h>
|
| 8 |
+
|
| 9 |
+
namespace c10 {
|
| 10 |
+
namespace impl {
|
| 11 |
+
|
| 12 |
+
// A CppSignature object holds RTTI information about a C++ function signature at runtime
|
| 13 |
+
// and can compare them or get a debug-printable name.
|
| 14 |
+
class TORCH_API CppSignature final {
|
| 15 |
+
public:
|
| 16 |
+
CppSignature(const CppSignature&) = default;
|
| 17 |
+
CppSignature(CppSignature&&) noexcept = default;
|
| 18 |
+
CppSignature& operator=(const CppSignature&) = default;
|
| 19 |
+
CppSignature& operator=(CppSignature&&) noexcept = default;
|
| 20 |
+
|
| 21 |
+
template<class FuncType>
|
| 22 |
+
static CppSignature make() {
|
| 23 |
+
// Normalize functors, lambdas, function pointers, etc. into the plain function type
|
| 24 |
+
// The first argument of the schema might be of type DispatchKeySet, in which case we remove it.
|
| 25 |
+
// We do this to guarantee that all CppSignature's for an operator will match, even if they're registered
|
| 26 |
+
// with different calling conventions.
|
| 27 |
+
// See Note [Plumbing Keys Through The Dispatcher]
|
| 28 |
+
using decayed_function_type = typename c10::remove_DispatchKeySet_arg_from_func<std::decay_t<FuncType>>::func_type;
|
| 29 |
+
|
| 30 |
+
return CppSignature(std::type_index(typeid(decayed_function_type)));
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
std::string name() const {
|
| 34 |
+
return c10::demangle(signature_.name());
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
friend bool operator==(const CppSignature& lhs, const CppSignature& rhs) {
|
| 38 |
+
if (lhs.signature_ == rhs.signature_) {
|
| 39 |
+
return true;
|
| 40 |
+
}
|
| 41 |
+
// Without RTLD_GLOBAL, the type_index comparison could yield false because
|
| 42 |
+
// they point to different instances of the RTTI data, but the types would
|
| 43 |
+
// still be the same. Let's check for that case too.
|
| 44 |
+
// Note that there still is a case where this might not work, i.e. when
|
| 45 |
+
// linking libraries of different compilers together, they might have
|
| 46 |
+
// different ways to serialize a type name. That, together with a missing
|
| 47 |
+
// RTLD_GLOBAL, would still fail this.
|
| 48 |
+
if (0 == strcmp(lhs.signature_.name(), rhs.signature_.name())) {
|
| 49 |
+
return true;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
return false;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
private:
|
| 56 |
+
explicit CppSignature(std::type_index signature): signature_(std::move(signature)) {}
|
| 57 |
+
std::type_index signature_;
|
| 58 |
+
};
|
| 59 |
+
|
| 60 |
+
inline bool operator!=(const CppSignature& lhs, const CppSignature& rhs) {
|
| 61 |
+
return !(lhs == rhs );
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
}
|
| 65 |
+
}
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/DispatchKeyExtractor.h
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cstdint>
|
| 4 |
+
#include <ATen/core/function_schema.h>
|
| 5 |
+
#include <ATen/core/jit_type.h>
|
| 6 |
+
#include <c10/util/Bitset.h>
|
| 7 |
+
#include <c10/core/DispatchKeySet.h>
|
| 8 |
+
#include <c10/util/irange.h>
|
| 9 |
+
#include <ATen/core/Variadic.h>
|
| 10 |
+
#include <ATen/core/stack.h>
|
| 11 |
+
|
| 12 |
+
namespace c10 {
|
| 13 |
+
|
| 14 |
+
namespace impl {
|
| 15 |
+
|
| 16 |
+
// Take a DispatchKeySet for a Tensor and determine what the actual dispatch
|
| 17 |
+
// DispatchKey should be, taking into account TLS, and skipping backends which
|
| 18 |
+
// fall through.
|
| 19 |
+
//
|
| 20 |
+
// Unlike Tensor::key_set(), the value of this on a tensor can change depending
|
| 21 |
+
// on TLS.
|
| 22 |
+
//
|
| 23 |
+
// NB: If there is no valid dispatch key, this will return Undefined
|
| 24 |
+
inline DispatchKeySet computeDispatchKeySet(
|
| 25 |
+
DispatchKeySet ks,
|
| 26 |
+
// The key mask lets us eliminate (by zero entries) keys which should not
|
| 27 |
+
// be considered for dispatch. There are two cases when we use this:
|
| 28 |
+
//
|
| 29 |
+
// - If an operator's dispatch table contains a fallthrough entry, we
|
| 30 |
+
// should bypass it entirely when finding the key
|
| 31 |
+
// - If a user invokes with redispatch, the mask lets us
|
| 32 |
+
// zero out the key the user asked us to stop.
|
| 33 |
+
//
|
| 34 |
+
// These excluded backends are NOT tracked in the TLS, but must be applied
|
| 35 |
+
// AFTER TLS (since the backend may have been introduced for consideration
|
| 36 |
+
// by the included TLS), which is why you have to pass them in to this
|
| 37 |
+
// function (as opposed to just applying it to the input 'ks').
|
| 38 |
+
DispatchKeySet key_mask
|
| 39 |
+
) {
|
| 40 |
+
c10::impl::LocalDispatchKeySet local = c10::impl::tls_local_dispatch_key_set();
|
| 41 |
+
// TODO: It's a bit irritating that we have to do logical ORs here, it would
|
| 42 |
+
// be nice to only do one. Can always_included be folded into the TLS? Well,
|
| 43 |
+
// it's a bit troublesome, because fastpath TLS access requires the type of
|
| 44 |
+
// the TLS in question to be zero-initialized, so you don't actually win
|
| 45 |
+
// anything in that case.
|
| 46 |
+
return (((ks | local.included_) - local.excluded_) & key_mask);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
namespace detail {
|
| 52 |
+
// A small gadget to extract the DispatchKeySet from types which are known
|
| 53 |
+
// to have it. Used to extract dispatch keys from unboxed calls.
|
| 54 |
+
struct MultiDispatchKeySet : at::IterArgs<MultiDispatchKeySet> {
|
| 55 |
+
DispatchKeySet ts;
|
| 56 |
+
void operator()(const at::Tensor& x) {
|
| 57 |
+
ts = ts | x.key_set();
|
| 58 |
+
}
|
| 59 |
+
void operator()(const std::optional<at::Tensor>& x) {
|
| 60 |
+
if (x.has_value()) {
|
| 61 |
+
ts = ts | x->key_set();
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
void operator()(at::ArrayRef<at::Tensor> xs) {
|
| 65 |
+
for (const auto& x : xs) {
|
| 66 |
+
ts = ts | x.key_set();
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
// Tensor?[] translates to this case.
|
| 70 |
+
void operator()(const c10::List<std::optional<at::Tensor>>& xs) {
|
| 71 |
+
for (std::optional<at::Tensor> x : xs) {
|
| 72 |
+
if (x.has_value()) {
|
| 73 |
+
ts = ts | x.value().key_set();
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
// Structured Tensor[] translates to this case
|
| 78 |
+
void operator()(const at::ITensorListRef& xs) {
|
| 79 |
+
for (const auto& x : xs) {
|
| 80 |
+
ts = ts | x.key_set();
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
[[noreturn]] void operator()(at::ArrayRef<std::optional<at::Tensor>>) {
|
| 84 |
+
// Just checking that the handling of Tensor?[] didn't change.
|
| 85 |
+
TORCH_INTERNAL_ASSERT(false);
|
| 86 |
+
}
|
| 87 |
+
void operator()(const at::Generator& gen) {
|
| 88 |
+
if (gen.defined()) {
|
| 89 |
+
ts = ts | gen.key_set();
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
void operator()(const std::optional<at::Generator>& gen) {
|
| 93 |
+
if (gen.has_value() && gen->defined()) {
|
| 94 |
+
ts = ts | gen->key_set();
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
template <typename T>
|
| 98 |
+
void operator()(const T&) {
|
| 99 |
+
// do nothing
|
| 100 |
+
}
|
| 101 |
+
};
|
| 102 |
+
|
| 103 |
+
// NB: take by const reference (Don't do universal forwarding here! You
|
| 104 |
+
// don't want to move into this function!)
|
| 105 |
+
template <typename... Args>
|
| 106 |
+
DispatchKeySet multi_dispatch_key_set(const Args&... args) {
|
| 107 |
+
return MultiDispatchKeySet().apply(args...).ts;
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
/**
|
| 112 |
+
* An instance of DispatchKeyExtractor knows how to get a dispatch key given
|
| 113 |
+
* a list of arguments for an operator call.
|
| 114 |
+
*
|
| 115 |
+
* The instance is specific for a certain operator as:
|
| 116 |
+
* - In boxed dispatch, different operators have different ways to extract
|
| 117 |
+
* the dispatch key (e.g. different numbers of arguments), and we precompute
|
| 118 |
+
* the stack locations we should look at; and
|
| 119 |
+
* - In all dispatch, some backends should be excluded from dispatch because
|
| 120 |
+
* they have been registered as fallthrough. The set of excluded backends
|
| 121 |
+
* varies from operator, as some operators may have overridden the
|
| 122 |
+
* fallthrough with custom behavior.
|
| 123 |
+
*
|
| 124 |
+
* Note - this should maintain identical impl to the py dispatcher key extraction logic
|
| 125 |
+
* at pytorch/torch/dispatcher.py
|
| 126 |
+
*/
|
| 127 |
+
struct TORCH_API DispatchKeyExtractor final {
|
| 128 |
+
public:
|
| 129 |
+
static DispatchKeyExtractor make(const FunctionSchema& schema) {
|
| 130 |
+
return DispatchKeyExtractor(makeBitsetForDispatchArgs(schema));
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
static DispatchKeyExtractor makeUninitialized() {
|
| 134 |
+
return DispatchKeyExtractor(c10::utils::bitset());
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
void registerSchema(const FunctionSchema& schema) {
|
| 138 |
+
TORCH_INTERNAL_ASSERT(dispatch_arg_indices_reverse_.is_entirely_unset());
|
| 139 |
+
dispatch_arg_indices_reverse_ = makeBitsetForDispatchArgs(schema);
|
| 140 |
+
}
|
| 141 |
+
void deregisterSchema() {
|
| 142 |
+
dispatch_arg_indices_reverse_ = c10::utils::bitset();
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
DispatchKeySet getDispatchKeySetBoxed(const torch::jit::Stack* stack) const {
|
| 146 |
+
DispatchKeySet ks;
|
| 147 |
+
dispatch_arg_indices_reverse_.for_each_set_bit([&] (size_t reverse_arg_index) {
|
| 148 |
+
const auto& ivalue = torch::jit::peek(*stack, 0, reverse_arg_index + 1);
|
| 149 |
+
if (C10_LIKELY(ivalue.isTensor())) {
|
| 150 |
+
// NB: Take care not to introduce a refcount bump (there's
|
| 151 |
+
// no safe toTensorRef method, alas)
|
| 152 |
+
ks = ks | ivalue.unsafeToTensorImpl()->key_set();
|
| 153 |
+
} else if (C10_UNLIKELY(ivalue.isTensorList())) {
|
| 154 |
+
for (const at::Tensor& tensor : ivalue.toTensorList()) {
|
| 155 |
+
ks = ks | tensor.key_set();
|
| 156 |
+
}
|
| 157 |
+
}
|
| 158 |
+
// Tensor?[] translates to a c10::List<IValue> so we need to peek inside
|
| 159 |
+
else if (C10_UNLIKELY(ivalue.isList())) {
|
| 160 |
+
for (const auto& elt : ivalue.toListRef()) {
|
| 161 |
+
if (elt.isTensor()) {
|
| 162 |
+
ks = ks | elt.toTensor().key_set();
|
| 163 |
+
}
|
| 164 |
+
}
|
| 165 |
+
}
|
| 166 |
+
});
|
| 167 |
+
// Keys that are fallthrough should be skipped
|
| 168 |
+
if (requiresBitsetPerBackend_) {
|
| 169 |
+
auto backend_idx = ks.getBackendIndex();
|
| 170 |
+
return impl::computeDispatchKeySet(ks, nonFallthroughKeysPerBackend_[backend_idx]);
|
| 171 |
+
} else {
|
| 172 |
+
return impl::computeDispatchKeySet(ks, nonFallthroughKeys_);
|
| 173 |
+
}
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
template<class... Args>
|
| 177 |
+
DispatchKeySet getDispatchKeySetUnboxed(const Args&... args) const {
|
| 178 |
+
auto ks = detail::multi_dispatch_key_set(args...);
|
| 179 |
+
// Keys that are fallthrough should be skipped
|
| 180 |
+
if (requiresBitsetPerBackend_) {
|
| 181 |
+
auto backend_idx = ks.getBackendIndex();
|
| 182 |
+
return impl::computeDispatchKeySet(ks, nonFallthroughKeysPerBackend_[backend_idx]);
|
| 183 |
+
} else {
|
| 184 |
+
return impl::computeDispatchKeySet(ks, nonFallthroughKeys_);
|
| 185 |
+
}
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
void setOperatorHasFallthroughForKey(DispatchKey k, bool has_fallthrough);
|
| 189 |
+
|
| 190 |
+
std::string dumpState() const;
|
| 191 |
+
void checkInvariants(const FunctionSchema& schema) const;
|
| 192 |
+
|
| 193 |
+
private:
|
| 194 |
+
static c10::utils::bitset makeBitsetForDispatchArgs(const FunctionSchema& schema) {
|
| 195 |
+
TORCH_CHECK(schema.arguments().size() <= c10::utils::bitset::NUM_BITS(),
|
| 196 |
+
"The function schema has ", schema.arguments().size(),
|
| 197 |
+
" arguments but this PyTorch build only supports ", c10::utils::bitset::NUM_BITS());
|
| 198 |
+
c10::utils::bitset dispatch_arg_indices_reverse;
|
| 199 |
+
for (const auto index : c10::irange(schema.arguments().size())) {
|
| 200 |
+
if (schema.arguments()[index].type()->isSubtypeOf(*TensorType::get()) ||
|
| 201 |
+
schema.arguments()[index].type()->isSubtypeOf(
|
| 202 |
+
*ListType::ofTensors()) ||
|
| 203 |
+
schema.arguments()[index].type()->isSubtypeOf(
|
| 204 |
+
*ListType::ofOptionalTensors()) ||
|
| 205 |
+
schema.arguments()[index].type()->isSubtypeOf(
|
| 206 |
+
*OptionalType::ofTensor())) {
|
| 207 |
+
dispatch_arg_indices_reverse.set(schema.arguments().size() - 1 - index);
|
| 208 |
+
}
|
| 209 |
+
}
|
| 210 |
+
return dispatch_arg_indices_reverse;
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
explicit DispatchKeyExtractor(c10::utils::bitset dispatch_arg_indices_reverse)
|
| 214 |
+
: dispatch_arg_indices_reverse_(dispatch_arg_indices_reverse)
|
| 215 |
+
, nonFallthroughKeys_(DispatchKeySet::FULL)
|
| 216 |
+
, requiresBitsetPerBackend_(false) {
|
| 217 |
+
for (const auto i : c10::irange(nonFallthroughKeysPerBackend_.size())) {
|
| 218 |
+
nonFallthroughKeysPerBackend_[i] = DispatchKeySet::FULL;
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
// this is a bitset that has ones for each argument index which has to be
|
| 223 |
+
// considered for dispatch. This avoids having to iterate over the stack
|
| 224 |
+
// to find all the tensors. The bits are stored in reverse order, i.e.
|
| 225 |
+
// dispatch_arg_indices_reverse_[i] == true, then the i-th argument from
|
| 226 |
+
// the top of the stack (i.e. the i-th last argument of the function)
|
| 227 |
+
// is relevant for dispatch.
|
| 228 |
+
// dispatch_arg_indices_reverse_ is allowed to have zero bits set; that just means you must do the
|
| 229 |
+
// fallthrough
|
| 230 |
+
c10::utils::bitset dispatch_arg_indices_reverse_;
|
| 231 |
+
|
| 232 |
+
// Set of functionality keys for which the operator does NOT have fallthrough kernel.
|
| 233 |
+
DispatchKeySet nonFallthroughKeys_;
|
| 234 |
+
// Set of functionality keys for which the operator does NOT have fallthrough kernel, defined PER BACKEND.
|
| 235 |
+
// This is only needed if we know that the operator has a different set of fallthroughs defined for some backends.
|
| 236 |
+
std::array<DispatchKeySet, num_backends> nonFallthroughKeysPerBackend_;
|
| 237 |
+
// Flag to tell us if we can use the single set of nonFallthroughKeys_ (fast path),
|
| 238 |
+
// or if we need to fall back to the slower path and check nonFallthroughKeysPerBackend_
|
| 239 |
+
bool requiresBitsetPerBackend_;
|
| 240 |
+
};
|
| 241 |
+
|
| 242 |
+
}
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/Dispatcher.h
ADDED
|
@@ -0,0 +1,793 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/SequenceNumber.h>
|
| 4 |
+
#include <ATen/core/boxing/KernelFunction.h>
|
| 5 |
+
#include <ATen/core/boxing/impl/boxing.h>
|
| 6 |
+
#include <ATen/core/dispatch/OperatorEntry.h>
|
| 7 |
+
#include <ATen/core/dispatch/CppSignature.h>
|
| 8 |
+
#include <ATen/core/dispatch/RegistrationHandleRAII.h>
|
| 9 |
+
#include <ATen/record_function.h>
|
| 10 |
+
#include <c10/util/Exception.h>
|
| 11 |
+
#include <c10/util/LeftRight.h>
|
| 12 |
+
#include <list>
|
| 13 |
+
#include <mutex>
|
| 14 |
+
#include <condition_variable>
|
| 15 |
+
#include <type_traits>
|
| 16 |
+
#include <c10/core/SafePyObject.h>
|
| 17 |
+
|
| 18 |
+
#include <ATen/core/grad_mode.h>
|
| 19 |
+
#include <ATen/core/enum_tag.h>
|
| 20 |
+
|
| 21 |
+
#ifndef NDEBUG
|
| 22 |
+
#include <iostream>
|
| 23 |
+
#endif
|
| 24 |
+
|
| 25 |
+
namespace c10 {
|
| 26 |
+
|
| 27 |
+
TORCH_API bool show_dispatch_trace();
|
| 28 |
+
TORCH_API void dispatch_trace_nesting_incr();
|
| 29 |
+
TORCH_API void dispatch_trace_nesting_decr();
|
| 30 |
+
TORCH_API int64_t dispatch_trace_nesting_value();
|
| 31 |
+
|
| 32 |
+
struct DispatchTraceNestingGuard {
|
| 33 |
+
DispatchTraceNestingGuard() { dispatch_trace_nesting_incr(); }
|
| 34 |
+
~DispatchTraceNestingGuard() { dispatch_trace_nesting_decr(); }
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
class TORCH_API OperatorHandle;
|
| 38 |
+
template<class FuncType> class TypedOperatorHandle;
|
| 39 |
+
|
| 40 |
+
/**
|
| 41 |
+
* Implement this interface and register your instance with the dispatcher
|
| 42 |
+
* to get notified when operators are registered or deregistered with
|
| 43 |
+
* the dispatcher.
|
| 44 |
+
*
|
| 45 |
+
* NB: registration events only occur when a 'def' occurs; we don't trigger
|
| 46 |
+
* on 'impl' or 'fallback' calls.
|
| 47 |
+
*/
|
| 48 |
+
class TORCH_API OpRegistrationListener {
|
| 49 |
+
public:
|
| 50 |
+
virtual ~OpRegistrationListener();
|
| 51 |
+
|
| 52 |
+
virtual void onOperatorRegistered(const OperatorHandle& op) = 0;
|
| 53 |
+
virtual void onOperatorDeregistered(const OperatorHandle& op) = 0;
|
| 54 |
+
};
|
| 55 |
+
|
| 56 |
+
namespace detail {
|
| 57 |
+
class RegistrationListenerList;
|
| 58 |
+
}
|
| 59 |
+
class SchemaRegistrationHandleRAII;
|
| 60 |
+
|
| 61 |
+
/**
|
| 62 |
+
* Top-level dispatch interface for dispatching via the dynamic dispatcher.
|
| 63 |
+
* Most end users shouldn't use this directly; if you're trying to register
|
| 64 |
+
* ops look in op_registration
|
| 65 |
+
*/
|
| 66 |
+
class TORCH_API Dispatcher final {
|
| 67 |
+
private:
|
| 68 |
+
// For direct access to backend fallback information
|
| 69 |
+
friend class impl::OperatorEntry;
|
| 70 |
+
|
| 71 |
+
struct OperatorDef final {
|
| 72 |
+
explicit OperatorDef(OperatorName&& op_name)
|
| 73 |
+
: op(std::move(op_name)) {}
|
| 74 |
+
|
| 75 |
+
impl::OperatorEntry op;
|
| 76 |
+
|
| 77 |
+
// These refer to the number of outstanding RegistrationHandleRAII
|
| 78 |
+
// for this operator. def_count reflects only def() registrations
|
| 79 |
+
// (in the new world, this should only ever be 1, but old style
|
| 80 |
+
// registrations may register the schema multiple times, which
|
| 81 |
+
// will increase this count). def_and_impl_count reflects the number
|
| 82 |
+
// of combined def() and impl() registrations. When the last def() gets
|
| 83 |
+
// unregistered, we must immediately call the Deregistered listeners, but we
|
| 84 |
+
// must not actually delete the handle as there are other outstanding RAII
|
| 85 |
+
// destructors which will try to destruct and they had better still have a
|
| 86 |
+
// working operator handle in this case
|
| 87 |
+
size_t def_count = 0;
|
| 88 |
+
size_t def_and_impl_count = 0;
|
| 89 |
+
};
|
| 90 |
+
friend class OperatorHandle;
|
| 91 |
+
template<class> friend class TypedOperatorHandle;
|
| 92 |
+
|
| 93 |
+
struct Guard final {
|
| 94 |
+
Guard() : alive(true), mutex() {}
|
| 95 |
+
std::atomic<bool> alive;
|
| 96 |
+
std::mutex mutex;
|
| 97 |
+
};
|
| 98 |
+
|
| 99 |
+
public:
|
| 100 |
+
~Dispatcher();
|
| 101 |
+
|
| 102 |
+
// Implementation note: this class abstracts over the fact that we have per-operator
|
| 103 |
+
// dispatch tables. This could be easily adjusted to have a single global hash
|
| 104 |
+
// table.
|
| 105 |
+
static Dispatcher& realSingleton();
|
| 106 |
+
|
| 107 |
+
C10_ALWAYS_INLINE static Dispatcher& singleton() {
|
| 108 |
+
#if !defined C10_MOBILE
|
| 109 |
+
// Implemented inline so that steady-state code needn't incur
|
| 110 |
+
// function-call overhead. We can't just inline `realSingleton`
|
| 111 |
+
// because the function-local static would get duplicated across
|
| 112 |
+
// all DSOs that include & use this header, leading to multiple
|
| 113 |
+
// singleton instances.
|
| 114 |
+
static Dispatcher& s = realSingleton();
|
| 115 |
+
return s;
|
| 116 |
+
#else
|
| 117 |
+
// For C10_MOBILE, we should never inline a static function that
|
| 118 |
+
// has a static member, since the generated code calls
|
| 119 |
+
// __cxa_guard_acquire and __cxa_guard_release which help
|
| 120 |
+
// implement exactly once semantics for the initialization of the
|
| 121 |
+
// static Dispatcher& s above (for the non-mobile case). That
|
| 122 |
+
// additional code when duplicated across all operator stubs
|
| 123 |
+
// for every backend results in a lot of additional code
|
| 124 |
+
// being generated by the compiler.
|
| 125 |
+
return realSingleton();
|
| 126 |
+
#endif
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
// ------------------------------------------------------------------------
|
| 130 |
+
//
|
| 131 |
+
// Accessing operators by schema
|
| 132 |
+
//
|
| 133 |
+
// ------------------------------------------------------------------------
|
| 134 |
+
|
| 135 |
+
/**
|
| 136 |
+
* Looks for an operator schema with the given name and overload name
|
| 137 |
+
* and returns it if it is registered WITH A SCHEMA.
|
| 138 |
+
* Returns nullopt otherwise.
|
| 139 |
+
*/
|
| 140 |
+
std::optional<OperatorHandle> findSchema(const OperatorName& operator_name);
|
| 141 |
+
|
| 142 |
+
/**
|
| 143 |
+
* Variant of findSchema that results in less code generated at the call site.
|
| 144 |
+
* It (1) takes const char* pointer rather than OperatorName (so we skip
|
| 145 |
+
* generating std::string constructor calls at the call site), and (2)
|
| 146 |
+
* it raises an exception if the operator is not found (so we skip
|
| 147 |
+
* generating exception raising code at the call site)
|
| 148 |
+
*
|
| 149 |
+
* Irritatingly, we still have to generate the handful of instructions
|
| 150 |
+
* for dealing with an exception being thrown during static initialization
|
| 151 |
+
* (e.g. __cxa_guard_abort). If we could annotate this method noexcept we
|
| 152 |
+
* could avoid this code too, but as the name of the function suggests,
|
| 153 |
+
* it does throw exceptions.
|
| 154 |
+
*/
|
| 155 |
+
OperatorHandle findSchemaOrThrow(const char* name, const char* overload_name);
|
| 156 |
+
|
| 157 |
+
// Like findSchema, but also returns OperatorHandle even if there is no schema
|
| 158 |
+
std::optional<OperatorHandle> findOp(const OperatorName& operator_name);
|
| 159 |
+
|
| 160 |
+
// Returns a list of all operator names present in the operatorLookupTable_
|
| 161 |
+
const std::vector<OperatorName> getAllOpNames();
|
| 162 |
+
|
| 163 |
+
// ------------------------------------------------------------------------
|
| 164 |
+
//
|
| 165 |
+
// Invoking operators
|
| 166 |
+
//
|
| 167 |
+
// ------------------------------------------------------------------------
|
| 168 |
+
|
| 169 |
+
template<class Return, class... Args>
|
| 170 |
+
Return call(const TypedOperatorHandle<Return (Args...)>& op, Args... args) const;
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
template<class Return, class... Args>
|
| 174 |
+
static Return callWithDispatchKeySlowPath(const TypedOperatorHandle<Return (Args...)>& op, at::StepCallbacks& stepCallbacks, DispatchKeySet dispatchKeySet, const KernelFunction& kernel, Args... args);
|
| 175 |
+
|
| 176 |
+
// Like call, but intended for use in a redispatch in kernels that have explicitly performed the DispatchKey update calculatulation.
|
| 177 |
+
// This will take the DispatchKeySet completely as is and dispatch to the kernel of the corresponding highest priority key in the set.
|
| 178 |
+
// Note that this version of redispatch treats the inputted DispatchKeySet *as is*, and does NOT mask out the highest priority key.
|
| 179 |
+
// See Note [Plumbing Keys Through The Dispatcher]
|
| 180 |
+
template<class Return, class... Args>
|
| 181 |
+
Return redispatch(const TypedOperatorHandle<Return (Args...)>& op, DispatchKeySet currentDispatchKeySet, Args... args) const;
|
| 182 |
+
|
| 183 |
+
// Invoke an operator via the boxed calling convention using an IValue stack
|
| 184 |
+
void callBoxed(const OperatorHandle& op, Stack* stack) const;
|
| 185 |
+
void callBoxedForDispatchKey(const OperatorHandle& op, DispatchKey dk, Stack* stack) const;
|
| 186 |
+
|
| 187 |
+
// TODO: This will only be useful if we write a backend fallback that plumbs dispatch keys (currently there are none)
|
| 188 |
+
// See Note [Plumbing Keys Through The Dispatcher]
|
| 189 |
+
void redispatchBoxed(const OperatorHandle& op, DispatchKeySet dispatchKeySet, Stack* stack) const;
|
| 190 |
+
|
| 191 |
+
bool hasBackendFallbackForDispatchKey(DispatchKey dk) {
|
| 192 |
+
auto dispatch_ix = getDispatchTableIndexForDispatchKey(dk);
|
| 193 |
+
if (dispatch_ix < 0) return false;
|
| 194 |
+
return backendFallbackKernels_[dispatch_ix].kernel.isValid();
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
// Used by torchdeploy/multipy for multiple interpreters racing.
|
| 198 |
+
void waitForDef(const FunctionSchema& schema);
|
| 199 |
+
void waitForImpl(const OperatorName& op_name, std::optional<DispatchKey> dispatch_key);
|
| 200 |
+
|
| 201 |
+
// ------------------------------------------------------------------------
|
| 202 |
+
//
|
| 203 |
+
// Performing registrations (NON user public; use op_registration)
|
| 204 |
+
//
|
| 205 |
+
// ------------------------------------------------------------------------
|
| 206 |
+
|
| 207 |
+
/**
|
| 208 |
+
* Register a new operator schema.
|
| 209 |
+
*
|
| 210 |
+
* If a schema with the same operator name and overload name already exists,
|
| 211 |
+
* this function will check that both schemas are exactly identical.
|
| 212 |
+
*/
|
| 213 |
+
RegistrationHandleRAII registerDef(FunctionSchema schema, std::string debug, std::vector<at::Tag> tags = {});
|
| 214 |
+
|
| 215 |
+
/**
|
| 216 |
+
* Register a kernel to the dispatch table for an operator.
|
| 217 |
+
* If dispatch_key is nullopt, then this registers a fallback kernel.
|
| 218 |
+
*
|
| 219 |
+
* @return A RAII object that manages the lifetime of the registration.
|
| 220 |
+
* Once that object is destructed, the kernel will be deregistered.
|
| 221 |
+
*/
|
| 222 |
+
// NB: steals the inferred function schema, as we may need to hold on to
|
| 223 |
+
// it for a bit until the real schema turns up
|
| 224 |
+
RegistrationHandleRAII registerImpl(OperatorName op_name, std::optional<DispatchKey> dispatch_key, KernelFunction kernel, std::optional<impl::CppSignature> cpp_signature, std::unique_ptr<FunctionSchema> inferred_function_schema, std::string debug);
|
| 225 |
+
|
| 226 |
+
/**
|
| 227 |
+
* Given an operator, tells the Dispatcher that we have implemented a fake impl
|
| 228 |
+
* for this op in the given Python module. Call this a "pystub".
|
| 229 |
+
*/
|
| 230 |
+
RegistrationHandleRAII registerPythonModule(const OperatorName& op_name, const char* pymodule, const char* context);
|
| 231 |
+
|
| 232 |
+
/**
|
| 233 |
+
* Given an operator, throws if we have a pystub.
|
| 234 |
+
*/
|
| 235 |
+
void throwIfHasPythonModule(OperatorName op_name);
|
| 236 |
+
|
| 237 |
+
std::optional<std::pair<const char*, const char*>> getPyStub(OperatorName op_name);
|
| 238 |
+
|
| 239 |
+
/**
|
| 240 |
+
* Register a new operator by name.
|
| 241 |
+
*/
|
| 242 |
+
RegistrationHandleRAII registerName(OperatorName op_name);
|
| 243 |
+
|
| 244 |
+
/**
|
| 245 |
+
* Register a fallback kernel for a backend.
|
| 246 |
+
* If an operator is called but there is no concrete kernel for the dispatch
|
| 247 |
+
* key of the given operator arguments, it will check if there is such a
|
| 248 |
+
* fallback kernel for the given dispatch key and, if yes, call that one.
|
| 249 |
+
*/
|
| 250 |
+
RegistrationHandleRAII registerFallback(DispatchKey dispatch_key, KernelFunction kernel, std::string debug);
|
| 251 |
+
|
| 252 |
+
/**
|
| 253 |
+
* Use to register whenever we had a TORCH_LIBRARY declaration in the frontend
|
| 254 |
+
* API. These invocations are only permitted once per program, so we raise
|
| 255 |
+
* an error if this is called again for the same namespace.
|
| 256 |
+
*/
|
| 257 |
+
RegistrationHandleRAII registerLibrary(std::string ns, std::string debug);
|
| 258 |
+
|
| 259 |
+
// ------------------------------------------------------------------------
|
| 260 |
+
//
|
| 261 |
+
// Listeners on registrations
|
| 262 |
+
//
|
| 263 |
+
// ------------------------------------------------------------------------
|
| 264 |
+
|
| 265 |
+
/**
|
| 266 |
+
* Add a listener that gets called whenever a new op is registered or an existing
|
| 267 |
+
* op is deregistered. Immediately after registering, this listener gets called
|
| 268 |
+
* for all previously registered ops, so it can be used to keep track of ops
|
| 269 |
+
* registered with this dispatcher.
|
| 270 |
+
*/
|
| 271 |
+
RegistrationHandleRAII addRegistrationListener(std::unique_ptr<OpRegistrationListener> listener);
|
| 272 |
+
|
| 273 |
+
void checkInvariants() const;
|
| 274 |
+
|
| 275 |
+
//
|
| 276 |
+
// ------------------------------------------------------------------------
|
| 277 |
+
//
|
| 278 |
+
// Assertions
|
| 279 |
+
//
|
| 280 |
+
// ------------------------------------------------------------------------
|
| 281 |
+
|
| 282 |
+
/**
|
| 283 |
+
* For testing purposes.
|
| 284 |
+
* Returns a list of all operators that were created through calls to registerImpl(),
|
| 285 |
+
* without any corresponding calls to registerDef(). After static initialization
|
| 286 |
+
* is done this is almost certainly a bug, as the created OperatorHandle won't have
|
| 287 |
+
* any schema associated with it and users calling the op through the dispatcher
|
| 288 |
+
* won't be able to access it
|
| 289 |
+
*
|
| 290 |
+
* Note that we cannot enforce this invariant "as we go" during static initialization,
|
| 291 |
+
* due to undefined static initialization order- we have no guarantees over the order
|
| 292 |
+
* in which .def() and .impl() calls are registered in the dispatcher at static
|
| 293 |
+
* initialization time. So this function should only be called after static initialization.
|
| 294 |
+
*/
|
| 295 |
+
std::vector<OperatorHandle> findDanglingImpls() const;
|
| 296 |
+
|
| 297 |
+
/**
|
| 298 |
+
* Useful for inspecting global Dispatcher registration state.
|
| 299 |
+
* Returns the names of all operators with a kernel registered for the specified DispatchKey.
|
| 300 |
+
* If no DispatchKey is specified, it returns all registered operators.
|
| 301 |
+
*/
|
| 302 |
+
std::vector<OperatorName> getRegistrationsForDispatchKey(std::optional<DispatchKey> k) const;
|
| 303 |
+
|
| 304 |
+
private:
|
| 305 |
+
Dispatcher();
|
| 306 |
+
|
| 307 |
+
static int64_t sequenceNumberForRunningRecordFunction(DispatchKey dispatchKey, DispatchKeySet dispatchKeySet);
|
| 308 |
+
static void runRecordFunction(at::RecordFunction& guard, at::RecordFunction::schema_ref_t schema_ref, DispatchKey dispatchKey, DispatchKeySet dispatchKeySet);
|
| 309 |
+
static void runRecordFunction(at::RecordFunction& guard, at::RecordFunction::schema_ref_t schema_ref, DispatchKey dispatchKey, DispatchKeySet dispatchKeySet, c10::ArrayRef<const c10::IValue> args);
|
| 310 |
+
|
| 311 |
+
#ifdef FBCODE_CAFFE2
|
| 312 |
+
static bool profilingOperatorEvents();
|
| 313 |
+
static void fireOpStartUSDT(at::RecordFunction::schema_ref_t schema_ref);
|
| 314 |
+
static void fireOpEndUSDT(at::RecordFunction::schema_ref_t schema_ref);
|
| 315 |
+
#endif // FBCODE_CAFFE2
|
| 316 |
+
|
| 317 |
+
OperatorHandle findOrRegisterSchema_(FunctionSchema&& schema);
|
| 318 |
+
OperatorHandle findOrRegisterName_(const OperatorName& op_name);
|
| 319 |
+
|
| 320 |
+
void deregisterDef_(const OperatorHandle& op, const OperatorName& op_name);
|
| 321 |
+
void deregisterImpl_(
|
| 322 |
+
const OperatorHandle& op,
|
| 323 |
+
const OperatorName& op_name,
|
| 324 |
+
std::optional<DispatchKey> dispatch_key,
|
| 325 |
+
impl::OperatorEntry::AnnotatedKernelContainerIterator kernel_handle);
|
| 326 |
+
void deregisterName_(const OperatorHandle& op, const OperatorName& op_name);
|
| 327 |
+
void deregisterFallback_(DispatchKey dispatchKey);
|
| 328 |
+
void deregisterLibrary_(const std::string& ns);
|
| 329 |
+
void cleanup(const OperatorHandle& op, const OperatorName& op_name);
|
| 330 |
+
void checkSchemaCompatibility(const OperatorHandle& op, const FunctionSchema& schema, const std::string& debug);
|
| 331 |
+
|
| 332 |
+
std::list<OperatorDef> operators_;
|
| 333 |
+
#if !defined(C10_MOBILE)
|
| 334 |
+
LeftRight<ska::flat_hash_map<OperatorName, OperatorHandle>> operatorLookupTable_;
|
| 335 |
+
#else
|
| 336 |
+
RWSafeLeftRightWrapper<ska::flat_hash_map<OperatorName, OperatorHandle>> operatorLookupTable_;
|
| 337 |
+
#endif
|
| 338 |
+
// Map from namespace to debug string (saying, e.g., where the library was defined)
|
| 339 |
+
ska::flat_hash_map<std::string, std::string> libraries_;
|
| 340 |
+
|
| 341 |
+
std::array<impl::AnnotatedKernel, num_runtime_entries> backendFallbackKernels_;
|
| 342 |
+
|
| 343 |
+
std::unique_ptr<detail::RegistrationListenerList> listeners_;
|
| 344 |
+
|
| 345 |
+
// This condition variable gets notified whenever we add a new def/impl to the
|
| 346 |
+
// dispatch table. This is primarily used by multipy/torchdeploy, when
|
| 347 |
+
// we have multiple interpreters trying to register to the dispatch table.
|
| 348 |
+
// In this situation, whenever the non-primary interpreter would have tried
|
| 349 |
+
// to register to the dispatch table, instead it will check to see if the
|
| 350 |
+
// expected registration has already been made, and if it hasn't, wait on
|
| 351 |
+
// this condition variable to see if it was just racing with the primary
|
| 352 |
+
// interpreter.
|
| 353 |
+
//
|
| 354 |
+
// We expect it to be rare for there to be any waiters on this condition
|
| 355 |
+
// variable. This is mostly just to help give better diagnostics if
|
| 356 |
+
// something goes horribly wrong
|
| 357 |
+
std::condition_variable cond_var_;
|
| 358 |
+
|
| 359 |
+
// Protect concurrent access to the dispatcher. We store this in a
|
| 360 |
+
// `shared_ptr` as we return callbacks that call back into dispatcher methods,
|
| 361 |
+
// and we need to be able to handle and guard against the event when the
|
| 362 |
+
// `Dispatcher` has been destroyed before the callbacks fire.
|
| 363 |
+
std::shared_ptr<Guard> guard_;
|
| 364 |
+
};
|
| 365 |
+
|
| 366 |
+
/**
|
| 367 |
+
* This is a handle to an operator schema registered with the dispatcher.
|
| 368 |
+
* This handle can be used to register kernels with the dispatcher or
|
| 369 |
+
* to lookup a kernel for a certain set of arguments.
|
| 370 |
+
*/
|
| 371 |
+
class TORCH_API OperatorHandle {
|
| 372 |
+
template <typename T> friend struct std::hash;
|
| 373 |
+
|
| 374 |
+
public:
|
| 375 |
+
OperatorHandle(OperatorHandle&&) noexcept = default;
|
| 376 |
+
OperatorHandle& operator=(OperatorHandle&&) noexcept = default;
|
| 377 |
+
OperatorHandle(const OperatorHandle&) = default;
|
| 378 |
+
OperatorHandle& operator=(const OperatorHandle&) = default;
|
| 379 |
+
// NOLINTNEXTLINE(performance-trivially-destructible)
|
| 380 |
+
~OperatorHandle();
|
| 381 |
+
|
| 382 |
+
const OperatorName& operator_name() const {
|
| 383 |
+
return operatorDef_->op.operator_name();
|
| 384 |
+
}
|
| 385 |
+
|
| 386 |
+
bool hasSchema() const {
|
| 387 |
+
return operatorDef_->op.hasSchema();
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
const FunctionSchema& schema() const {
|
| 391 |
+
return operatorDef_->op.schema();
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
const std::string& debug() const {
|
| 395 |
+
return operatorDef_->op.debug();
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
std::string dumpState() const {
|
| 399 |
+
return operatorDef_->op.dumpState();
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
bool hasKernelForDispatchKey(DispatchKey k) const {
|
| 403 |
+
return operatorDef_->op.hasKernelForDispatchKey(k);
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
bool isKernelFallthroughKernel(DispatchKey k) const {
|
| 407 |
+
return operatorDef_->op.kernelForDispatchKey(k).isFallthrough();
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
+
bool hasKernelForAnyDispatchKey(DispatchKeySet k) const {
|
| 411 |
+
return operatorDef_->op.hasKernelForAnyDispatchKey(k);
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
bool hasComputedKernelForDispatchKey(DispatchKey k) const {
|
| 415 |
+
return operatorDef_->op.hasComputedKernelForDispatchKey(k);
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
std::string dumpComputedTable() const {
|
| 419 |
+
return operatorDef_->op.dumpComputedTable();
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
void checkInvariants() const {
|
| 423 |
+
return operatorDef_->op.checkInvariants();
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
+
c10::ArrayRef<at::Tag> getTags() const {
|
| 427 |
+
return operatorDef_->op.getTags();
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
void setReportErrorCallback_(std::unique_ptr<c10::SafePyObject> callback) {
|
| 431 |
+
operatorDef_->op.setReportErrorCallback_(std::move(callback));
|
| 432 |
+
}
|
| 433 |
+
|
| 434 |
+
bool hasTag(const at::Tag& tag) const {
|
| 435 |
+
for(const auto& tag_: getTags()) {
|
| 436 |
+
if (tag == tag_) {
|
| 437 |
+
return true;
|
| 438 |
+
}
|
| 439 |
+
}
|
| 440 |
+
return false;
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
template<class FuncType>
|
| 444 |
+
TypedOperatorHandle<FuncType> typed() const {
|
| 445 |
+
// NB: This assert is not 100% sound: you can retrieve a typed() operator
|
| 446 |
+
// handle prior to ANY C++ signature being registered on the operator
|
| 447 |
+
// and the check will say everything is OK (at which point you can then
|
| 448 |
+
// smuggle in a kernel that is typed incorrectly). For everything
|
| 449 |
+
// in core library this won't happen, because all the static registrations
|
| 450 |
+
// will be done by the time a typed() handle is acquired.
|
| 451 |
+
#if !defined C10_MOBILE
|
| 452 |
+
operatorDef_->op.assertSignatureIsCorrect<FuncType>();
|
| 453 |
+
if (fn_has_symint<FuncType>::value) {
|
| 454 |
+
operatorDef_->op.assertSignatureIsCorrect<typename fn_remove_symint<FuncType>::type>();
|
| 455 |
+
}
|
| 456 |
+
#endif
|
| 457 |
+
return TypedOperatorHandle<FuncType>(operatorIterator_);
|
| 458 |
+
}
|
| 459 |
+
|
| 460 |
+
void callBoxed(Stack* stack) const {
|
| 461 |
+
c10::Dispatcher::singleton().callBoxed(*this, stack);
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
void callBoxed(Stack& stack) const {
|
| 465 |
+
callBoxed(&stack);
|
| 466 |
+
}
|
| 467 |
+
|
| 468 |
+
void callBoxedForDispatchKey(DispatchKey dk, Stack& stack) const {
|
| 469 |
+
c10::Dispatcher::singleton().callBoxedForDispatchKey(*this, dk, &stack);
|
| 470 |
+
}
|
| 471 |
+
|
| 472 |
+
void redispatchBoxed(DispatchKeySet ks, Stack* stack) const {
|
| 473 |
+
c10::Dispatcher::singleton().redispatchBoxed(*this, ks, stack);
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
template <typename F>
|
| 477 |
+
PyObject* getPythonOp(c10::impl::PyInterpreter* self_interpreter, F slow_accessor) const {
|
| 478 |
+
return operatorDef_->op.getPythonOp(self_interpreter, slow_accessor);
|
| 479 |
+
}
|
| 480 |
+
|
| 481 |
+
bool operator==(const OperatorHandle& other) const {
|
| 482 |
+
return operatorDef_ == other.operatorDef_;
|
| 483 |
+
}
|
| 484 |
+
|
| 485 |
+
bool operator!=(const OperatorHandle& other) const {
|
| 486 |
+
return operatorDef_ != other.operatorDef_;
|
| 487 |
+
}
|
| 488 |
+
|
| 489 |
+
private:
|
| 490 |
+
explicit OperatorHandle(std::list<Dispatcher::OperatorDef>::iterator operatorIterator)
|
| 491 |
+
: operatorDef_(&*operatorIterator), operatorIterator_(operatorIterator) {}
|
| 492 |
+
friend class Dispatcher;
|
| 493 |
+
template<class> friend class TypedOperatorHandle;
|
| 494 |
+
|
| 495 |
+
// Storing a direct pointer to the OperatorDef even though we
|
| 496 |
+
// already have the iterator saves an instruction in the critical
|
| 497 |
+
// dispatch path. The iterator is effectively a
|
| 498 |
+
// pointer-to-std::list-node, and (at least in libstdc++'s
|
| 499 |
+
// implementation) the element is at an offset 16 bytes from that,
|
| 500 |
+
// because the prev/next pointers come first in the list node
|
| 501 |
+
// struct. So, an add instruction would be necessary to convert from the
|
| 502 |
+
// iterator to an OperatorDef*.
|
| 503 |
+
Dispatcher::OperatorDef* operatorDef_;
|
| 504 |
+
|
| 505 |
+
// We need to store this iterator in order to make
|
| 506 |
+
// Dispatcher::cleanup() fast -- it runs a lot on program
|
| 507 |
+
// termination (and presuambly library unloading).
|
| 508 |
+
std::list<Dispatcher::OperatorDef>::iterator operatorIterator_;
|
| 509 |
+
};
|
| 510 |
+
|
| 511 |
+
/**
|
| 512 |
+
* This is a handle to an operator schema registered with the dispatcher.
|
| 513 |
+
* It holds the same information as an OperatorHandle, but it is templated
|
| 514 |
+
* on the operator arguments and allows calling the operator in an
|
| 515 |
+
* unboxed way.
|
| 516 |
+
*/
|
| 517 |
+
template<class FuncType>
|
| 518 |
+
class TypedOperatorHandle final {
|
| 519 |
+
static_assert(guts::false_t<FuncType>(), "FuncType in OperatorHandle::typed<FuncType> was not a valid function type");
|
| 520 |
+
};
|
| 521 |
+
template<class Return, class... Args>
|
| 522 |
+
class TypedOperatorHandle<Return (Args...)> final : public OperatorHandle {
|
| 523 |
+
public:
|
| 524 |
+
TypedOperatorHandle(TypedOperatorHandle&&) noexcept = default;
|
| 525 |
+
TypedOperatorHandle& operator=(TypedOperatorHandle&&) noexcept = default;
|
| 526 |
+
TypedOperatorHandle(const TypedOperatorHandle&) = default;
|
| 527 |
+
TypedOperatorHandle& operator=(const TypedOperatorHandle&) = default;
|
| 528 |
+
|
| 529 |
+
// See [Note: Argument forwarding in the dispatcher] for why Args doesn't use &&
|
| 530 |
+
C10_ALWAYS_INLINE Return call(Args... args) const {
|
| 531 |
+
return c10::Dispatcher::singleton().call<Return, Args...>(*this, std::forward<Args>(args)...);
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
// See [Note: Argument forwarding in the dispatcher] for why Args doesn't use &&
|
| 535 |
+
C10_ALWAYS_INLINE Return redispatch(DispatchKeySet currentDispatchKeySet, Args... args) const {
|
| 536 |
+
return c10::Dispatcher::singleton().redispatch<Return, Args...>(*this, currentDispatchKeySet, std::forward<Args>(args)...);
|
| 537 |
+
}
|
| 538 |
+
|
| 539 |
+
private:
|
| 540 |
+
explicit TypedOperatorHandle(std::list<Dispatcher::OperatorDef>::iterator operatorIterator)
|
| 541 |
+
: OperatorHandle(operatorIterator) {}
|
| 542 |
+
friend class OperatorHandle;
|
| 543 |
+
};
|
| 544 |
+
|
| 545 |
+
namespace detail {
|
| 546 |
+
template <class... Args> inline void unused_arg_(const Args&...) {}
|
| 547 |
+
|
| 548 |
+
// CaptureKernelCall is intended to capture return values from Dispatcher
|
| 549 |
+
// unboxed kernel calls. A record function may request to get outputs from the
|
| 550 |
+
// kernel calls. For boxed kernels, it's straightforward, the returned values
|
| 551 |
+
// are in the stack object. The stack can be passed to record functions. For
|
| 552 |
+
// unboxed kernels, we need to handle different kinds of return values, cache
|
| 553 |
+
// them temporarily, then release the values for the actual function call
|
| 554 |
+
// return.
|
| 555 |
+
template <typename ReturnType>
|
| 556 |
+
struct CaptureKernelCall {
|
| 557 |
+
template <typename F, typename... Args>
|
| 558 |
+
CaptureKernelCall(
|
| 559 |
+
const F& kernel,
|
| 560 |
+
const TypedOperatorHandle<ReturnType(Args...)>& op,
|
| 561 |
+
const DispatchKeySet& dispatchKeySet,
|
| 562 |
+
Args&&... args)
|
| 563 |
+
// Calls the kernel and capture the result in output_.
|
| 564 |
+
: output_{kernel.template call<ReturnType, Args...>(
|
| 565 |
+
op,
|
| 566 |
+
dispatchKeySet,
|
| 567 |
+
std::forward<Args>(args)...)} {}
|
| 568 |
+
// Wraps the return values in a Stack.
|
| 569 |
+
Stack getOutputs() {
|
| 570 |
+
Stack stack;
|
| 571 |
+
impl::push_outputs<ReturnType, false>::copy(output_, &stack);
|
| 572 |
+
return stack;
|
| 573 |
+
}
|
| 574 |
+
// Since we are returning the output_, we don't expect the output_ to be used
|
| 575 |
+
// afterward. Copy elision and RVO do not apply to class data members. Using
|
| 576 |
+
// move semantic to avoid copies when possible.
|
| 577 |
+
ReturnType release() && {
|
| 578 |
+
return std::move(output_);
|
| 579 |
+
}
|
| 580 |
+
|
| 581 |
+
private:
|
| 582 |
+
ReturnType output_;
|
| 583 |
+
};
|
| 584 |
+
|
| 585 |
+
// Handle the lvalue reference differently since it should not be moved.
|
| 586 |
+
template <>
|
| 587 |
+
inline at::Tensor& CaptureKernelCall<at::Tensor&>::release() && {
|
| 588 |
+
return output_;
|
| 589 |
+
}
|
| 590 |
+
|
| 591 |
+
// Handle case where the kernel returns void.
|
| 592 |
+
template <>
|
| 593 |
+
struct CaptureKernelCall<void> {
|
| 594 |
+
template <typename F, typename... Args>
|
| 595 |
+
CaptureKernelCall(
|
| 596 |
+
const F& kernel,
|
| 597 |
+
const TypedOperatorHandle<void(Args...)>& op,
|
| 598 |
+
const DispatchKeySet& dispatchKeySet,
|
| 599 |
+
Args&&... args) {
|
| 600 |
+
// Calling the kernel and no need to capture void.
|
| 601 |
+
kernel.template call<void, Args...>(
|
| 602 |
+
op, dispatchKeySet, std::forward<Args>(args)...);
|
| 603 |
+
}
|
| 604 |
+
Stack getOutputs() {
|
| 605 |
+
return Stack();
|
| 606 |
+
}
|
| 607 |
+
void release() && {}
|
| 608 |
+
};
|
| 609 |
+
|
| 610 |
+
TORCH_API void _print_dispatch_trace(const std::string& label, const std::string& op_name, const DispatchKeySet& dispatchKeySet);
|
| 611 |
+
|
| 612 |
+
} // namespace detail
|
| 613 |
+
|
| 614 |
+
// See [Note: Argument forwarding in the dispatcher] for why Args doesn't use &&
|
| 615 |
+
template<class Return, class... Args>
|
| 616 |
+
inline Return Dispatcher::callWithDispatchKeySlowPath(const TypedOperatorHandle<Return(Args...)>& op, at::StepCallbacks& stepCallbacks, DispatchKeySet dispatchKeySet, const KernelFunction& kernel, Args... args) {
|
| 617 |
+
// If callbacks need inputs, we box the arguments and pass them to the guard.
|
| 618 |
+
// Note: For perf reasons we wouldn't want to prematurely box the arguments.
|
| 619 |
+
at::RecordFunction guard(std::move(stepCallbacks));
|
| 620 |
+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(op.operatorDef_->op.isObserved());
|
| 621 |
+
auto dispatchKey = dispatchKeySet.highestPriorityTypeId();
|
| 622 |
+
auto& schema = op.schema();
|
| 623 |
+
auto schema_ref = std::reference_wrapper<const FunctionSchema>(schema);
|
| 624 |
+
constexpr auto num_boxed_args = impl::boxed_size<Args...>();
|
| 625 |
+
if constexpr (num_boxed_args != 0) {
|
| 626 |
+
if (guard.needsInputs()) {
|
| 627 |
+
// If we used std::array<IValue, num_boxed_args> here, we would
|
| 628 |
+
// have to spend time default constructing the IValues in
|
| 629 |
+
// boxedArgs. aligned_storage has no such requirement.
|
| 630 |
+
impl::IValueAlignedStorage boxedArgs[num_boxed_args];
|
| 631 |
+
// For debugging only; could be removed (but the compiler will do
|
| 632 |
+
// that for us and it's nice to have the extra assurance of
|
| 633 |
+
// correctness from our debug builds).
|
| 634 |
+
int lastArgIdx = 0;
|
| 635 |
+
impl::boxArgsToStack(boxedArgs, lastArgIdx, args...);
|
| 636 |
+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(lastArgIdx == num_boxed_args);
|
| 637 |
+
// I don't *think* we need std::launder here, because IValue has
|
| 638 |
+
// no subclasses and no const or reference fields.
|
| 639 |
+
runRecordFunction(guard, schema_ref, dispatchKey, dispatchKeySet, c10::ArrayRef<const c10::IValue>(reinterpret_cast<IValue *>(boxedArgs), num_boxed_args));
|
| 640 |
+
for (size_t ii = 0; ii < num_boxed_args; ++ii) {
|
| 641 |
+
reinterpret_cast<IValue *>(&boxedArgs[ii])->~IValue();
|
| 642 |
+
}
|
| 643 |
+
} else {
|
| 644 |
+
runRecordFunction(guard, schema_ref, dispatchKey, dispatchKeySet);
|
| 645 |
+
}
|
| 646 |
+
} else {
|
| 647 |
+
runRecordFunction(guard, schema_ref, dispatchKey, dispatchKeySet);
|
| 648 |
+
}
|
| 649 |
+
|
| 650 |
+
if (C10_UNLIKELY(guard.needsOutputs())) {
|
| 651 |
+
// Calls the kernel and capture the output temporarily to pass to
|
| 652 |
+
// RecordFunction.
|
| 653 |
+
detail::CaptureKernelCall<Return> captureKernelCall(
|
| 654 |
+
kernel, op, dispatchKeySet, std::forward<Args>(args)...);
|
| 655 |
+
guard.setOutputs(captureKernelCall.getOutputs());
|
| 656 |
+
// Releases the captured output to return to caller.
|
| 657 |
+
return std::move(captureKernelCall).release();
|
| 658 |
+
}
|
| 659 |
+
|
| 660 |
+
// keeping the guard alive while executing the kernel
|
| 661 |
+
return kernel.template call<Return, Args...>(op, dispatchKeySet, std::forward<Args>(args)...);
|
| 662 |
+
}
|
| 663 |
+
|
| 664 |
+
// See [Note: Argument forwarding in the dispatcher] for why Args doesn't use &&
|
| 665 |
+
template<class Return, class... Args>
|
| 666 |
+
C10_ALWAYS_INLINE_UNLESS_MOBILE Return Dispatcher::call(const TypedOperatorHandle<Return(Args...)>& op, Args... args) const {
|
| 667 |
+
detail::unused_arg_(args...); // workaround for a false-positive warning about unused parameters in gcc 5
|
| 668 |
+
auto dispatchKeySet = op.operatorDef_->op.dispatchKeyExtractor()
|
| 669 |
+
.template getDispatchKeySetUnboxed<Args...>(args...);
|
| 670 |
+
#ifndef NDEBUG
|
| 671 |
+
DispatchTraceNestingGuard debug_guard;
|
| 672 |
+
if (show_dispatch_trace()) {
|
| 673 |
+
detail::_print_dispatch_trace("[call]", toString(op.operator_name()), dispatchKeySet);
|
| 674 |
+
}
|
| 675 |
+
#endif
|
| 676 |
+
const KernelFunction& kernel = op.operatorDef_->op.lookup(dispatchKeySet);
|
| 677 |
+
#ifndef PYTORCH_DISABLE_PER_OP_PROFILING
|
| 678 |
+
auto step_callbacks = at::getStepCallbacksUnlessEmpty(at::RecordScope::FUNCTION);
|
| 679 |
+
if (C10_UNLIKELY(step_callbacks.has_value() && op.operatorDef_->op.isObserved())) {
|
| 680 |
+
return callWithDispatchKeySlowPath<Return, Args...>(op, *step_callbacks, dispatchKeySet, kernel, std::forward<Args>(args)...);
|
| 681 |
+
}
|
| 682 |
+
#endif // PYTORCH_DISABLE_PER_OP_PROFILING
|
| 683 |
+
|
| 684 |
+
#ifdef FBCODE_CAFFE2
|
| 685 |
+
if(profilingOperatorEvents()) {
|
| 686 |
+
struct FireOpRAII {
|
| 687 |
+
FireOpRAII(at::RecordFunction::schema_ref_t schema_ref) : schema_ref_(schema_ref) {
|
| 688 |
+
fireOpStartUSDT(schema_ref);
|
| 689 |
+
}
|
| 690 |
+
~FireOpRAII() { fireOpEndUSDT(schema_ref_); }
|
| 691 |
+
at::RecordFunction::schema_ref_t schema_ref_;
|
| 692 |
+
} event(op.schema());
|
| 693 |
+
return kernel.template call<Return, Args...>(op, dispatchKeySet, std::forward<Args>(args)...);
|
| 694 |
+
} else {
|
| 695 |
+
return kernel.template call<Return, Args...>(op, dispatchKeySet, std::forward<Args>(args)...);
|
| 696 |
+
}
|
| 697 |
+
#else
|
| 698 |
+
return kernel.template call<Return, Args...>(op, dispatchKeySet, std::forward<Args>(args)...);
|
| 699 |
+
#endif // FBCODE_CAFFE2
|
| 700 |
+
}
|
| 701 |
+
|
| 702 |
+
// See [Note: Argument forwarding in the dispatcher] for why Args doesn't use &&
|
| 703 |
+
template<class Return, class... Args>
|
| 704 |
+
inline Return Dispatcher::redispatch(const TypedOperatorHandle<Return (Args...)>& op, DispatchKeySet currentDispatchKeySet, Args... args) const {
|
| 705 |
+
detail::unused_arg_(args...); // workaround for a false-positive warning about unused parameters in gcc 5
|
| 706 |
+
// do not use RecordFunction on redispatch
|
| 707 |
+
#ifndef NDEBUG
|
| 708 |
+
DispatchTraceNestingGuard debug_guard;
|
| 709 |
+
if (show_dispatch_trace()) {
|
| 710 |
+
detail::_print_dispatch_trace("[redispatch]", toString(op.operator_name()), currentDispatchKeySet);
|
| 711 |
+
}
|
| 712 |
+
#endif
|
| 713 |
+
const KernelFunction& kernel = op.operatorDef_->op.lookup(currentDispatchKeySet);
|
| 714 |
+
return kernel.template call<Return, Args...>(op, currentDispatchKeySet, std::forward<Args>(args)...);
|
| 715 |
+
}
|
| 716 |
+
|
| 717 |
+
inline void Dispatcher::callBoxed(const OperatorHandle& op, Stack* stack) const {
|
| 718 |
+
// note: this doesn't need the mutex because write operations on the list keep iterators intact.
|
| 719 |
+
const auto& entry = op.operatorDef_->op;
|
| 720 |
+
auto dispatchKeySet = entry.dispatchKeyExtractor().getDispatchKeySetBoxed(stack);
|
| 721 |
+
#ifndef NDEBUG
|
| 722 |
+
DispatchTraceNestingGuard debug_guard;
|
| 723 |
+
if (show_dispatch_trace()) {
|
| 724 |
+
detail::_print_dispatch_trace("[callBoxed]", toString(op.operator_name()), dispatchKeySet);
|
| 725 |
+
}
|
| 726 |
+
#endif
|
| 727 |
+
const auto& kernel = entry.lookup(dispatchKeySet);
|
| 728 |
+
#ifndef PYTORCH_DISABLE_PER_OP_PROFILING
|
| 729 |
+
auto step_callbacks = at::getStepCallbacksUnlessEmpty(at::RecordScope::FUNCTION);
|
| 730 |
+
if (C10_UNLIKELY(step_callbacks.has_value() && entry.isObserved())) {
|
| 731 |
+
at::RecordFunction guard(std::move(*step_callbacks));
|
| 732 |
+
auto dispatchKey = dispatchKeySet.highestPriorityTypeId();
|
| 733 |
+
auto& schema = op.schema();
|
| 734 |
+
auto schema_ref = std::reference_wrapper<const FunctionSchema>(schema);
|
| 735 |
+
guard.needsInputs() ? runRecordFunction(guard, schema_ref, dispatchKey, dispatchKeySet, c10::ArrayRef<const c10::IValue>(stack->data(), stack->size()))
|
| 736 |
+
: runRecordFunction(guard, schema_ref, dispatchKey, dispatchKeySet);
|
| 737 |
+
|
| 738 |
+
// keeping the guard alive while executing the kernel
|
| 739 |
+
kernel.callBoxed(op, dispatchKeySet, stack);
|
| 740 |
+
|
| 741 |
+
if (C10_UNLIKELY(guard.needsOutputs())) {
|
| 742 |
+
guard.setOutputs(*stack);
|
| 743 |
+
}
|
| 744 |
+
return;
|
| 745 |
+
}
|
| 746 |
+
#endif // PYTORCH_DISABLE_PER_OP_PROFILING
|
| 747 |
+
kernel.callBoxed(op, dispatchKeySet, stack);
|
| 748 |
+
}
|
| 749 |
+
|
| 750 |
+
// NB: this doesn't count as a "true" dispatcher jump, so no instrumentation
|
| 751 |
+
inline void Dispatcher::callBoxedForDispatchKey(const OperatorHandle& op, DispatchKey dk, Stack* stack) const {
|
| 752 |
+
// note: this doesn't need the mutex because write operations on the list keep iterators intact.
|
| 753 |
+
const auto& entry = op.operatorDef_->op;
|
| 754 |
+
// We still compute this as we're obligated to pass it on to the internal
|
| 755 |
+
// kernel, if it is a boxed fallback
|
| 756 |
+
auto dispatchKeySet = entry.dispatchKeyExtractor().getDispatchKeySetBoxed(stack);
|
| 757 |
+
const auto& kernel = ([&]() {
|
| 758 |
+
if (op.hasKernelForDispatchKey(dk)) {
|
| 759 |
+
return entry.kernelForDispatchKey(dk);
|
| 760 |
+
} else {
|
| 761 |
+
auto idx = getDispatchTableIndexForDispatchKey(dk);
|
| 762 |
+
TORCH_INTERNAL_ASSERT(idx >= 0);
|
| 763 |
+
return backendFallbackKernels_[idx].kernel;
|
| 764 |
+
}
|
| 765 |
+
})();
|
| 766 |
+
kernel.callBoxed(op, dispatchKeySet, stack);
|
| 767 |
+
}
|
| 768 |
+
|
| 769 |
+
inline void Dispatcher::redispatchBoxed(const OperatorHandle& op, DispatchKeySet dispatchKeySet, Stack* stack) const {
|
| 770 |
+
// note: this doesn't need the mutex because write operations on the list keep iterators intact.
|
| 771 |
+
const auto& entry = op.operatorDef_->op;
|
| 772 |
+
#ifndef NDEBUG
|
| 773 |
+
DispatchTraceNestingGuard debug_guard;
|
| 774 |
+
if (show_dispatch_trace()) {
|
| 775 |
+
detail::_print_dispatch_trace("[redispatchBoxed]", toString(op.operator_name()), dispatchKeySet);
|
| 776 |
+
}
|
| 777 |
+
#endif
|
| 778 |
+
const auto& kernel = entry.lookup(dispatchKeySet);
|
| 779 |
+
return kernel.callBoxed(op, dispatchKeySet, stack);
|
| 780 |
+
}
|
| 781 |
+
|
| 782 |
+
} // namespace c10
|
| 783 |
+
|
| 784 |
+
namespace std {
|
| 785 |
+
|
| 786 |
+
template <>
|
| 787 |
+
struct hash<c10::OperatorHandle> {
|
| 788 |
+
size_t operator()(const c10::OperatorHandle& op) const noexcept {
|
| 789 |
+
return std::hash<void*>{}(static_cast<void*>(op.operatorDef_));
|
| 790 |
+
}
|
| 791 |
+
};
|
| 792 |
+
|
| 793 |
+
} // namespace std
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/ObservedOperators.h
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/core/operator_name.h>
|
| 4 |
+
#include <string>
|
| 5 |
+
#include <unordered_set>
|
| 6 |
+
|
| 7 |
+
namespace c10 {
|
| 8 |
+
|
| 9 |
+
struct TORCH_API ObservedOperators {
|
| 10 |
+
ObservedOperators() = delete;
|
| 11 |
+
|
| 12 |
+
static bool isObserved(const OperatorName& name);
|
| 13 |
+
|
| 14 |
+
static std::unordered_set<std::string>& getUnobservedOperatorList();
|
| 15 |
+
};
|
| 16 |
+
|
| 17 |
+
} // namespace c10
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/OperatorEntry.h
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/core/function_schema.h>
|
| 4 |
+
#include <c10/util/Metaprogramming.h>
|
| 5 |
+
#include <c10/util/flat_hash_map.h>
|
| 6 |
+
#include <c10/core/DispatchKey.h>
|
| 7 |
+
#include <c10/core/PyHandleCache.h>
|
| 8 |
+
#include <c10/core/SafePyObject.h>
|
| 9 |
+
#include <ATen/core/ivalue.h>
|
| 10 |
+
#include <ATen/core/boxing/KernelFunction.h>
|
| 11 |
+
#include <ATen/core/dispatch/DispatchKeyExtractor.h>
|
| 12 |
+
|
| 13 |
+
#include <ATen/core/dispatch/OperatorOptions.h>
|
| 14 |
+
#include <ATen/core/dispatch/CppSignature.h>
|
| 15 |
+
#include <ATen/core/dispatch/RegistrationHandleRAII.h>
|
| 16 |
+
#include <ATen/core/enum_tag.h>
|
| 17 |
+
|
| 18 |
+
#include <optional>
|
| 19 |
+
#include <array>
|
| 20 |
+
#include <list>
|
| 21 |
+
|
| 22 |
+
#ifdef C10_MOBILE
|
| 23 |
+
#define C10_DISPATCHER_ONE_KERNEL_PER_DISPATCH_KEY
|
| 24 |
+
#endif
|
| 25 |
+
|
| 26 |
+
namespace c10 {
|
| 27 |
+
|
| 28 |
+
class Dispatcher;
|
| 29 |
+
|
| 30 |
+
namespace impl {
|
| 31 |
+
|
| 32 |
+
// This data structure represents a kernel that was registered to us from a
|
| 33 |
+
// user. Unlike KernelFunction, AnnotatedKernel contains some extra metadata
|
| 34 |
+
// about the kernel that isn't necessary for actual dispatching (this is why
|
| 35 |
+
// we don't put AnnotatedKernel in the actual DispatchTable), but is useful for
|
| 36 |
+
// giving good error messages.
|
| 37 |
+
struct AnnotatedKernel final {
|
| 38 |
+
AnnotatedKernel(KernelFunction k, std::unique_ptr<FunctionSchema> s, std::string d)
|
| 39 |
+
: kernel(std::move(k))
|
| 40 |
+
, inferred_function_schema(std::move(s))
|
| 41 |
+
, debug(std::move(d))
|
| 42 |
+
{}
|
| 43 |
+
AnnotatedKernel() = default;
|
| 44 |
+
KernelFunction kernel;
|
| 45 |
+
std::unique_ptr<FunctionSchema> inferred_function_schema;
|
| 46 |
+
// A little debug string to help us identify the kernel in question.
|
| 47 |
+
// Most importantly it records the TORCH_LIBRARY block that did the
|
| 48 |
+
// registration.
|
| 49 |
+
std::string debug;
|
| 50 |
+
};
|
| 51 |
+
|
| 52 |
+
// This data structure represents operator schema, with metadata specifying
|
| 53 |
+
// where the registration of this schema occurred
|
| 54 |
+
struct AnnotatedSchema final {
|
| 55 |
+
AnnotatedSchema(FunctionSchema s, std::string d)
|
| 56 |
+
: schema(std::move(s))
|
| 57 |
+
, debug(std::move(d))
|
| 58 |
+
{}
|
| 59 |
+
FunctionSchema schema;
|
| 60 |
+
std::string debug;
|
| 61 |
+
};
|
| 62 |
+
|
| 63 |
+
// Internal data structure that records information about a specific operator.
|
| 64 |
+
// It's not part of the public API; typically, users will interact with
|
| 65 |
+
// OperatorHandle instead.
|
| 66 |
+
//
|
| 67 |
+
// Concurrent writes to OperatorEntry are protected by the GLOBAL Dispatcher
|
| 68 |
+
// lock (this is important because some methods in OperatorEntry access
|
| 69 |
+
// dispatcher state)
|
| 70 |
+
class TORCH_API OperatorEntry final {
|
| 71 |
+
public:
|
| 72 |
+
explicit OperatorEntry(OperatorName&& operator_name);
|
| 73 |
+
|
| 74 |
+
OperatorEntry(const OperatorEntry&) = delete;
|
| 75 |
+
OperatorEntry(OperatorEntry&&) noexcept = delete;
|
| 76 |
+
OperatorEntry& operator=(const OperatorEntry&) = delete;
|
| 77 |
+
OperatorEntry& operator=(OperatorEntry&&) noexcept = delete;
|
| 78 |
+
|
| 79 |
+
const FunctionSchema& schema() const {
|
| 80 |
+
TORCH_INTERNAL_ASSERT(schema_.has_value(), "Tried to access the schema for ", name_, " which doesn't have a schema registered yet");
|
| 81 |
+
return schema_->schema;
|
| 82 |
+
}
|
| 83 |
+
const std::string& debug() const {
|
| 84 |
+
TORCH_INTERNAL_ASSERT(schema_.has_value());
|
| 85 |
+
return schema_->debug;
|
| 86 |
+
}
|
| 87 |
+
bool hasSchema() const {
|
| 88 |
+
return schema_.has_value();
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
bool isObserved() const {
|
| 92 |
+
return is_observed_;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
// We may allocate an OperatorEntry for an operator even when we don't
|
| 96 |
+
// have a schema. When we receive the schema registration, we post
|
| 97 |
+
// facto register a schema.
|
| 98 |
+
//
|
| 99 |
+
// NB: registerSchema/deregisterSchema are not idempotent; if you
|
| 100 |
+
// attempt to register a schema when one is already present or vice
|
| 101 |
+
// versa that is an error. (Refcounting for the registrations is
|
| 102 |
+
// handled in the OperatorHandle in Dispatcher)
|
| 103 |
+
void registerSchema(FunctionSchema&&, std::string&& debug, std::vector<at::Tag> tags = {});
|
| 104 |
+
void deregisterSchema();
|
| 105 |
+
|
| 106 |
+
const OperatorName& operator_name() const {
|
| 107 |
+
return name_;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
#ifdef C10_DISPATCHER_ONE_KERNEL_PER_DISPATCH_KEY
|
| 111 |
+
using AnnotatedKernelContainer = std::array<AnnotatedKernel, 1>;
|
| 112 |
+
#else
|
| 113 |
+
using AnnotatedKernelContainer = std::list<AnnotatedKernel>;
|
| 114 |
+
#endif
|
| 115 |
+
using AnnotatedKernelContainerIterator = AnnotatedKernelContainer::iterator;
|
| 116 |
+
|
| 117 |
+
// Why are kernels and fallback asymmetric? It has to do with ownership.
|
| 118 |
+
// Kernels and the computed dispatch tables for them are canonically
|
| 119 |
+
// owned by OperatorEntry, but backend fallbacks are specified once
|
| 120 |
+
// and apply for all operators, so they should be owned by Dispatcher.
|
| 121 |
+
// However, the registration of a backend fallback affects the
|
| 122 |
+
// state of the computed dispatch table, so when a backend fallback
|
| 123 |
+
// is updated, we need to update the operator tables too. Thus,
|
| 124 |
+
// registerKernel is the mechanism by which we give kernels to
|
| 125 |
+
// operator entry to own (and update dispatch table), but we only
|
| 126 |
+
// need a non-owning mechanism to update fallback.
|
| 127 |
+
|
| 128 |
+
// Precondition: Dispatcher::mutex_ is held
|
| 129 |
+
// Postcondition: caller is responsible for disposing of the kernel
|
| 130 |
+
AnnotatedKernelContainerIterator registerKernel(
|
| 131 |
+
const Dispatcher& dispatcher,
|
| 132 |
+
std::optional<DispatchKey> dispatch_key,
|
| 133 |
+
KernelFunction kernel,
|
| 134 |
+
std::optional<CppSignature> cpp_signature,
|
| 135 |
+
std::unique_ptr<FunctionSchema> inferred_function_schema,
|
| 136 |
+
std::string debug
|
| 137 |
+
);
|
| 138 |
+
|
| 139 |
+
// Precondition: Dispatcher::mutex_ is held
|
| 140 |
+
void deregisterKernel_(
|
| 141 |
+
const Dispatcher& dispatcher,
|
| 142 |
+
std::optional<DispatchKey> dispatch_key,
|
| 143 |
+
AnnotatedKernelContainerIterator kernel
|
| 144 |
+
);
|
| 145 |
+
|
| 146 |
+
// Precondition: Dispatcher::mutex_ is held
|
| 147 |
+
void updateFallback(
|
| 148 |
+
const Dispatcher& dispatcher,
|
| 149 |
+
DispatchKey dispatch_key
|
| 150 |
+
);
|
| 151 |
+
|
| 152 |
+
// Precondition: Dispatcher::mutex_ is held
|
| 153 |
+
void updateSchemaAliasAnalysis(AliasAnalysisKind a) {
|
| 154 |
+
TORCH_INTERNAL_ASSERT(schema_.has_value());
|
| 155 |
+
schema_->schema.setAliasAnalysis(a);
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
std::string dumpComputedTable() const;
|
| 159 |
+
std::string dumpState() const;
|
| 160 |
+
void checkInvariants() const;
|
| 161 |
+
|
| 162 |
+
const DispatchKeyExtractor& dispatchKeyExtractor() const { return dispatchKeyExtractor_; }
|
| 163 |
+
|
| 164 |
+
// Asserts that the given FuncType is correct for calling this operator in an unboxed way.
|
| 165 |
+
template<class FuncType>
|
| 166 |
+
inline void assertSignatureIsCorrect() {
|
| 167 |
+
assertSignatureIsCorrect(CppSignature::make<FuncType>(), fn_has_symint<FuncType>::value);
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
void assertSignatureIsCorrect(const CppSignature& call_signature, bool has_symint) const;
|
| 171 |
+
|
| 172 |
+
[[noreturn]] void reportError(DispatchKey dispatchKey) const;
|
| 173 |
+
|
| 174 |
+
const KernelFunction& lookup(DispatchKeySet ks) const {
|
| 175 |
+
const auto idx = ks.getDispatchTableIndexForDispatchKeySet();
|
| 176 |
+
if (C10_UNLIKELY(idx == -1)) {
|
| 177 |
+
reportError(ks.highestPriorityTypeId());
|
| 178 |
+
}
|
| 179 |
+
const auto& kernel = dispatchTable_[idx];
|
| 180 |
+
// A valid kernel *always* has a boxed kernel and *may* have an
|
| 181 |
+
// unboxed kernel. However, we typically do unboxed calls in at::
|
| 182 |
+
// APIs, where the kernel 1) will very likely be valid and 2)
|
| 183 |
+
// should have an unboxed kernel. Checking the unboxed kernel
|
| 184 |
+
// first will allow us to avoid touching the boxed kernel at all
|
| 185 |
+
// in the common case.
|
| 186 |
+
if (C10_UNLIKELY(!kernel.isValidUnboxed())) {
|
| 187 |
+
if (!kernel.isValid()) {
|
| 188 |
+
reportError(ks.highestPriorityTypeId());
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
return kernel;
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
std::string listAllDispatchKeys() const;
|
| 195 |
+
|
| 196 |
+
// Returns true if kernel_ has entry for any key in ks.
|
| 197 |
+
//
|
| 198 |
+
// Invariant: There are no alias keys in the passed-in dispatch key set.
|
| 199 |
+
// Note [No Alias Keys in DispatchKeySet]
|
| 200 |
+
// Alias keys should be checked using `hasKernelForDispatchKey`
|
| 201 |
+
// Alias keys shouldn't go inside of a DispatchKeySet, since they can technically
|
| 202 |
+
// have a value > 63 (causing overflow).
|
| 203 |
+
bool hasKernelForAnyDispatchKey(DispatchKeySet ks) const;
|
| 204 |
+
// Returns true if kernel_ has entry for a particular key.
|
| 205 |
+
bool hasKernelForDispatchKey(DispatchKey k) const;
|
| 206 |
+
// Retrieves the kernel entry at a particular key. Symmetric with
|
| 207 |
+
// hasKernelForDispatchKey. To get the AnnotatedKernel, see
|
| 208 |
+
// getKernelForDispatchKey (private)
|
| 209 |
+
const KernelFunction& kernelForDispatchKey(DispatchKey k) const;
|
| 210 |
+
// Returns true if the "computed table" has an entry for a particular key.
|
| 211 |
+
bool hasComputedKernelForDispatchKey(DispatchKey k) const;
|
| 212 |
+
// Returns all the operator tags added at the time of registration
|
| 213 |
+
const std::vector<at::Tag>& getTags() const;
|
| 214 |
+
void setReportErrorCallback_(std::unique_ptr<c10::SafePyObject> callback);
|
| 215 |
+
|
| 216 |
+
template <typename F>
|
| 217 |
+
PyObject* getPythonOp(PyInterpreter* self_interpreter, F slow_accessor) const {
|
| 218 |
+
return py_cache_.ptr_or(self_interpreter, slow_accessor);
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
private:
|
| 222 |
+
|
| 223 |
+
OperatorName name_;
|
| 224 |
+
std::optional<AnnotatedSchema> schema_;
|
| 225 |
+
#ifndef C10_MOBILE
|
| 226 |
+
std::vector<at::Tag> tags_;
|
| 227 |
+
#endif
|
| 228 |
+
std::array<KernelFunction, c10::num_runtime_entries> dispatchTable_;
|
| 229 |
+
DispatchKeyExtractor dispatchKeyExtractor_;
|
| 230 |
+
// Pointer to the torch.ops.ns.op.overload object for speed
|
| 231 |
+
c10::PyHandleCache py_cache_;
|
| 232 |
+
|
| 233 |
+
// kernels_ stores all registered kernels for the corresponding dispatch key
|
| 234 |
+
// and catchAllKernels_ stores the catch-all kernels.
|
| 235 |
+
// If an operator library gets loaded that overwrites an already existing kernel,
|
| 236 |
+
// both kernels will be in that list but only the newer one will be in
|
| 237 |
+
// dispatchTable. If any of the kernels go away (say the library gets
|
| 238 |
+
// unloaded), we remove the kernel from this list and update the
|
| 239 |
+
// dispatchTable if necessary.
|
| 240 |
+
// Kernels in the list are ordered by registration time descendingly,
|
| 241 |
+
// newer registrations are before older registrations.
|
| 242 |
+
// We do not combine dispatchTable and kernels into one hash map because
|
| 243 |
+
// kernels is a larger data structure and accessed quite infrequently
|
| 244 |
+
// while dispatchTable is accessed often and should be kept small to fit
|
| 245 |
+
// into CPU caches.
|
| 246 |
+
// Invariants:
|
| 247 |
+
// - dispatchTable[dispatch_key] == kernels_[dispatch_key].front()
|
| 248 |
+
// - dispatchTable[dispatch_key] does not exist if and only if
|
| 249 |
+
// kernels_[dispatch_key] does not exist
|
| 250 |
+
// - If kernels_[dispatch_key] exists, then it has elements.
|
| 251 |
+
// It is never an empty list.
|
| 252 |
+
//
|
| 253 |
+
// Why do we do that?
|
| 254 |
+
// -----
|
| 255 |
+
// We mostly do this to enable Jupyter notebooks where a cell registering
|
| 256 |
+
// a kernel could be executed multiple times and the later execution
|
| 257 |
+
// should overwrite the earlier one. Note that this still fails when the
|
| 258 |
+
// function schema changed between the executions, but it works as long
|
| 259 |
+
// as the function schema didn't change. A better solution would be to
|
| 260 |
+
// unload the old extension library from the Jupyter cell when the cell is
|
| 261 |
+
// re-executed and then only allow one kernel here, i.e. error if a kernel
|
| 262 |
+
// is already registered, but that's a lot of effort to implement and
|
| 263 |
+
// currently not high-pri.
|
| 264 |
+
ska::flat_hash_map<DispatchKey,
|
| 265 |
+
#ifdef C10_DISPATCHER_ONE_KERNEL_PER_DISPATCH_KEY
|
| 266 |
+
// On mobile, we needn't worry about Jupyter notebooks.
|
| 267 |
+
std::array<AnnotatedKernel, 1>
|
| 268 |
+
#else
|
| 269 |
+
std::list<AnnotatedKernel>
|
| 270 |
+
#endif
|
| 271 |
+
> kernels_;
|
| 272 |
+
|
| 273 |
+
const AnnotatedKernel& missingKernel() const;
|
| 274 |
+
const AnnotatedKernel& ambiguousAutogradOtherKernel() const;
|
| 275 |
+
|
| 276 |
+
// cpp_signature_ stores function signature if any of
|
| 277 |
+
// the kernels was created in a way that allowed us to know the function
|
| 278 |
+
// signature (i.e. by supplying an unboxed C++ kernel function).
|
| 279 |
+
// If this is set, it will be used to check that future kernel
|
| 280 |
+
// registrations match and it will be used in unboxed function calls
|
| 281 |
+
// to verify their arguments against the known function signature.
|
| 282 |
+
struct CppSignatureWithDebug {
|
| 283 |
+
CppSignature signature;
|
| 284 |
+
std::string debug;
|
| 285 |
+
std::optional<DispatchKey> dispatch_key;
|
| 286 |
+
};
|
| 287 |
+
std::optional<CppSignatureWithDebug> cpp_signature_;
|
| 288 |
+
std::optional<CppSignatureWithDebug> sym_cpp_signature_;
|
| 289 |
+
|
| 290 |
+
// A Python custom error handler for OperatorEntry::reportError
|
| 291 |
+
std::unique_ptr<c10::SafePyObject> report_error_callback_;
|
| 292 |
+
|
| 293 |
+
// Whether this operator needs to be observed with RecordFunction
|
| 294 |
+
const bool is_observed_;
|
| 295 |
+
|
| 296 |
+
[[noreturn]] void reportSignatureError(const CppSignature& call_signature, const CppSignatureWithDebug& saved_signature) const;
|
| 297 |
+
const KernelFunction& computeDispatchTableEntry(const c10::Dispatcher& dispatcher, DispatchKey dispatch_key) const;
|
| 298 |
+
std::pair<const AnnotatedKernel&, const char*> computeDispatchTableEntryWithDebug(
|
| 299 |
+
const c10::Dispatcher& dispatcher, DispatchKey dispatch_key
|
| 300 |
+
) const;
|
| 301 |
+
// This function re-establishes the invariant that dispatchTable
|
| 302 |
+
// contains the front element from the kernels list for a given runtime dispatch key.
|
| 303 |
+
void updateDispatchTableEntry_(const c10::Dispatcher& dispatcher, DispatchKey dispatch_key);
|
| 304 |
+
// Like above, but also handles alias dispatch keys.
|
| 305 |
+
void updateDispatchTable_(const c10::Dispatcher& dispatcher, DispatchKey dispatch_key);
|
| 306 |
+
// Like above, but for ALL entries in the dispatch table.
|
| 307 |
+
void updateDispatchTableFull_(const c10::Dispatcher& dispatcher);
|
| 308 |
+
// Retrieves a pointer to AnnotatedKernel at kernels_.at(dispatch_key).front().
|
| 309 |
+
const AnnotatedKernel* getKernelForDispatchKey(DispatchKey dispatch_key) const;
|
| 310 |
+
};
|
| 311 |
+
|
| 312 |
+
} // namespace impl
|
| 313 |
+
} // namespace c10
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/dispatch/RegistrationHandleRAII.h
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <functional>
|
| 4 |
+
|
| 5 |
+
namespace c10 {
|
| 6 |
+
|
| 7 |
+
class RegistrationHandleRAII final {
|
| 8 |
+
public:
|
| 9 |
+
explicit RegistrationHandleRAII(std::function<void()> onDestruction)
|
| 10 |
+
: onDestruction_(std::move(onDestruction)) {}
|
| 11 |
+
|
| 12 |
+
~RegistrationHandleRAII() {
|
| 13 |
+
if (onDestruction_) {
|
| 14 |
+
onDestruction_();
|
| 15 |
+
}
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
RegistrationHandleRAII(const RegistrationHandleRAII&) = delete;
|
| 19 |
+
RegistrationHandleRAII& operator=(const RegistrationHandleRAII&) = delete;
|
| 20 |
+
|
| 21 |
+
RegistrationHandleRAII(RegistrationHandleRAII&& rhs) noexcept
|
| 22 |
+
: onDestruction_(std::move(rhs.onDestruction_)) {
|
| 23 |
+
rhs.onDestruction_ = nullptr;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
RegistrationHandleRAII& operator=(RegistrationHandleRAII&& rhs) noexcept {
|
| 27 |
+
onDestruction_ = std::move(rhs.onDestruction_);
|
| 28 |
+
rhs.onDestruction_ = nullptr;
|
| 29 |
+
return *this;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
private:
|
| 33 |
+
std::function<void()> onDestruction_;
|
| 34 |
+
};
|
| 35 |
+
|
| 36 |
+
}
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/function.h
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/core/function_schema.h>
|
| 4 |
+
#include <ATen/core/ivalue.h>
|
| 5 |
+
#include <ATen/core/qualified_name.h>
|
| 6 |
+
#include <c10/util/Exception.h>
|
| 7 |
+
#include <c10/util/FunctionRef.h>
|
| 8 |
+
|
| 9 |
+
namespace c10 {
|
| 10 |
+
struct FunctionSchema;
|
| 11 |
+
};
|
| 12 |
+
|
| 13 |
+
namespace at {
|
| 14 |
+
TORCH_API void launch(std::function<void()> func);
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
namespace torch::jit {
|
| 18 |
+
|
| 19 |
+
struct Graph;
|
| 20 |
+
struct Code;
|
| 21 |
+
|
| 22 |
+
namespace mobile {
|
| 23 |
+
struct Code;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
using Stack = std::vector<at::IValue>;
|
| 27 |
+
using Kwargs = std::unordered_map<std::string, at::IValue>;
|
| 28 |
+
struct RecursiveMethodCallError : public std::exception {};
|
| 29 |
+
using TaskLauncher = std::function<void(std::function<void()>)>;
|
| 30 |
+
|
| 31 |
+
TORCH_API void preoptimizeGraph(
|
| 32 |
+
std::shared_ptr<Graph>& graph,
|
| 33 |
+
bool disable_autocast = false);
|
| 34 |
+
|
| 35 |
+
// A Function is a pure Graph with no implicit `self` object bound.
|
| 36 |
+
// It contains schema information and the executor that manages the
|
| 37 |
+
// execution of the function. Method is a wrapper around an
|
| 38 |
+
// underlying Function that also provides a `self` object.
|
| 39 |
+
struct TORCH_API Function {
|
| 40 |
+
Function() = default;
|
| 41 |
+
Function(const Function&) = default;
|
| 42 |
+
Function& operator=(const Function&) = default;
|
| 43 |
+
Function(Function&&) noexcept = default;
|
| 44 |
+
Function& operator=(Function&&) noexcept = default;
|
| 45 |
+
virtual c10::string_view doc_string() const {
|
| 46 |
+
static constexpr c10::string_view no_doc_string = "";
|
| 47 |
+
return no_doc_string;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
virtual bool isGraphFunction() const {
|
| 51 |
+
return false;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
virtual void run(Stack& stack) = 0;
|
| 55 |
+
|
| 56 |
+
virtual c10::intrusive_ptr<c10::ivalue::Future> runAsync(
|
| 57 |
+
Stack& /*stack*/,
|
| 58 |
+
// NOLINTNEXTLINE(performance-unnecessary-value-param)
|
| 59 |
+
C10_UNUSED TaskLauncher taskLauncher = at::launch) {
|
| 60 |
+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(false);
|
| 61 |
+
return {};
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
at::IValue operator()(Stack stack, const Kwargs& kwargs = Kwargs()) {
|
| 65 |
+
getSchema().checkAndNormalizeInputs(stack, kwargs);
|
| 66 |
+
run(stack);
|
| 67 |
+
return stack.front();
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
virtual const c10::QualifiedName& qualname() const = 0;
|
| 71 |
+
|
| 72 |
+
const std::string& name() const {
|
| 73 |
+
return qualname().name();
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
// if this isn't yet defined, run its method_creator function
|
| 77 |
+
virtual void ensure_defined() = 0;
|
| 78 |
+
|
| 79 |
+
virtual const c10::FunctionSchema& getSchema() const = 0;
|
| 80 |
+
|
| 81 |
+
virtual size_t num_inputs() const = 0;
|
| 82 |
+
|
| 83 |
+
virtual Function& setSchema(c10::FunctionSchema schema) = 0;
|
| 84 |
+
|
| 85 |
+
// call() defines how different interpreter implementations interacts with
|
| 86 |
+
// Function objects. Basically interpreters need to provide a callback to
|
| 87 |
+
// communicate to Functions what to do if provided a Code object.
|
| 88 |
+
// Alternatively we could design the signature to return an optional Code
|
| 89 |
+
// object, but that requires special handling the null case in interpreter
|
| 90 |
+
// and the fallback behavior is not well defined by interpreter but rather
|
| 91 |
+
// Function themselves, so a callback approach is more reasonable than
|
| 92 |
+
// returning values.
|
| 93 |
+
// If call() returns true, then callback completes successfully, otherwise
|
| 94 |
+
// call() returns false.
|
| 95 |
+
|
| 96 |
+
// Overload for server interpreter, a bailout size is needed for graph
|
| 97 |
+
// executor.
|
| 98 |
+
virtual bool call(
|
| 99 |
+
Stack&,
|
| 100 |
+
std::optional<size_t>,
|
| 101 |
+
c10::function_ref<void(const Code&)>) {
|
| 102 |
+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(false);
|
| 103 |
+
return false;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
// Overload for mobile interpreter.
|
| 107 |
+
virtual bool call(Stack&, c10::function_ref<void(const mobile::Code&)>) {
|
| 108 |
+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(false);
|
| 109 |
+
return false;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
virtual ~Function() = default;
|
| 113 |
+
};
|
| 114 |
+
} // namespace torch::jit
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/op_registration/adaption.h
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/Tensor.h>
|
| 4 |
+
#include <ATen/TensorUtils.h>
|
| 5 |
+
#include <ATen/core/List.h>
|
| 6 |
+
#include <c10/core/TensorOptions.h>
|
| 7 |
+
|
| 8 |
+
/*
|
| 9 |
+
* [Note: hacky wrapper removal for optional tensor]
|
| 10 |
+
*
|
| 11 |
+
* The kernel implementation takes an optional tensor marked in the schema as
|
| 12 |
+
* Tensor? but the C++ function takes Tensor instead of the std::optional<Tensor>
|
| 13 |
+
* expected by the dispatcher.
|
| 14 |
+
*
|
| 15 |
+
* To remove the hacky wrapper, the C++ function is changed to take
|
| 16 |
+
* std::optional<Tensor> and unwrap the Tensor value at the beginning of
|
| 17 |
+
* the function, e.g.:
|
| 18 |
+
* > c10::MaybeOwned<Tensor> weight_maybe_owned =
|
| 19 |
+
* > at::borrow_from_optional_tensor(weight_opt);
|
| 20 |
+
* > const Tensor& weight = *weight_maybe_owned;
|
| 21 |
+
*
|
| 22 |
+
* We may want to make the kernel handle optional directly without
|
| 23 |
+
* going through the creation of a default-constructed Tensor in
|
| 24 |
+
* at::borrow_from_optional_tensor.
|
| 25 |
+
*/
|
| 26 |
+
|
| 27 |
+
/*
|
| 28 |
+
* [Note: hacky wrapper removal for TensorOptions]
|
| 29 |
+
*
|
| 30 |
+
* The kernel implementation takes a TensorOptions argument but the dispatcher
|
| 31 |
+
* expects separate arguments for dtype, layout, device, pin_memory.
|
| 32 |
+
*
|
| 33 |
+
* To remove the hacky wrapper, the kernel implementation is changed to take
|
| 34 |
+
* the 4 arguments (dtype, layout, device, pin_memory), and assemble the
|
| 35 |
+
* TensorOptions value at the beginning of the function, e.g.:
|
| 36 |
+
* > TensorOptions options = TensorOptions().dtype(dtype).layout(layout)
|
| 37 |
+
* > .device(device).pinned_memory(pin_memory);
|
| 38 |
+
*
|
| 39 |
+
* We may want make the kernel handle these parameters directly without going
|
| 40 |
+
* through the creation of a TensorOptions value.
|
| 41 |
+
*/
|
| 42 |
+
|
| 43 |
+
namespace c10 {
|
| 44 |
+
namespace impl {
|
| 45 |
+
|
| 46 |
+
TORCH_API void common_device_check_failure(Device common_device, const at::Tensor& tensor, at::CheckedFrom methodName, at::CheckedFrom argName);
|
| 47 |
+
|
| 48 |
+
inline void check_and_update_common_device(std::optional<Device>& common_device, const at::Tensor& tensor, at::CheckedFrom methodName, at::CheckedFrom argName) {
|
| 49 |
+
// TODO: Remove this once the following issue is addressed:
|
| 50 |
+
// https://github.com/pytorch/pytorch/issues/57380
|
| 51 |
+
if (!tensor.defined()) {
|
| 52 |
+
return;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
if (!common_device.has_value()) {
|
| 56 |
+
common_device = tensor.device();
|
| 57 |
+
return;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
if (C10_UNLIKELY(common_device != tensor.device())) {
|
| 61 |
+
common_device_check_failure(*common_device, tensor, methodName, argName);
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
inline void check_and_update_common_device(std::optional<Device>& common_device, const std::optional<at::Tensor>& tensor, at::CheckedFrom methodName, at::CheckedFrom argName) {
|
| 66 |
+
if (tensor.has_value()) {
|
| 67 |
+
check_and_update_common_device(common_device, tensor.value(), methodName, argName);
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
inline void check_and_update_common_device(std::optional<Device>& common_device, at::ITensorListRef tensors, at::CheckedFrom methodName, at::CheckedFrom argName) {
|
| 72 |
+
for (const auto& tensor : tensors) {
|
| 73 |
+
check_and_update_common_device(common_device, tensor, methodName, argName);
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
inline void check_and_update_common_device(std::optional<Device>& common_device, const List<std::optional<at::Tensor>>& tensors, at::CheckedFrom methodName, at::CheckedFrom argName) {
|
| 78 |
+
for (const auto& tensor : tensors) {
|
| 79 |
+
check_and_update_common_device(common_device, tensor, methodName, argName);
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
} // namespace impl
|
| 83 |
+
} // namespace c10
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/type_factory.h
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <type_traits>
|
| 4 |
+
#include <unordered_map>
|
| 5 |
+
|
| 6 |
+
#include <ATen/core/dynamic_type.h>
|
| 7 |
+
#include <ATen/core/jit_type_base.h>
|
| 8 |
+
#include <c10/macros/Macros.h>
|
| 9 |
+
|
| 10 |
+
namespace c10 {
|
| 11 |
+
|
| 12 |
+
template <typename T>
|
| 13 |
+
struct TORCH_API TypeFactoryBase {};
|
| 14 |
+
|
| 15 |
+
template <>
|
| 16 |
+
struct TORCH_API TypeFactoryBase<c10::DynamicType> {
|
| 17 |
+
template <typename T, typename... Args>
|
| 18 |
+
static c10::DynamicTypePtr create(TypePtr ty, Args&&... args) {
|
| 19 |
+
return std::make_shared<c10::DynamicType>(
|
| 20 |
+
c10::DynamicTypeTrait<T>::tagValue(),
|
| 21 |
+
c10::DynamicType::Arguments(c10::ArrayRef<c10::TypePtr>(
|
| 22 |
+
{std::move(ty), std::forward<Args>(args)...})));
|
| 23 |
+
}
|
| 24 |
+
template <typename T>
|
| 25 |
+
static c10::DynamicTypePtr create(const std::vector<c10::TypePtr>& types) {
|
| 26 |
+
return std::make_shared<c10::DynamicType>(
|
| 27 |
+
c10::DynamicTypeTrait<T>::tagValue(),
|
| 28 |
+
c10::DynamicType::Arguments(types));
|
| 29 |
+
}
|
| 30 |
+
static c10::DynamicTypePtr createNamedTuple(
|
| 31 |
+
const std::string& name,
|
| 32 |
+
const std::vector<c10::string_view>& fields,
|
| 33 |
+
const std::vector<c10::TypePtr>& types) {
|
| 34 |
+
return std::make_shared<c10::DynamicType>(
|
| 35 |
+
c10::DynamicType::Tag::Tuple,
|
| 36 |
+
name,
|
| 37 |
+
c10::DynamicType::Arguments(fields, types));
|
| 38 |
+
}
|
| 39 |
+
template <typename T>
|
| 40 |
+
C10_ERASE static c10::DynamicTypePtr createNamed(const std::string& name) {
|
| 41 |
+
return std::make_shared<c10::DynamicType>(
|
| 42 |
+
c10::DynamicTypeTrait<T>::tagValue(),
|
| 43 |
+
name,
|
| 44 |
+
c10::DynamicType::Arguments{});
|
| 45 |
+
}
|
| 46 |
+
template <typename T>
|
| 47 |
+
C10_ERASE static c10::DynamicTypePtr get() {
|
| 48 |
+
return DynamicTypeTrait<T>::getBaseType();
|
| 49 |
+
}
|
| 50 |
+
static const std::unordered_map<std::string, c10::TypePtr>& basePythonTypes();
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
using DynamicTypeFactory = TypeFactoryBase<c10::DynamicType>;
|
| 54 |
+
|
| 55 |
+
// Helper functions for constructing DynamicTypes inline.
|
| 56 |
+
template <
|
| 57 |
+
typename T,
|
| 58 |
+
std::enable_if_t<DynamicTypeTrait<T>::isBaseType, int> = 0>
|
| 59 |
+
C10_ERASE DynamicTypePtr dynT() {
|
| 60 |
+
return DynamicTypeFactory::get<T>();
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
template <
|
| 64 |
+
typename T,
|
| 65 |
+
typename... Args,
|
| 66 |
+
std::enable_if_t<!DynamicTypeTrait<T>::isBaseType, int> = 0>
|
| 67 |
+
C10_ERASE DynamicTypePtr dynT(Args&&... args) {
|
| 68 |
+
return DynamicTypeFactory::create<T>(std::forward<Args>(args)...);
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
template <>
|
| 72 |
+
struct TORCH_API TypeFactoryBase<c10::Type> {
|
| 73 |
+
template <typename T, typename... Args>
|
| 74 |
+
static c10::TypePtr create(TypePtr ty, Args&&... args) {
|
| 75 |
+
return T::create(std::move(ty), std::forward<Args>(args)...);
|
| 76 |
+
}
|
| 77 |
+
template <typename T>
|
| 78 |
+
static c10::TypePtr create(std::vector<c10::TypePtr> types) {
|
| 79 |
+
return T::create(std::move(types));
|
| 80 |
+
}
|
| 81 |
+
static c10::TypePtr createNamedTuple(
|
| 82 |
+
const std::string& name,
|
| 83 |
+
const std::vector<c10::string_view>& fields,
|
| 84 |
+
const std::vector<c10::TypePtr>& types);
|
| 85 |
+
template <typename T>
|
| 86 |
+
C10_ERASE static c10::TypePtr createNamed(const std::string& name) {
|
| 87 |
+
return T::create(name);
|
| 88 |
+
}
|
| 89 |
+
static const std::unordered_map<std::string, c10::TypePtr>& basePythonTypes();
|
| 90 |
+
template <typename T>
|
| 91 |
+
C10_ERASE static c10::TypePtr get() {
|
| 92 |
+
return T::get();
|
| 93 |
+
}
|
| 94 |
+
};
|
| 95 |
+
|
| 96 |
+
using DefaultTypeFactory = TypeFactoryBase<c10::Type>;
|
| 97 |
+
|
| 98 |
+
using PlatformType =
|
| 99 |
+
#ifdef C10_MOBILE
|
| 100 |
+
c10::DynamicType
|
| 101 |
+
#else
|
| 102 |
+
c10::Type
|
| 103 |
+
#endif
|
| 104 |
+
;
|
| 105 |
+
|
| 106 |
+
using TypeFactory = TypeFactoryBase<PlatformType>;
|
| 107 |
+
|
| 108 |
+
} // namespace c10
|
vllm/lib/python3.10/site-packages/torch/include/ATen/core/type_ptr.h
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <memory>
|
| 4 |
+
#include <type_traits>
|
| 5 |
+
|
| 6 |
+
#include <c10/util/Exception.h>
|
| 7 |
+
#include <c10/util/MaybeOwned.h>
|
| 8 |
+
|
| 9 |
+
namespace c10 {
|
| 10 |
+
|
| 11 |
+
// Compatibility wrapper around a raw pointer so that existing code
|
| 12 |
+
// written to deal with a shared_ptr can keep working.
|
| 13 |
+
template <typename T>
|
| 14 |
+
class SingletonTypePtr {
|
| 15 |
+
public:
|
| 16 |
+
/* implicit */ SingletonTypePtr(T* p) : repr_(p) {}
|
| 17 |
+
|
| 18 |
+
// We need this to satisfy Pybind11, but it shouldn't be hit.
|
| 19 |
+
explicit SingletonTypePtr(std::shared_ptr<T>) { TORCH_CHECK(false); }
|
| 20 |
+
|
| 21 |
+
using element_type = typename std::shared_ptr<T>::element_type;
|
| 22 |
+
|
| 23 |
+
template <typename U = T, std::enable_if_t<!std::is_same_v<std::remove_const_t<U>, void>, bool> = true>
|
| 24 |
+
T& operator*() const {
|
| 25 |
+
return *repr_;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
T* get() const {
|
| 29 |
+
return repr_;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
T* operator->() const {
|
| 33 |
+
return repr_;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
operator bool() const {
|
| 37 |
+
return repr_ != nullptr;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
private:
|
| 41 |
+
T* repr_{nullptr};
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
template <typename T, typename U>
|
| 45 |
+
bool operator==(SingletonTypePtr<T> lhs, SingletonTypePtr<U> rhs) {
|
| 46 |
+
return (void*)lhs.get() == (void*)rhs.get();
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
template <typename T, typename U>
|
| 50 |
+
bool operator!=(SingletonTypePtr<T> lhs, SingletonTypePtr<U> rhs) {
|
| 51 |
+
return !(lhs == rhs);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
} // namespace c10
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/functional.h
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/cpu/vec/functional_base.h>
|
| 4 |
+
#include <ATen/cpu/vec/functional_bfloat16.h>
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/functional_base.h
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// DO NOT DEFINE STATIC DATA IN THIS HEADER!
|
| 4 |
+
// See Note [Do not compile initializers with AVX]
|
| 5 |
+
|
| 6 |
+
#include <ATen/cpu/vec/vec.h>
|
| 7 |
+
#include <c10/util/irange.h>
|
| 8 |
+
|
| 9 |
+
namespace at::vec {
|
| 10 |
+
|
| 11 |
+
// slow path
|
| 12 |
+
template <typename scalar_t, typename Op>
|
| 13 |
+
inline scalar_t vec_reduce_all(
|
| 14 |
+
const Op& vec_fun,
|
| 15 |
+
vec::Vectorized<scalar_t> acc_vec,
|
| 16 |
+
int64_t size) {
|
| 17 |
+
using Vec = vec::Vectorized<scalar_t>;
|
| 18 |
+
scalar_t acc_arr[Vec::size()];
|
| 19 |
+
acc_vec.store(acc_arr);
|
| 20 |
+
for (const auto i : c10::irange(1, size)) {
|
| 21 |
+
std::array<scalar_t, Vec::size()> acc_arr_next = {0};
|
| 22 |
+
acc_arr_next[0] = acc_arr[i];
|
| 23 |
+
Vec acc_vec_next = Vec::loadu(acc_arr_next.data());
|
| 24 |
+
acc_vec = vec_fun(acc_vec, acc_vec_next);
|
| 25 |
+
}
|
| 26 |
+
acc_vec.store(acc_arr);
|
| 27 |
+
return acc_arr[0];
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
template <typename scalar_t, typename Op>
|
| 31 |
+
struct VecReduceAllSIMD {
|
| 32 |
+
static inline scalar_t apply(const Op& vec_fun, const Vectorized<scalar_t>& acc_vec) {
|
| 33 |
+
return vec_reduce_all(vec_fun, acc_vec, Vectorized<scalar_t>::size());
|
| 34 |
+
}
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
#if defined(__GNUC__) && (__GNUC__ > 5) && !defined(_MSC_VER) && !defined(C10_MOBILE)
|
| 38 |
+
#if defined(CPU_CAPABILITY_AVX2)
|
| 39 |
+
template <typename Op>
|
| 40 |
+
struct VecReduceAllSIMD<float, Op> {
|
| 41 |
+
static inline float apply(const Op& vec_fun, const Vectorized<float>& acc_vec) {
|
| 42 |
+
using Vec = Vectorized<float>;
|
| 43 |
+
Vec v = acc_vec;
|
| 44 |
+
// 128-bit shuffle
|
| 45 |
+
Vec v1 = _mm256_permute2f128_ps(v, v, 0x1);
|
| 46 |
+
v = vec_fun(v, v1);
|
| 47 |
+
// 64-bit shuffle
|
| 48 |
+
v1 = _mm256_shuffle_ps(v, v, 0x4E);
|
| 49 |
+
v = vec_fun(v, v1);
|
| 50 |
+
// 32-bit shuffle
|
| 51 |
+
v1 = _mm256_shuffle_ps(v, v, 0xB1);
|
| 52 |
+
v = vec_fun(v, v1);
|
| 53 |
+
return _mm256_cvtss_f32(v);
|
| 54 |
+
}
|
| 55 |
+
};
|
| 56 |
+
#endif // defined(CPU_CAPABILITY_AVX2)
|
| 57 |
+
#if defined(CPU_CAPABILITY_AVX512)
|
| 58 |
+
template <typename Op>
|
| 59 |
+
struct VecReduceAllSIMD<float, Op> {
|
| 60 |
+
static inline float apply(const Op& vec_fun, const Vectorized<float>& acc_vec) {
|
| 61 |
+
using Vec = Vectorized<float>;
|
| 62 |
+
Vec v = acc_vec;
|
| 63 |
+
// 256-bit shuffle
|
| 64 |
+
Vec v1 = _mm512_shuffle_f32x4(v, v, 0x4E);
|
| 65 |
+
v = vec_fun(v, v1);
|
| 66 |
+
// 128-bit shuffle
|
| 67 |
+
v1 = _mm512_shuffle_f32x4(v, v, 0xB1);
|
| 68 |
+
v = vec_fun(v, v1);
|
| 69 |
+
// 64-bit shuffle
|
| 70 |
+
v1 = _mm512_shuffle_ps(v, v, 0x4E);
|
| 71 |
+
v = vec_fun(v, v1);
|
| 72 |
+
// 32-bit shuffle
|
| 73 |
+
v1 = _mm512_shuffle_ps(v, v, 0xB1);
|
| 74 |
+
v = vec_fun(v, v1);
|
| 75 |
+
return _mm512_cvtss_f32(v);
|
| 76 |
+
}
|
| 77 |
+
};
|
| 78 |
+
#endif // defined(CPU_CAPABILITY_AVX512)
|
| 79 |
+
#endif // defined(__GNUC__) && (__GNUC__ > 5) && !defined(_MSC_VER) && !defined(C10_MOBILE)
|
| 80 |
+
|
| 81 |
+
#if defined(__aarch64__) && !defined(C10_MOBILE) && !defined(__CUDACC__)
|
| 82 |
+
template <typename Op>
|
| 83 |
+
struct VecReduceAllSIMD<float, Op> {
|
| 84 |
+
static inline float apply(const Op& vec_fun, const Vectorized<float>& acc_vec) {
|
| 85 |
+
using Vec = Vectorized<float>;
|
| 86 |
+
Vec v = acc_vec;
|
| 87 |
+
|
| 88 |
+
// 128-bit shuffle: [a1, a2, a3, a4, a5, a6, a7, a8] -> [a5, a6, a7, a8, a1, a2, a3, a4]
|
| 89 |
+
Vec v1 = {v.get_high(), v.get_low()};
|
| 90 |
+
// [a1+a5, a2+a6, a3+a7, a4+a8, -, -, -, -] ('+' stands for the reduction function. Note that the last 4 elements are not required)
|
| 91 |
+
v = vec_fun(v, v1);
|
| 92 |
+
|
| 93 |
+
// 64-bit shuffle: [a1+a5, a2+a6, a3+a7, a4+a8, -, -, -, -] -> [a3+a7, a4+a8, a1+a5, a2+a6, -, -, -, -]
|
| 94 |
+
float32x4_t v1_1 = vextq_f32(v.get_low(), v.get_low(), 2);
|
| 95 |
+
v1 = {v1_1, v1_1};
|
| 96 |
+
// [a1+a3+a5+a7, a2+a4+a6+a8, a1+a3+a5+a7, a2+a4+a6+a8, -, -, -, -]
|
| 97 |
+
v = vec_fun(v, v1);
|
| 98 |
+
|
| 99 |
+
// 32-bit shuffle: [a1+a3+a5+a7, a2+a4+a6+a8, a1+a3+a5+a7, a2+a4+a6+a8, -, -, -, -] -> [a2+a4+a6+a8, a1+a3+a5+a7, a2+a4+a6+a8, a1+a3+a5+a7, -, -, -, -]
|
| 100 |
+
v1_1 = vrev64q_f32(v.get_low());
|
| 101 |
+
v1 = {v1_1, v1_1};
|
| 102 |
+
// [a1+a2+a3+a4+a5+a6+a7+a8, a1+a2+a3+a4+a5+a6+a7+a8, a1+a2+a3+a4+a5+a6+a7+a8, a1+a2+a3+a4+a5+a6+a7+a8, -, -, -, -]
|
| 103 |
+
v = vec_fun(v, v1);
|
| 104 |
+
|
| 105 |
+
return v.get_low()[0];
|
| 106 |
+
}
|
| 107 |
+
};
|
| 108 |
+
#endif // defined(__aarch64__)
|
| 109 |
+
|
| 110 |
+
template <typename scalar_t, typename Op>
|
| 111 |
+
inline scalar_t vec_reduce_all(const Op& vec_fun, const Vectorized<scalar_t>& acc_vec) {
|
| 112 |
+
return VecReduceAllSIMD<scalar_t, Op>::apply(vec_fun, acc_vec);
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
template <typename scalar_t, typename Op,
|
| 116 |
+
typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
|
| 117 |
+
inline scalar_t reduce_all(const Op& vec_fun, const scalar_t* data, int64_t size) {
|
| 118 |
+
using Vec = vec::Vectorized<scalar_t>;
|
| 119 |
+
if (size < Vec::size())
|
| 120 |
+
return vec_reduce_all(vec_fun, Vec::loadu(data, size), size);
|
| 121 |
+
int64_t d = Vec::size();
|
| 122 |
+
Vec acc_vec = Vec::loadu(data);
|
| 123 |
+
for (; d < size - (size % Vec::size()); d += Vec::size()) {
|
| 124 |
+
Vec data_vec = Vec::loadu(data + d);
|
| 125 |
+
acc_vec = vec_fun(acc_vec, data_vec);
|
| 126 |
+
}
|
| 127 |
+
if (size - d > 0) {
|
| 128 |
+
Vec data_vec = Vec::loadu(data + d, size - d);
|
| 129 |
+
acc_vec = Vec::set(acc_vec, vec_fun(acc_vec, data_vec), size - d);
|
| 130 |
+
}
|
| 131 |
+
return vec_reduce_all(vec_fun, acc_vec);
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
// similar to reduce_all, but reduces into two outputs
|
| 135 |
+
template <typename scalar_t, typename Op1, typename Op2,
|
| 136 |
+
typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
|
| 137 |
+
inline std::pair<scalar_t, scalar_t> reduce2_all(const Op1& vec_fun1, const Op2& vec_fun2,
|
| 138 |
+
const scalar_t* data, int64_t size) {
|
| 139 |
+
using Vec = vec::Vectorized<scalar_t>;
|
| 140 |
+
if (size < Vec::size()) {
|
| 141 |
+
auto loaded_data = Vec::loadu(data, size);
|
| 142 |
+
return std::pair<scalar_t, scalar_t>(
|
| 143 |
+
vec_reduce_all(vec_fun1, loaded_data, size),
|
| 144 |
+
vec_reduce_all(vec_fun2, loaded_data, size));
|
| 145 |
+
}
|
| 146 |
+
int64_t d = Vec::size();
|
| 147 |
+
Vec acc_vec1 = Vec::loadu(data);
|
| 148 |
+
Vec acc_vec2 = Vec::loadu(data);
|
| 149 |
+
for (; d < size - (size % Vec::size()); d += Vec::size()) {
|
| 150 |
+
Vec data_vec = Vec::loadu(data + d);
|
| 151 |
+
acc_vec1 = vec_fun1(acc_vec1, data_vec);
|
| 152 |
+
acc_vec2 = vec_fun2(acc_vec2, data_vec);
|
| 153 |
+
}
|
| 154 |
+
if (size - d > 0) {
|
| 155 |
+
Vec data_vec = Vec::loadu(data + d, size - d);
|
| 156 |
+
acc_vec1 = Vec::set(acc_vec1, vec_fun1(acc_vec1, data_vec), size - d);
|
| 157 |
+
acc_vec2 = Vec::set(acc_vec2, vec_fun2(acc_vec2, data_vec), size - d);
|
| 158 |
+
}
|
| 159 |
+
return std::pair<scalar_t, scalar_t>(
|
| 160 |
+
vec_reduce_all(vec_fun1, acc_vec1),
|
| 161 |
+
vec_reduce_all(vec_fun2, acc_vec2));
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
template <typename scalar_t, typename MapOp, typename ReduceOp,
|
| 165 |
+
typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
|
| 166 |
+
inline scalar_t map_reduce_all(
|
| 167 |
+
const MapOp& map_fun,
|
| 168 |
+
const ReduceOp& red_fun,
|
| 169 |
+
const scalar_t* data,
|
| 170 |
+
int64_t size) {
|
| 171 |
+
using Vec = vec::Vectorized<scalar_t>;
|
| 172 |
+
if (size < Vec::size())
|
| 173 |
+
return vec_reduce_all(red_fun, map_fun(Vec::loadu(data, size)), size);
|
| 174 |
+
int64_t d = Vec::size();
|
| 175 |
+
Vec acc_vec = map_fun(Vec::loadu(data));
|
| 176 |
+
for (; d < size - (size % Vec::size()); d += Vec::size()) {
|
| 177 |
+
Vec data_vec = Vec::loadu(data + d);
|
| 178 |
+
data_vec = map_fun(data_vec);
|
| 179 |
+
acc_vec = red_fun(acc_vec, data_vec);
|
| 180 |
+
}
|
| 181 |
+
if (size - d > 0) {
|
| 182 |
+
Vec data_vec = Vec::loadu(data + d, size - d);
|
| 183 |
+
data_vec = map_fun(data_vec);
|
| 184 |
+
acc_vec = Vec::set(acc_vec, red_fun(acc_vec, data_vec), size - d);
|
| 185 |
+
}
|
| 186 |
+
return vec_reduce_all(red_fun, acc_vec);
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
template <typename scalar_t, typename MapOp, typename ReduceOp,
|
| 190 |
+
typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
|
| 191 |
+
inline scalar_t map2_reduce_all(
|
| 192 |
+
const MapOp& map_fun,
|
| 193 |
+
const ReduceOp& red_fun,
|
| 194 |
+
const scalar_t* data,
|
| 195 |
+
const scalar_t* data2,
|
| 196 |
+
int64_t size) {
|
| 197 |
+
using Vec = vec::Vectorized<scalar_t>;
|
| 198 |
+
if (size < Vec::size()) {
|
| 199 |
+
Vec data_vec = Vec::loadu(data, size);
|
| 200 |
+
Vec data2_vec = Vec::loadu(data2, size);
|
| 201 |
+
data_vec = map_fun(data_vec, data2_vec);
|
| 202 |
+
return vec_reduce_all(red_fun, data_vec, size);
|
| 203 |
+
}
|
| 204 |
+
int64_t d = Vec::size();
|
| 205 |
+
Vec acc_vec = map_fun(Vec::loadu(data), Vec::loadu(data2));
|
| 206 |
+
for (; d < size - (size % Vec::size()); d += Vec::size()) {
|
| 207 |
+
Vec data_vec = Vec::loadu(data + d);
|
| 208 |
+
Vec data2_vec = Vec::loadu(data2 + d);
|
| 209 |
+
data_vec = map_fun(data_vec, data2_vec);
|
| 210 |
+
acc_vec = red_fun(acc_vec, data_vec);
|
| 211 |
+
}
|
| 212 |
+
if (size - d > 0) {
|
| 213 |
+
Vec data_vec = Vec::loadu(data + d, size - d);
|
| 214 |
+
Vec data2_vec = Vec::loadu(data2 + d, size - d);
|
| 215 |
+
data_vec = map_fun(data_vec, data2_vec);
|
| 216 |
+
acc_vec = Vec::set(acc_vec, red_fun(acc_vec, data_vec), size - d);
|
| 217 |
+
}
|
| 218 |
+
return vec_reduce_all(red_fun, acc_vec);
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
template <typename scalar_t, typename MapOp, typename ReduceOp,
|
| 222 |
+
typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
|
| 223 |
+
inline scalar_t map3_reduce_all(
|
| 224 |
+
const MapOp& map_fun,
|
| 225 |
+
const ReduceOp& red_fun,
|
| 226 |
+
const scalar_t* data,
|
| 227 |
+
const scalar_t* data2,
|
| 228 |
+
const scalar_t* data3,
|
| 229 |
+
int64_t size) {
|
| 230 |
+
using Vec = vec::Vectorized<scalar_t>;
|
| 231 |
+
if (size < Vec::size()) {
|
| 232 |
+
Vec data_vec = Vec::loadu(data, size);
|
| 233 |
+
Vec data2_vec = Vec::loadu(data2, size);
|
| 234 |
+
Vec data3_vec = Vec::loadu(data3, size);
|
| 235 |
+
data_vec = map_fun(data_vec, data2_vec, data3_vec);
|
| 236 |
+
return vec_reduce_all(red_fun, data_vec, size);
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
int64_t d = Vec::size();
|
| 240 |
+
Vec acc_vec = map_fun(Vec::loadu(data), Vec::loadu(data2), Vec::loadu(data3));
|
| 241 |
+
for (; d < size - (size % Vec::size()); d += Vec::size()) {
|
| 242 |
+
Vec data_vec = Vec::loadu(data + d);
|
| 243 |
+
Vec data2_vec = Vec::loadu(data2 + d);
|
| 244 |
+
Vec data3_vec = Vec::loadu(data3 + d);
|
| 245 |
+
data_vec = map_fun(data_vec, data2_vec, data3_vec);
|
| 246 |
+
acc_vec = red_fun(acc_vec, data_vec);
|
| 247 |
+
}
|
| 248 |
+
if (size - d > 0) {
|
| 249 |
+
Vec data_vec = Vec::loadu(data + d, size - d);
|
| 250 |
+
Vec data2_vec = Vec::loadu(data2 + d, size - d);
|
| 251 |
+
Vec data3_vec = Vec::loadu(data3 + d, size - d);
|
| 252 |
+
data_vec = map_fun(data_vec, data2_vec, data3_vec);
|
| 253 |
+
acc_vec = Vec::set(acc_vec, red_fun(acc_vec, data_vec), size - d);
|
| 254 |
+
}
|
| 255 |
+
return vec_reduce_all(red_fun, acc_vec);
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
template <typename scalar_t, typename Op,
|
| 259 |
+
typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
|
| 260 |
+
inline void map(
|
| 261 |
+
const Op& vec_fun,
|
| 262 |
+
scalar_t* output_data,
|
| 263 |
+
const scalar_t* input_data,
|
| 264 |
+
int64_t size) {
|
| 265 |
+
using Vec = vec::Vectorized<scalar_t>;
|
| 266 |
+
int64_t d = 0;
|
| 267 |
+
for (; d < size - (size % Vec::size()); d += Vec::size()) {
|
| 268 |
+
Vec output_vec = vec_fun(Vec::loadu(input_data + d));
|
| 269 |
+
output_vec.store(output_data + d);
|
| 270 |
+
}
|
| 271 |
+
if (size - d > 0) {
|
| 272 |
+
Vec output_vec = vec_fun(Vec::loadu(input_data + d, size - d));
|
| 273 |
+
output_vec.store(output_data + d, size - d);
|
| 274 |
+
}
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
template <typename scalar_t, typename Op,
|
| 278 |
+
typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
|
| 279 |
+
inline void map2(
|
| 280 |
+
const Op& vec_fun,
|
| 281 |
+
scalar_t* output_data,
|
| 282 |
+
const scalar_t* input_data,
|
| 283 |
+
const scalar_t* input_data2,
|
| 284 |
+
int64_t size) {
|
| 285 |
+
using Vec = vec::Vectorized<scalar_t>;
|
| 286 |
+
int64_t d = 0;
|
| 287 |
+
for (; d < size - (size % Vec::size()); d += Vec::size()) {
|
| 288 |
+
Vec data_vec = Vec::loadu(input_data + d);
|
| 289 |
+
Vec data_vec2 = Vec::loadu(input_data2 + d);
|
| 290 |
+
Vec output_vec = vec_fun(data_vec, data_vec2);
|
| 291 |
+
output_vec.store(output_data + d);
|
| 292 |
+
}
|
| 293 |
+
if (size - d > 0) {
|
| 294 |
+
Vec data_vec = Vec::loadu(input_data + d, size - d);
|
| 295 |
+
Vec data_vec2 = Vec::loadu(input_data2 + d, size - d);
|
| 296 |
+
Vec output_vec = vec_fun(data_vec, data_vec2);
|
| 297 |
+
output_vec.store(output_data + d, size - d);
|
| 298 |
+
}
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
template <typename scalar_t, typename Op,
|
| 302 |
+
typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
|
| 303 |
+
inline void map3(
|
| 304 |
+
const Op& vec_fun,
|
| 305 |
+
scalar_t* output_data,
|
| 306 |
+
const scalar_t* input_data1,
|
| 307 |
+
const scalar_t* input_data2,
|
| 308 |
+
const scalar_t* input_data3,
|
| 309 |
+
int64_t size) {
|
| 310 |
+
using Vec = vec::Vectorized<scalar_t>;
|
| 311 |
+
int64_t d = 0;
|
| 312 |
+
for (; d < size - (size % Vec::size()); d += Vec::size()) {
|
| 313 |
+
Vec data_vec1 = Vec::loadu(input_data1 + d);
|
| 314 |
+
Vec data_vec2 = Vec::loadu(input_data2 + d);
|
| 315 |
+
Vec data_vec3 = Vec::loadu(input_data3 + d);
|
| 316 |
+
Vec output_vec = vec_fun(data_vec1, data_vec2, data_vec3);
|
| 317 |
+
output_vec.store(output_data + d);
|
| 318 |
+
}
|
| 319 |
+
if (size - d > 0) {
|
| 320 |
+
Vec data_vec1 = Vec::loadu(input_data1 + d, size - d);
|
| 321 |
+
Vec data_vec2 = Vec::loadu(input_data2 + d, size - d);
|
| 322 |
+
Vec data_vec3 = Vec::loadu(input_data3 + d, size - d);
|
| 323 |
+
Vec output_vec = vec_fun(data_vec1, data_vec2, data_vec3);
|
| 324 |
+
output_vec.store(output_data + d, size - d);
|
| 325 |
+
}
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
template <typename scalar_t, typename Op,
|
| 329 |
+
typename std::enable_if_t<!is_reduced_floating_point_v<scalar_t>, int> = 0>
|
| 330 |
+
inline void map4(
|
| 331 |
+
const Op& vec_fun,
|
| 332 |
+
scalar_t* output_data,
|
| 333 |
+
const scalar_t* input_data1,
|
| 334 |
+
const scalar_t* input_data2,
|
| 335 |
+
const scalar_t* input_data3,
|
| 336 |
+
const scalar_t* input_data4,
|
| 337 |
+
int64_t size) {
|
| 338 |
+
using Vec = vec::Vectorized<scalar_t>;
|
| 339 |
+
int64_t d = 0;
|
| 340 |
+
for (; d < size - (size % Vec::size()); d += Vec::size()) {
|
| 341 |
+
Vec data_vec1 = Vec::loadu(input_data1 + d);
|
| 342 |
+
Vec data_vec2 = Vec::loadu(input_data2 + d);
|
| 343 |
+
Vec data_vec3 = Vec::loadu(input_data3 + d);
|
| 344 |
+
Vec data_vec4 = Vec::loadu(input_data4 + d);
|
| 345 |
+
Vec output_vec = vec_fun(data_vec1, data_vec2, data_vec3, data_vec4);
|
| 346 |
+
output_vec.store(output_data + d);
|
| 347 |
+
}
|
| 348 |
+
if (size - d > 0) {
|
| 349 |
+
Vec data_vec1 = Vec::loadu(input_data1 + d, size - d);
|
| 350 |
+
Vec data_vec2 = Vec::loadu(input_data2 + d, size - d);
|
| 351 |
+
Vec data_vec3 = Vec::loadu(input_data3 + d, size - d);
|
| 352 |
+
Vec data_vec4 = Vec::loadu(input_data4 + d, size - d);
|
| 353 |
+
Vec output_vec = vec_fun(data_vec1, data_vec2, data_vec3, data_vec4);
|
| 354 |
+
output_vec.store(output_data + d, size - d);
|
| 355 |
+
}
|
| 356 |
+
}
|
| 357 |
+
|
| 358 |
+
} // namespace at::vec
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec.h
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#if defined(CPU_CAPABILITY_AVX512)
|
| 4 |
+
#include <ATen/cpu/vec/vec512/vec512.h>
|
| 5 |
+
#else
|
| 6 |
+
#include <ATen/cpu/vec/vec256/vec256.h>
|
| 7 |
+
#endif
|
| 8 |
+
|
| 9 |
+
namespace at::vec {
|
| 10 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 11 |
+
inline namespace CPU_CAPABILITY {
|
| 12 |
+
|
| 13 |
+
inline Vectorized<bool> convert_to_bool(Vectorized<int8_t> x) {
|
| 14 |
+
__at_align__ bool buffer[x.size()];
|
| 15 |
+
x.ne(Vectorized<int8_t>(0)).store(buffer);
|
| 16 |
+
|
| 17 |
+
Vectorized<bool> ret;
|
| 18 |
+
static_assert(x.size() == ret.size());
|
| 19 |
+
std::memcpy(ret, buffer, ret.size() * sizeof(bool));
|
| 20 |
+
return ret;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
template <>
|
| 24 |
+
inline Vectorized<bool> Vectorized<bool>::loadu(const void* ptr) {
|
| 25 |
+
// See NOTE [Loading boolean values]
|
| 26 |
+
return convert_to_bool(Vectorized<int8_t>::loadu(ptr));
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
template <>
|
| 30 |
+
inline Vectorized<bool> Vectorized<bool>::loadu(const void* ptr, int64_t count) {
|
| 31 |
+
// See NOTE [Loading boolean values]
|
| 32 |
+
return convert_to_bool(Vectorized<int8_t>::loadu(ptr, count));
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
template <typename VT>
|
| 36 |
+
struct VecHoldType { using hold_type = typename VT::value_type; };
|
| 37 |
+
|
| 38 |
+
template <>
|
| 39 |
+
struct VecHoldType<Vectorized<BFloat16>> { using hold_type = BFloat16; };
|
| 40 |
+
|
| 41 |
+
template <>
|
| 42 |
+
struct VecHoldType<Vectorized<Half>> {using hold_type = Half; };
|
| 43 |
+
|
| 44 |
+
template <typename VT>
|
| 45 |
+
using vechold_type = typename VecHoldType<VT>::hold_type;
|
| 46 |
+
|
| 47 |
+
}} // namespace at::vec::CPU_CAPABILITY
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vec256_complex_double.h
ADDED
|
@@ -0,0 +1,432 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// DO NOT DEFINE STATIC DATA IN THIS HEADER!
|
| 4 |
+
// See Note [Do not compile initializers with AVX]
|
| 5 |
+
|
| 6 |
+
#include <c10/util/complex.h>
|
| 7 |
+
#include <c10/util/irange.h>
|
| 8 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 9 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 10 |
+
|
| 11 |
+
#if defined(CPU_CAPABILITY_AVX2)
|
| 12 |
+
#define SLEEF_STATIC_LIBS
|
| 13 |
+
#include <sleef.h>
|
| 14 |
+
#endif
|
| 15 |
+
|
| 16 |
+
namespace at::vec {
|
| 17 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 18 |
+
inline namespace CPU_CAPABILITY {
|
| 19 |
+
|
| 20 |
+
#if defined(CPU_CAPABILITY_AVX2)
|
| 21 |
+
|
| 22 |
+
template <> class Vectorized<c10::complex<double>> {
|
| 23 |
+
private:
|
| 24 |
+
__m256d values;
|
| 25 |
+
public:
|
| 26 |
+
using value_type = c10::complex<double>;
|
| 27 |
+
using size_type = int;
|
| 28 |
+
static constexpr size_type size() {
|
| 29 |
+
return 2;
|
| 30 |
+
}
|
| 31 |
+
Vectorized() {}
|
| 32 |
+
Vectorized(__m256d v) : values(v) {}
|
| 33 |
+
Vectorized(c10::complex<double> val) {
|
| 34 |
+
double real_value = val.real();
|
| 35 |
+
double imag_value = val.imag();
|
| 36 |
+
values = _mm256_setr_pd(real_value, imag_value,
|
| 37 |
+
real_value, imag_value);
|
| 38 |
+
}
|
| 39 |
+
Vectorized(c10::complex<double> val1, c10::complex<double> val2) {
|
| 40 |
+
values = _mm256_setr_pd(val1.real(), val1.imag(),
|
| 41 |
+
val2.real(), val2.imag());
|
| 42 |
+
}
|
| 43 |
+
operator __m256d() const {
|
| 44 |
+
return values;
|
| 45 |
+
}
|
| 46 |
+
template <int64_t mask>
|
| 47 |
+
static Vectorized<c10::complex<double>> blend(const Vectorized<c10::complex<double>>& a, const Vectorized<c10::complex<double>>& b) {
|
| 48 |
+
// convert c10::complex<V> index mask to V index mask: xy -> xxyy
|
| 49 |
+
static_assert (mask > -1 && mask < 4, "Unexpected mask value");
|
| 50 |
+
switch (mask) {
|
| 51 |
+
case 0:
|
| 52 |
+
return a;
|
| 53 |
+
case 1:
|
| 54 |
+
return _mm256_blend_pd(a.values, b.values, 0x03);
|
| 55 |
+
case 2:
|
| 56 |
+
return _mm256_blend_pd(a.values, b.values, 0x0c);
|
| 57 |
+
case 3: break;
|
| 58 |
+
}
|
| 59 |
+
return b;
|
| 60 |
+
}
|
| 61 |
+
static Vectorized<c10::complex<double>> blendv(const Vectorized<c10::complex<double>>& a, const Vectorized<c10::complex<double>>& b,
|
| 62 |
+
const Vectorized<c10::complex<double>>& mask) {
|
| 63 |
+
// convert c10::complex<V> index mask to V index mask: xy -> xxyy
|
| 64 |
+
auto mask_ = _mm256_unpacklo_pd(mask.values, mask.values);
|
| 65 |
+
return _mm256_blendv_pd(a.values, b.values, mask_);
|
| 66 |
+
|
| 67 |
+
}
|
| 68 |
+
template<typename step_t>
|
| 69 |
+
static Vectorized<c10::complex<double>> arange(c10::complex<double> base = 0., step_t step = static_cast<step_t>(1)) {
|
| 70 |
+
return Vectorized<c10::complex<double>>(base,
|
| 71 |
+
base + step);
|
| 72 |
+
}
|
| 73 |
+
static Vectorized<c10::complex<double>> set(const Vectorized<c10::complex<double>>& a, const Vectorized<c10::complex<double>>& b,
|
| 74 |
+
int64_t count = size()) {
|
| 75 |
+
switch (count) {
|
| 76 |
+
case 0:
|
| 77 |
+
return a;
|
| 78 |
+
case 1:
|
| 79 |
+
return blend<1>(a, b);
|
| 80 |
+
}
|
| 81 |
+
return b;
|
| 82 |
+
}
|
| 83 |
+
static Vectorized<c10::complex<double>> loadu(const void* ptr, int64_t count = size()) {
|
| 84 |
+
if (count == size())
|
| 85 |
+
return _mm256_loadu_pd(reinterpret_cast<const double*>(ptr));
|
| 86 |
+
|
| 87 |
+
__at_align__ double tmp_values[2*size()];
|
| 88 |
+
// Ensure uninitialized memory does not change the output value See https://github.com/pytorch/pytorch/issues/32502
|
| 89 |
+
// for more details. We do not initialize arrays to zero using "={0}" because gcc would compile it to two
|
| 90 |
+
// instructions while a loop would be compiled to one instruction.
|
| 91 |
+
for (const auto i : c10::irange(2*size())) {
|
| 92 |
+
tmp_values[i] = 0.0;
|
| 93 |
+
}
|
| 94 |
+
std::memcpy(
|
| 95 |
+
tmp_values,
|
| 96 |
+
reinterpret_cast<const double*>(ptr),
|
| 97 |
+
count * sizeof(c10::complex<double>));
|
| 98 |
+
return _mm256_load_pd(tmp_values);
|
| 99 |
+
}
|
| 100 |
+
void store(void* ptr, int count = size()) const {
|
| 101 |
+
if (count == size()) {
|
| 102 |
+
_mm256_storeu_pd(reinterpret_cast<double*>(ptr), values);
|
| 103 |
+
} else if (count > 0) {
|
| 104 |
+
double tmp_values[2*size()];
|
| 105 |
+
_mm256_storeu_pd(reinterpret_cast<double*>(tmp_values), values);
|
| 106 |
+
std::memcpy(ptr, tmp_values, count * sizeof(c10::complex<double>));
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
const c10::complex<double>& operator[](int idx) const = delete;
|
| 110 |
+
c10::complex<double>& operator[](int idx) = delete;
|
| 111 |
+
Vectorized<c10::complex<double>> map(c10::complex<double> (*const f)(const c10::complex<double> &)) const {
|
| 112 |
+
__at_align__ c10::complex<double> tmp[size()];
|
| 113 |
+
store(tmp);
|
| 114 |
+
for (const auto i : c10::irange(size())) {
|
| 115 |
+
tmp[i] = f(tmp[i]);
|
| 116 |
+
}
|
| 117 |
+
return loadu(tmp);
|
| 118 |
+
}
|
| 119 |
+
__m256d abs_2_() const {
|
| 120 |
+
auto val_2 = _mm256_mul_pd(values, values); // a*a b*b
|
| 121 |
+
return _mm256_hadd_pd(val_2, val_2); // a*a+b*b a*a+b*b
|
| 122 |
+
}
|
| 123 |
+
__m256d abs_() const {
|
| 124 |
+
auto real = _mm256_movedup_pd(values); // real real
|
| 125 |
+
// movehdup_pd does not exist...
|
| 126 |
+
auto imag = _mm256_permute_pd(values, 0xf); // imag imag
|
| 127 |
+
return Sleef_hypotd4_u05(real, imag); // abs abs
|
| 128 |
+
}
|
| 129 |
+
Vectorized<c10::complex<double>> abs() const {
|
| 130 |
+
const __m256d real_mask = _mm256_castsi256_pd(_mm256_setr_epi64x(0xFFFFFFFFFFFFFFFF, 0x0000000000000000,
|
| 131 |
+
0xFFFFFFFFFFFFFFFF, 0x0000000000000000));
|
| 132 |
+
return _mm256_and_pd(abs_(), real_mask); // abs 0
|
| 133 |
+
}
|
| 134 |
+
__m256d angle_() const {
|
| 135 |
+
//angle = atan2(b/a)
|
| 136 |
+
auto b_a = _mm256_permute_pd(values, 0x05); // b a
|
| 137 |
+
return Sleef_atan2d4_u10(values, b_a); // 90-angle angle
|
| 138 |
+
}
|
| 139 |
+
Vectorized<c10::complex<double>> angle() const {
|
| 140 |
+
const __m256d real_mask = _mm256_castsi256_pd(_mm256_setr_epi64x(0xFFFFFFFFFFFFFFFF, 0x0000000000000000,
|
| 141 |
+
0xFFFFFFFFFFFFFFFF, 0x0000000000000000));
|
| 142 |
+
auto angle = _mm256_permute_pd(angle_(), 0x05); // angle 90-angle
|
| 143 |
+
return _mm256_and_pd(angle, real_mask); // angle 0
|
| 144 |
+
}
|
| 145 |
+
Vectorized<c10::complex<double>> sgn() const {
|
| 146 |
+
auto abs = abs_();
|
| 147 |
+
auto zero = _mm256_setzero_pd();
|
| 148 |
+
auto mask = _mm256_cmp_pd(abs, zero, _CMP_EQ_OQ);
|
| 149 |
+
auto div = _mm256_div_pd(values, abs);
|
| 150 |
+
return _mm256_blendv_pd(div, zero, mask);
|
| 151 |
+
}
|
| 152 |
+
__m256d real_() const {
|
| 153 |
+
const __m256d real_mask = _mm256_castsi256_pd(_mm256_setr_epi64x(0xFFFFFFFFFFFFFFFF, 0x0000000000000000,
|
| 154 |
+
0xFFFFFFFFFFFFFFFF, 0x0000000000000000));
|
| 155 |
+
return _mm256_and_pd(values, real_mask);
|
| 156 |
+
}
|
| 157 |
+
Vectorized<c10::complex<double>> real() const {
|
| 158 |
+
return real_();
|
| 159 |
+
}
|
| 160 |
+
__m256d imag_() const {
|
| 161 |
+
const __m256d imag_mask = _mm256_castsi256_pd(_mm256_setr_epi64x(0x0000000000000000, 0xFFFFFFFFFFFFFFFF,
|
| 162 |
+
0x0000000000000000, 0xFFFFFFFFFFFFFFFF));
|
| 163 |
+
return _mm256_and_pd(values, imag_mask);
|
| 164 |
+
}
|
| 165 |
+
Vectorized<c10::complex<double>> imag() const {
|
| 166 |
+
return _mm256_permute_pd(imag_(), 0x05); //b a
|
| 167 |
+
}
|
| 168 |
+
__m256d conj_() const {
|
| 169 |
+
const __m256d sign_mask = _mm256_setr_pd(0.0, -0.0, 0.0, -0.0);
|
| 170 |
+
return _mm256_xor_pd(values, sign_mask); // a -b
|
| 171 |
+
}
|
| 172 |
+
Vectorized<c10::complex<double>> conj() const {
|
| 173 |
+
return conj_();
|
| 174 |
+
}
|
| 175 |
+
Vectorized<c10::complex<double>> log() const {
|
| 176 |
+
// Most trigonomic ops use the log() op to improve complex number performance.
|
| 177 |
+
return map(std::log);
|
| 178 |
+
}
|
| 179 |
+
Vectorized<c10::complex<double>> log2() const {
|
| 180 |
+
const __m256d log2_ = _mm256_set1_pd(std::log(2));
|
| 181 |
+
return _mm256_div_pd(log(), log2_);
|
| 182 |
+
}
|
| 183 |
+
Vectorized<c10::complex<double>> log10() const {
|
| 184 |
+
const __m256d log10_ = _mm256_set1_pd(std::log(10));
|
| 185 |
+
return _mm256_div_pd(log(), log10_);
|
| 186 |
+
}
|
| 187 |
+
Vectorized<c10::complex<double>> log1p() const {
|
| 188 |
+
return map(std::log1p);
|
| 189 |
+
}
|
| 190 |
+
Vectorized<c10::complex<double>> asin() const {
|
| 191 |
+
// asin(x)
|
| 192 |
+
// = -i*ln(iz + sqrt(1 -z^2))
|
| 193 |
+
// = -i*ln((ai - b) + sqrt(1 - (a + bi)*(a + bi)))
|
| 194 |
+
// = -i*ln((-b + ai) + sqrt(1 - (a**2 - b**2) - 2*abi))
|
| 195 |
+
const __m256d one = _mm256_set1_pd(1);
|
| 196 |
+
|
| 197 |
+
auto conj = conj_();
|
| 198 |
+
auto b_a = _mm256_permute_pd(conj, 0x05); //-b a
|
| 199 |
+
auto ab = _mm256_mul_pd(conj, b_a); //-ab -ab
|
| 200 |
+
auto im = _mm256_add_pd(ab, ab); //-2ab -2ab
|
| 201 |
+
|
| 202 |
+
auto val_2 = _mm256_mul_pd(values, values); // a*a b*b
|
| 203 |
+
auto re = _mm256_hsub_pd(val_2, _mm256_permute_pd(val_2, 0x05)); // a*a-b*b b*b-a*a
|
| 204 |
+
re = _mm256_sub_pd(one, re);
|
| 205 |
+
|
| 206 |
+
auto root = Vectorized(_mm256_blend_pd(re, im, 0x0A)).sqrt(); //sqrt(re + i*im)
|
| 207 |
+
auto ln = Vectorized(_mm256_add_pd(b_a, root)).log(); //ln(iz + sqrt())
|
| 208 |
+
return Vectorized(_mm256_permute_pd(ln.values, 0x05)).conj(); //-i*ln()
|
| 209 |
+
}
|
| 210 |
+
Vectorized<c10::complex<double>> acos() const {
|
| 211 |
+
// acos(x) = pi/2 - asin(x)
|
| 212 |
+
constexpr auto pi_2d = c10::pi<double> / 2;
|
| 213 |
+
const __m256d pi_2 = _mm256_setr_pd(pi_2d, 0.0, pi_2d, 0.0);
|
| 214 |
+
return _mm256_sub_pd(pi_2, asin());
|
| 215 |
+
}
|
| 216 |
+
Vectorized<c10::complex<double>> atan() const;
|
| 217 |
+
Vectorized<c10::complex<double>> atanh() const {
|
| 218 |
+
return map(std::atanh);
|
| 219 |
+
}
|
| 220 |
+
Vectorized<c10::complex<double>> exp() const {
|
| 221 |
+
//exp(a + bi)
|
| 222 |
+
// = exp(a)*(cos(b) + sin(b)i)
|
| 223 |
+
auto exp = Sleef_expd4_u10(values); //exp(a) exp(b)
|
| 224 |
+
exp = _mm256_blend_pd(exp, _mm256_permute_pd(exp, 0x05), 0x0A); //exp(a) exp(a)
|
| 225 |
+
|
| 226 |
+
auto sin_cos = Sleef_sincosd4_u10(values); //[sin(a), cos(a)] [sin(b), cos(b)]
|
| 227 |
+
auto cos_sin = _mm256_blend_pd(_mm256_permute_pd(sin_cos.y, 0x05),
|
| 228 |
+
sin_cos.x, 0x0A); //cos(b) sin(b)
|
| 229 |
+
return _mm256_mul_pd(exp, cos_sin);
|
| 230 |
+
}
|
| 231 |
+
Vectorized<c10::complex<double>> exp2() const {
|
| 232 |
+
// Use identity 2**x = exp(log(2) * x)
|
| 233 |
+
const __m256d ln_2 = _mm256_set1_pd(c10::ln_2<double>);
|
| 234 |
+
Vectorized<c10::complex<double>> scaled_values = _mm256_mul_pd(values, ln_2);
|
| 235 |
+
return scaled_values.exp();
|
| 236 |
+
}
|
| 237 |
+
Vectorized<c10::complex<double>> expm1() const {
|
| 238 |
+
return map(std::expm1);
|
| 239 |
+
}
|
| 240 |
+
Vectorized<c10::complex<double>> sin() const {
|
| 241 |
+
return map(std::sin);
|
| 242 |
+
}
|
| 243 |
+
Vectorized<c10::complex<double>> sinh() const {
|
| 244 |
+
return map(std::sinh);
|
| 245 |
+
}
|
| 246 |
+
Vectorized<c10::complex<double>> cos() const {
|
| 247 |
+
return map(std::cos);
|
| 248 |
+
}
|
| 249 |
+
Vectorized<c10::complex<double>> cosh() const {
|
| 250 |
+
return map(std::cosh);
|
| 251 |
+
}
|
| 252 |
+
Vectorized<c10::complex<double>> ceil() const {
|
| 253 |
+
return _mm256_ceil_pd(values);
|
| 254 |
+
}
|
| 255 |
+
Vectorized<c10::complex<double>> floor() const {
|
| 256 |
+
return _mm256_floor_pd(values);
|
| 257 |
+
}
|
| 258 |
+
Vectorized<c10::complex<double>> neg() const {
|
| 259 |
+
auto zero = _mm256_setzero_pd();
|
| 260 |
+
return _mm256_sub_pd(zero, values);
|
| 261 |
+
}
|
| 262 |
+
Vectorized<c10::complex<double>> round() const {
|
| 263 |
+
return _mm256_round_pd(values, (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
|
| 264 |
+
}
|
| 265 |
+
Vectorized<c10::complex<double>> tan() const {
|
| 266 |
+
return map(std::tan);
|
| 267 |
+
}
|
| 268 |
+
Vectorized<c10::complex<double>> tanh() const {
|
| 269 |
+
return map(std::tanh);
|
| 270 |
+
}
|
| 271 |
+
Vectorized<c10::complex<double>> trunc() const {
|
| 272 |
+
return _mm256_round_pd(values, (_MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC));
|
| 273 |
+
}
|
| 274 |
+
Vectorized<c10::complex<double>> sqrt() const {
|
| 275 |
+
return map(std::sqrt);
|
| 276 |
+
}
|
| 277 |
+
Vectorized<c10::complex<double>> reciprocal() const;
|
| 278 |
+
Vectorized<c10::complex<double>> rsqrt() const {
|
| 279 |
+
return sqrt().reciprocal();
|
| 280 |
+
}
|
| 281 |
+
Vectorized<c10::complex<double>> pow(const Vectorized<c10::complex<double>> &exp) const {
|
| 282 |
+
__at_align__ c10::complex<double> x_tmp[size()];
|
| 283 |
+
__at_align__ c10::complex<double> y_tmp[size()];
|
| 284 |
+
store(x_tmp);
|
| 285 |
+
exp.store(y_tmp);
|
| 286 |
+
for (const auto i : c10::irange(size())) {
|
| 287 |
+
x_tmp[i] = std::pow(x_tmp[i], y_tmp[i]);
|
| 288 |
+
}
|
| 289 |
+
return loadu(x_tmp);
|
| 290 |
+
}
|
| 291 |
+
// Comparison using the _CMP_**_OQ predicate.
|
| 292 |
+
// `O`: get false if an operand is NaN
|
| 293 |
+
// `Q`: do not raise if an operand is NaN
|
| 294 |
+
Vectorized<c10::complex<double>> operator==(const Vectorized<c10::complex<double>>& other) const {
|
| 295 |
+
return _mm256_cmp_pd(values, other.values, _CMP_EQ_OQ);
|
| 296 |
+
}
|
| 297 |
+
Vectorized<c10::complex<double>> operator!=(const Vectorized<c10::complex<double>>& other) const {
|
| 298 |
+
return _mm256_cmp_pd(values, other.values, _CMP_NEQ_UQ);
|
| 299 |
+
}
|
| 300 |
+
Vectorized<c10::complex<double>> operator<(const Vectorized<c10::complex<double>>&) const {
|
| 301 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 302 |
+
}
|
| 303 |
+
Vectorized<c10::complex<double>> operator<=(const Vectorized<c10::complex<double>>&) const {
|
| 304 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 305 |
+
}
|
| 306 |
+
Vectorized<c10::complex<double>> operator>(const Vectorized<c10::complex<double>>&) const {
|
| 307 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 308 |
+
}
|
| 309 |
+
Vectorized<c10::complex<double>> operator>=(const Vectorized<c10::complex<double>>&) const {
|
| 310 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
Vectorized<c10::complex<double>> eq(const Vectorized<c10::complex<double>>& other) const;
|
| 314 |
+
Vectorized<c10::complex<double>> ne(const Vectorized<c10::complex<double>>& other) const;
|
| 315 |
+
};
|
| 316 |
+
|
| 317 |
+
template <> Vectorized<c10::complex<double>> inline operator+(const Vectorized<c10::complex<double>> &a, const Vectorized<c10::complex<double>> &b) {
|
| 318 |
+
return _mm256_add_pd(a, b);
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
template <> Vectorized<c10::complex<double>> inline operator-(const Vectorized<c10::complex<double>> &a, const Vectorized<c10::complex<double>> &b) {
|
| 322 |
+
return _mm256_sub_pd(a, b);
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
template <> Vectorized<c10::complex<double>> inline operator*(const Vectorized<c10::complex<double>> &a, const Vectorized<c10::complex<double>> &b) {
|
| 326 |
+
//(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
|
| 327 |
+
const __m256d sign_mask = _mm256_setr_pd(0.0, -0.0, 0.0, -0.0);
|
| 328 |
+
auto ac_bd = _mm256_mul_pd(a, b); //ac bd
|
| 329 |
+
|
| 330 |
+
auto d_c = _mm256_permute_pd(b, 0x05); //d c
|
| 331 |
+
d_c = _mm256_xor_pd(sign_mask, d_c); //d -c
|
| 332 |
+
auto ad_bc = _mm256_mul_pd(a, d_c); //ad -bc
|
| 333 |
+
|
| 334 |
+
auto ret = _mm256_hsub_pd(ac_bd, ad_bc); //ac - bd ad + bc
|
| 335 |
+
return ret;
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
template <> Vectorized<c10::complex<double>> inline operator/(const Vectorized<c10::complex<double>> &a, const Vectorized<c10::complex<double>> &b) {
|
| 339 |
+
//re + im*i = (a + bi) / (c + di)
|
| 340 |
+
auto mask = _mm256_set1_pd(-0.f);
|
| 341 |
+
auto fabs_cd = _mm256_andnot_pd(mask, b); // |c| |d|
|
| 342 |
+
auto fabs_dc = _mm256_permute_pd(fabs_cd, 0x05); // |d| |c|
|
| 343 |
+
auto scale = _mm256_div_pd(_mm256_set1_pd(1.0f), _mm256_max_pd(fabs_cd, fabs_dc)); // 1/sc 1/sc
|
| 344 |
+
auto a2 = _mm256_mul_pd(a, scale); // a/sc b/sc
|
| 345 |
+
auto b2 = _mm256_mul_pd(b, scale); // c/sc d/sc
|
| 346 |
+
auto acbd2 = _mm256_mul_pd(a2, b2);
|
| 347 |
+
|
| 348 |
+
const __m256d sign_mask = _mm256_setr_pd(-0.0, 0.0, -0.0, 0.0);
|
| 349 |
+
auto dc2 = _mm256_permute_pd(b2, 0x05); // d/sc c/sc
|
| 350 |
+
dc2 = _mm256_xor_pd(sign_mask, dc2); // -d/|c,d| c/sc
|
| 351 |
+
auto adbc2 = _mm256_mul_pd(a2, dc2); //-ad/sc^2 bc/sc^2
|
| 352 |
+
auto res2 = _mm256_hadd_pd(acbd2, adbc2); //(ac+bd)/sc^2 (bc-ad)/sc^2
|
| 353 |
+
|
| 354 |
+
// get the denominator
|
| 355 |
+
auto denom2 = Vectorized<c10::complex<double>>(b2).abs_2_(); // (c^2+d^2)/sc^2 (c^2+d^2)/sc^2
|
| 356 |
+
res2 = _mm256_div_pd(res2, denom2);
|
| 357 |
+
return res2;
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
// reciprocal. Implement this here so we can use multiplication.
|
| 361 |
+
inline Vectorized<c10::complex<double>> Vectorized<c10::complex<double>>::reciprocal() const{
|
| 362 |
+
//re + im*i = (a + bi) / (c + di)
|
| 363 |
+
//re = (ac + bd)/abs_2() = c/abs_2()
|
| 364 |
+
//im = (bc - ad)/abs_2() = d/abs_2()
|
| 365 |
+
const __m256d sign_mask = _mm256_setr_pd(0.0, -0.0, 0.0, -0.0);
|
| 366 |
+
auto c_d = _mm256_xor_pd(sign_mask, values); //c -d
|
| 367 |
+
return _mm256_div_pd(c_d, abs_2_());
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
inline Vectorized<c10::complex<double>> Vectorized<c10::complex<double>>::atan() const {
|
| 371 |
+
// atan(x) = i/2 * ln((i + z)/(i - z))
|
| 372 |
+
const __m256d i = _mm256_setr_pd(0.0, 1.0, 0.0, 1.0);
|
| 373 |
+
const Vectorized i_half = _mm256_setr_pd(0.0, 0.5, 0.0, 0.5);
|
| 374 |
+
|
| 375 |
+
auto sum = Vectorized(_mm256_add_pd(i, values)); // a 1+b
|
| 376 |
+
auto sub = Vectorized(_mm256_sub_pd(i, values)); // -a 1-b
|
| 377 |
+
auto ln = (sum/sub).log(); // ln((i + z)/(i - z))
|
| 378 |
+
return i_half*ln; // i/2*ln()
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
template <>
|
| 382 |
+
Vectorized<c10::complex<double>> inline maximum(const Vectorized<c10::complex<double>>& a, const Vectorized<c10::complex<double>>& b) {
|
| 383 |
+
auto abs_a = a.abs_2_();
|
| 384 |
+
auto abs_b = b.abs_2_();
|
| 385 |
+
auto mask = _mm256_cmp_pd(abs_a, abs_b, _CMP_LT_OQ);
|
| 386 |
+
auto max = _mm256_blendv_pd(a, b, mask);
|
| 387 |
+
// Exploit the fact that all-ones is a NaN.
|
| 388 |
+
auto isnan = _mm256_cmp_pd(abs_a, abs_b, _CMP_UNORD_Q);
|
| 389 |
+
return _mm256_or_pd(max, isnan);
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
template <>
|
| 393 |
+
Vectorized<c10::complex<double>> inline minimum(const Vectorized<c10::complex<double>>& a, const Vectorized<c10::complex<double>>& b) {
|
| 394 |
+
auto abs_a = a.abs_2_();
|
| 395 |
+
auto abs_b = b.abs_2_();
|
| 396 |
+
auto mask = _mm256_cmp_pd(abs_a, abs_b, _CMP_GT_OQ);
|
| 397 |
+
auto min = _mm256_blendv_pd(a, b, mask);
|
| 398 |
+
// Exploit the fact that all-ones is a NaN.
|
| 399 |
+
auto isnan = _mm256_cmp_pd(abs_a, abs_b, _CMP_UNORD_Q);
|
| 400 |
+
return _mm256_or_pd(min, isnan);
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
template <>
|
| 404 |
+
Vectorized<c10::complex<double>> inline operator&(const Vectorized<c10::complex<double>>& a, const Vectorized<c10::complex<double>>& b) {
|
| 405 |
+
return _mm256_and_pd(a, b);
|
| 406 |
+
}
|
| 407 |
+
|
| 408 |
+
template <>
|
| 409 |
+
Vectorized<c10::complex<double>> inline operator|(const Vectorized<c10::complex<double>>& a, const Vectorized<c10::complex<double>>& b) {
|
| 410 |
+
return _mm256_or_pd(a, b);
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
template <>
|
| 414 |
+
Vectorized<c10::complex<double>> inline operator^(const Vectorized<c10::complex<double>>& a, const Vectorized<c10::complex<double>>& b) {
|
| 415 |
+
return _mm256_xor_pd(a, b);
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
inline Vectorized<c10::complex<double>> Vectorized<c10::complex<double>>::eq(const Vectorized<c10::complex<double>>& other) const {
|
| 419 |
+
auto eq = (*this == other); // compares real and imag individually
|
| 420 |
+
// If both real numbers and imag numbers are equal, then the complex numbers are equal
|
| 421 |
+
return (eq.real() & eq.imag()) & Vectorized<c10::complex<double>>(_mm256_set1_pd(1.0));
|
| 422 |
+
}
|
| 423 |
+
|
| 424 |
+
inline Vectorized<c10::complex<double>> Vectorized<c10::complex<double>>::ne(const Vectorized<c10::complex<double>>& other) const {
|
| 425 |
+
auto ne = (*this != other); // compares real and imag individually
|
| 426 |
+
// If either real numbers or imag numbers are not equal, then the complex numbers are not equal
|
| 427 |
+
return (ne.real() | ne.imag()) & Vectorized<c10::complex<double>>(_mm256_set1_pd(1.0));
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
#endif
|
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#include <c10/util/irange.h>
|
| 8 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 9 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 10 |
+
#if defined(CPU_CAPABILITY_AVX2)
|
| 11 |
+
#define SLEEF_STATIC_LIBS
|
| 12 |
+
#include <sleef.h>
|
| 13 |
+
#endif
|
| 14 |
+
|
| 15 |
+
namespace at::vec {
|
| 16 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 17 |
+
inline namespace CPU_CAPABILITY {
|
| 18 |
+
|
| 19 |
+
#if defined(CPU_CAPABILITY_AVX2)
|
| 20 |
+
|
| 21 |
+
template <> class Vectorized<c10::complex<float>> {
|
| 22 |
+
private:
|
| 23 |
+
__m256 values;
|
| 24 |
+
public:
|
| 25 |
+
using value_type = c10::complex<float>;
|
| 26 |
+
using size_type = int;
|
| 27 |
+
static constexpr size_type size() {
|
| 28 |
+
return 4;
|
| 29 |
+
}
|
| 30 |
+
Vectorized() {}
|
| 31 |
+
Vectorized(__m256 v) : values(v) {}
|
| 32 |
+
Vectorized(c10::complex<float> val) {
|
| 33 |
+
float real_value = val.real();
|
| 34 |
+
float imag_value = val.imag();
|
| 35 |
+
values = _mm256_setr_ps(real_value, imag_value,
|
| 36 |
+
real_value, imag_value,
|
| 37 |
+
real_value, imag_value,
|
| 38 |
+
real_value, imag_value
|
| 39 |
+
);
|
| 40 |
+
}
|
| 41 |
+
Vectorized(c10::complex<float> val1, c10::complex<float> val2, c10::complex<float> val3, c10::complex<float> val4) {
|
| 42 |
+
values = _mm256_setr_ps(val1.real(), val1.imag(),
|
| 43 |
+
val2.real(), val2.imag(),
|
| 44 |
+
val3.real(), val3.imag(),
|
| 45 |
+
val4.real(), val4.imag()
|
| 46 |
+
);
|
| 47 |
+
}
|
| 48 |
+
operator __m256() const {
|
| 49 |
+
return values;
|
| 50 |
+
}
|
| 51 |
+
template <int64_t mask>
|
| 52 |
+
static Vectorized<c10::complex<float>> blend(const Vectorized<c10::complex<float>>& a, const Vectorized<c10::complex<float>>& b) {
|
| 53 |
+
// convert c10::complex<V> index mask to V index mask: xy -> xxyy
|
| 54 |
+
static_assert(mask > -1 && mask < 16, "Unexpected mask range");
|
| 55 |
+
switch (mask) {
|
| 56 |
+
case 0:
|
| 57 |
+
return a;
|
| 58 |
+
case 1:
|
| 59 |
+
return _mm256_blend_ps(a.values, b.values, 0x03); //b0000 0001 = b0000 0011
|
| 60 |
+
case 2:
|
| 61 |
+
return _mm256_blend_ps(a.values, b.values, 0x0C); //b0000 0010 = b0000 1100
|
| 62 |
+
case 3:
|
| 63 |
+
return _mm256_blend_ps(a.values, b.values, 0x0F); //b0000 0011 = b0000 1111
|
| 64 |
+
case 4:
|
| 65 |
+
return _mm256_blend_ps(a.values, b.values, 0x30); //b0000 0100 = b0011 0000
|
| 66 |
+
case 5:
|
| 67 |
+
return _mm256_blend_ps(a.values, b.values, 0x33); //b0000 0101 = b0011 0011
|
| 68 |
+
case 6:
|
| 69 |
+
return _mm256_blend_ps(a.values, b.values, 0x3C); //b0000 0110 = b0011 1100
|
| 70 |
+
case 7:
|
| 71 |
+
return _mm256_blend_ps(a.values, b.values, 0x3F); //b0000 0111 = b0011 1111
|
| 72 |
+
case 8:
|
| 73 |
+
return _mm256_blend_ps(a.values, b.values, 0xC0); //b0000 1000 = b1100 0000
|
| 74 |
+
case 9:
|
| 75 |
+
return _mm256_blend_ps(a.values, b.values, 0xC3); //b0000 1001 = b1100 0011
|
| 76 |
+
case 10:
|
| 77 |
+
return _mm256_blend_ps(a.values, b.values, 0xCC); //b0000 1010 = b1100 1100
|
| 78 |
+
case 11:
|
| 79 |
+
return _mm256_blend_ps(a.values, b.values, 0xCF); //b0000 1011 = b1100 1111
|
| 80 |
+
case 12:
|
| 81 |
+
return _mm256_blend_ps(a.values, b.values, 0xF0); //b0000 1100 = b1111 0000
|
| 82 |
+
case 13:
|
| 83 |
+
return _mm256_blend_ps(a.values, b.values, 0xF3); //b0000 1101 = b1111 0011
|
| 84 |
+
case 14:
|
| 85 |
+
return _mm256_blend_ps(a.values, b.values, 0xFC); //b0000 1110 = b1111 1100
|
| 86 |
+
default: break;
|
| 87 |
+
}
|
| 88 |
+
return b;
|
| 89 |
+
}
|
| 90 |
+
static Vectorized<c10::complex<float>> blendv(const Vectorized<c10::complex<float>>& a, const Vectorized<c10::complex<float>>& b,
|
| 91 |
+
const Vectorized<c10::complex<float>>& mask) {
|
| 92 |
+
// convert c10::complex<V> index mask to V index mask: xy -> xxyy
|
| 93 |
+
auto mask_ = _mm256_unpacklo_ps(mask.values, mask.values);
|
| 94 |
+
return _mm256_blendv_ps(a.values, b.values, mask_);
|
| 95 |
+
|
| 96 |
+
}
|
| 97 |
+
template<typename step_t>
|
| 98 |
+
static Vectorized<c10::complex<float>> arange(c10::complex<float> base = 0., step_t step = static_cast<step_t>(1)) {
|
| 99 |
+
return Vectorized<c10::complex<float>>(base,
|
| 100 |
+
base + step,
|
| 101 |
+
base + c10::complex<float>(2)*step,
|
| 102 |
+
base + c10::complex<float>(3)*step);
|
| 103 |
+
}
|
| 104 |
+
static Vectorized<c10::complex<float>> set(const Vectorized<c10::complex<float>>& a, const Vectorized<c10::complex<float>>& b,
|
| 105 |
+
int64_t count = size()) {
|
| 106 |
+
switch (count) {
|
| 107 |
+
case 0:
|
| 108 |
+
return a;
|
| 109 |
+
case 1:
|
| 110 |
+
return blend<1>(a, b);
|
| 111 |
+
case 2:
|
| 112 |
+
return blend<3>(a, b);
|
| 113 |
+
case 3:
|
| 114 |
+
return blend<7>(a, b);
|
| 115 |
+
}
|
| 116 |
+
return b;
|
| 117 |
+
}
|
| 118 |
+
static Vectorized<c10::complex<float>> loadu(const void* ptr, int64_t count = size()) {
|
| 119 |
+
if (count == size())
|
| 120 |
+
return _mm256_loadu_ps(reinterpret_cast<const float*>(ptr));
|
| 121 |
+
|
| 122 |
+
__at_align__ float tmp_values[2*size()];
|
| 123 |
+
// Ensure uninitialized memory does not change the output value See https://github.com/pytorch/pytorch/issues/32502
|
| 124 |
+
// for more details. We do not initialize arrays to zero using "={0}" because gcc would compile it to two
|
| 125 |
+
// instructions while a loop would be compiled to one instruction.
|
| 126 |
+
for (const auto i : c10::irange(2*size())) {
|
| 127 |
+
tmp_values[i] = 0.0;
|
| 128 |
+
}
|
| 129 |
+
std::memcpy(
|
| 130 |
+
tmp_values,
|
| 131 |
+
reinterpret_cast<const float*>(ptr),
|
| 132 |
+
count * sizeof(c10::complex<float>));
|
| 133 |
+
return _mm256_load_ps(tmp_values);
|
| 134 |
+
}
|
| 135 |
+
void store(void* ptr, int count = size()) const {
|
| 136 |
+
if (count == size()) {
|
| 137 |
+
_mm256_storeu_ps(reinterpret_cast<float*>(ptr), values);
|
| 138 |
+
} else if (count > 0) {
|
| 139 |
+
float tmp_values[2*size()];
|
| 140 |
+
_mm256_storeu_ps(reinterpret_cast<float*>(tmp_values), values);
|
| 141 |
+
std::memcpy(ptr, tmp_values, count * sizeof(c10::complex<float>));
|
| 142 |
+
}
|
| 143 |
+
}
|
| 144 |
+
const c10::complex<float>& operator[](int idx) const = delete;
|
| 145 |
+
c10::complex<float>& operator[](int idx) = delete;
|
| 146 |
+
Vectorized<c10::complex<float>> map(c10::complex<float> (*const f)(const c10::complex<float> &)) const {
|
| 147 |
+
__at_align__ c10::complex<float> tmp[size()];
|
| 148 |
+
store(tmp);
|
| 149 |
+
for (const auto i : c10::irange(size())) {
|
| 150 |
+
tmp[i] = f(tmp[i]);
|
| 151 |
+
}
|
| 152 |
+
return loadu(tmp);
|
| 153 |
+
}
|
| 154 |
+
__m256 abs_2_() const {
|
| 155 |
+
auto val_2 = _mm256_mul_ps(values, values); // a*a b*b
|
| 156 |
+
auto ret = _mm256_hadd_ps(val_2, val_2); // a*a+b*b a*a+b*b
|
| 157 |
+
return _mm256_permute_ps(ret, 0xD8);
|
| 158 |
+
}
|
| 159 |
+
__m256 abs_() const {
|
| 160 |
+
auto real = _mm256_moveldup_ps(values); // real real
|
| 161 |
+
auto imag = _mm256_movehdup_ps(values); // imag imag
|
| 162 |
+
return Sleef_hypotf8_u05(real, imag); // abs abs
|
| 163 |
+
}
|
| 164 |
+
Vectorized<c10::complex<float>> abs() const {
|
| 165 |
+
const __m256 real_mask = _mm256_castsi256_ps(_mm256_setr_epi32(0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000,
|
| 166 |
+
0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000));
|
| 167 |
+
return _mm256_and_ps(abs_(), real_mask); // abs 0
|
| 168 |
+
}
|
| 169 |
+
__m256 angle_() const {
|
| 170 |
+
//angle = atan2(b/a)
|
| 171 |
+
auto b_a = _mm256_permute_ps(values, 0xB1); // b a
|
| 172 |
+
return Sleef_atan2f8_u10(values, b_a); // 90-angle angle
|
| 173 |
+
}
|
| 174 |
+
Vectorized<c10::complex<float>> angle() const {
|
| 175 |
+
const __m256 real_mask = _mm256_castsi256_ps(_mm256_setr_epi32(0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000,
|
| 176 |
+
0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000));
|
| 177 |
+
auto angle = _mm256_permute_ps(angle_(), 0xB1); // angle 90-angle
|
| 178 |
+
return _mm256_and_ps(angle, real_mask); // angle 0
|
| 179 |
+
}
|
| 180 |
+
Vectorized<c10::complex<float>> sgn() const {
|
| 181 |
+
auto abs = abs_();
|
| 182 |
+
auto zero = _mm256_setzero_ps();
|
| 183 |
+
auto mask = _mm256_cmp_ps(abs, zero, _CMP_EQ_OQ);
|
| 184 |
+
auto div = _mm256_div_ps(values, abs);
|
| 185 |
+
return _mm256_blendv_ps(div, zero, mask);
|
| 186 |
+
}
|
| 187 |
+
__m256 real_() const {
|
| 188 |
+
const __m256 real_mask = _mm256_castsi256_ps(_mm256_setr_epi32(0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000,
|
| 189 |
+
0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000));
|
| 190 |
+
return _mm256_and_ps(values, real_mask);
|
| 191 |
+
}
|
| 192 |
+
Vectorized<c10::complex<float>> real() const {
|
| 193 |
+
return real_();
|
| 194 |
+
}
|
| 195 |
+
__m256 imag_() const {
|
| 196 |
+
const __m256 imag_mask = _mm256_castsi256_ps(_mm256_setr_epi32(0x00000000, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF,
|
| 197 |
+
0x00000000, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF));
|
| 198 |
+
return _mm256_and_ps(values, imag_mask);
|
| 199 |
+
}
|
| 200 |
+
Vectorized<c10::complex<float>> imag() const {
|
| 201 |
+
return _mm256_permute_ps(imag_(), 0xB1); //b a
|
| 202 |
+
}
|
| 203 |
+
__m256 conj_() const {
|
| 204 |
+
const __m256 sign_mask = _mm256_setr_ps(0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0);
|
| 205 |
+
return _mm256_xor_ps(values, sign_mask); // a -b
|
| 206 |
+
}
|
| 207 |
+
Vectorized<c10::complex<float>> conj() const {
|
| 208 |
+
return conj_();
|
| 209 |
+
}
|
| 210 |
+
Vectorized<c10::complex<float>> log() const {
|
| 211 |
+
// Most trigonomic ops use the log() op to improve complex number performance.
|
| 212 |
+
return map(std::log);
|
| 213 |
+
}
|
| 214 |
+
Vectorized<c10::complex<float>> log2() const {
|
| 215 |
+
const __m256 log2_ = _mm256_set1_ps(std::log(2));
|
| 216 |
+
return _mm256_div_ps(log(), log2_);
|
| 217 |
+
}
|
| 218 |
+
Vectorized<c10::complex<float>> log10() const {
|
| 219 |
+
const __m256 log10_ = _mm256_set1_ps(std::log(10));
|
| 220 |
+
return _mm256_div_ps(log(), log10_);
|
| 221 |
+
}
|
| 222 |
+
Vectorized<c10::complex<float>> log1p() const {
|
| 223 |
+
return map(std::log1p);
|
| 224 |
+
}
|
| 225 |
+
Vectorized<c10::complex<float>> asin() const {
|
| 226 |
+
// asin(x)
|
| 227 |
+
// = -i*ln(iz + sqrt(1 -z^2))
|
| 228 |
+
// = -i*ln((ai - b) + sqrt(1 - (a + bi)*(a + bi)))
|
| 229 |
+
// = -i*ln((-b + ai) + sqrt(1 - (a**2 - b**2) - 2*abi))
|
| 230 |
+
const __m256 one = _mm256_set1_ps(1);
|
| 231 |
+
|
| 232 |
+
auto conj = conj_();
|
| 233 |
+
auto b_a = _mm256_permute_ps(conj, 0xB1); //-b a
|
| 234 |
+
auto ab = _mm256_mul_ps(conj, b_a); //-ab -ab
|
| 235 |
+
auto im = _mm256_add_ps(ab, ab); //-2ab -2ab
|
| 236 |
+
|
| 237 |
+
auto val_2 = _mm256_mul_ps(values, values); // a*a b*b
|
| 238 |
+
auto re = _mm256_hsub_ps(val_2, _mm256_permute_ps(val_2, 0xB1)); // a*a-b*b b*b-a*a
|
| 239 |
+
re = _mm256_permute_ps(re, 0xD8);
|
| 240 |
+
re = _mm256_sub_ps(one, re);
|
| 241 |
+
|
| 242 |
+
auto root = Vectorized(_mm256_blend_ps(re, im, 0xAA)).sqrt(); //sqrt(re + i*im)
|
| 243 |
+
auto ln = Vectorized(_mm256_add_ps(b_a, root)).log(); //ln(iz + sqrt())
|
| 244 |
+
return Vectorized(_mm256_permute_ps(ln.values, 0xB1)).conj(); //-i*ln()
|
| 245 |
+
}
|
| 246 |
+
Vectorized<c10::complex<float>> acos() const {
|
| 247 |
+
return map(std::acos);
|
| 248 |
+
}
|
| 249 |
+
Vectorized<c10::complex<float>> atan() const;
|
| 250 |
+
Vectorized<c10::complex<float>> atanh() const {
|
| 251 |
+
return map(std::atanh);
|
| 252 |
+
}
|
| 253 |
+
Vectorized<c10::complex<float>> exp() const {
|
| 254 |
+
//exp(a + bi)
|
| 255 |
+
// = exp(a)*(cos(b) + sin(b)i)
|
| 256 |
+
auto exp = Sleef_expf8_u10(values); //exp(a) exp(b)
|
| 257 |
+
exp = _mm256_blend_ps(exp, _mm256_permute_ps(exp, 0xB1), 0xAA); //exp(a) exp(a)
|
| 258 |
+
|
| 259 |
+
auto sin_cos = Sleef_sincosf8_u10(values); //[sin(a), cos(a)] [sin(b), cos(b)]
|
| 260 |
+
auto cos_sin = _mm256_blend_ps(_mm256_permute_ps(sin_cos.y, 0xB1),
|
| 261 |
+
sin_cos.x, 0xAA); //cos(b) sin(b)
|
| 262 |
+
return _mm256_mul_ps(exp, cos_sin);
|
| 263 |
+
}
|
| 264 |
+
Vectorized<c10::complex<float>> exp2() const {
|
| 265 |
+
// Use identity 2**x = exp(log(2) * x)
|
| 266 |
+
const __m256 ln_2 = _mm256_set1_ps(c10::ln_2<float>);
|
| 267 |
+
Vectorized<c10::complex<float>> scaled_values = _mm256_mul_ps(values, ln_2);
|
| 268 |
+
return scaled_values.exp();
|
| 269 |
+
}
|
| 270 |
+
Vectorized<c10::complex<float>> expm1() const {
|
| 271 |
+
return map(std::expm1);
|
| 272 |
+
}
|
| 273 |
+
Vectorized<c10::complex<float>> sin() const {
|
| 274 |
+
return map(std::sin);
|
| 275 |
+
}
|
| 276 |
+
Vectorized<c10::complex<float>> sinh() const {
|
| 277 |
+
return map(std::sinh);
|
| 278 |
+
}
|
| 279 |
+
Vectorized<c10::complex<float>> cos() const {
|
| 280 |
+
return map(std::cos);
|
| 281 |
+
}
|
| 282 |
+
Vectorized<c10::complex<float>> cosh() const {
|
| 283 |
+
return map(std::cosh);
|
| 284 |
+
}
|
| 285 |
+
Vectorized<c10::complex<float>> ceil() const {
|
| 286 |
+
return _mm256_ceil_ps(values);
|
| 287 |
+
}
|
| 288 |
+
Vectorized<c10::complex<float>> floor() const {
|
| 289 |
+
return _mm256_floor_ps(values);
|
| 290 |
+
}
|
| 291 |
+
Vectorized<c10::complex<float>> neg() const {
|
| 292 |
+
auto zero = _mm256_setzero_ps();
|
| 293 |
+
return _mm256_sub_ps(zero, values);
|
| 294 |
+
}
|
| 295 |
+
Vectorized<c10::complex<float>> round() const {
|
| 296 |
+
return _mm256_round_ps(values, (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
|
| 297 |
+
}
|
| 298 |
+
Vectorized<c10::complex<float>> tan() const {
|
| 299 |
+
return map(std::tan);
|
| 300 |
+
}
|
| 301 |
+
Vectorized<c10::complex<float>> tanh() const {
|
| 302 |
+
return map(std::tanh);
|
| 303 |
+
}
|
| 304 |
+
Vectorized<c10::complex<float>> trunc() const {
|
| 305 |
+
return _mm256_round_ps(values, (_MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC));
|
| 306 |
+
}
|
| 307 |
+
Vectorized<c10::complex<float>> sqrt() const {
|
| 308 |
+
return map(std::sqrt);
|
| 309 |
+
}
|
| 310 |
+
Vectorized<c10::complex<float>> reciprocal() const;
|
| 311 |
+
Vectorized<c10::complex<float>> rsqrt() const {
|
| 312 |
+
return sqrt().reciprocal();
|
| 313 |
+
}
|
| 314 |
+
Vectorized<c10::complex<float>> pow(const Vectorized<c10::complex<float>> &exp) const {
|
| 315 |
+
__at_align__ c10::complex<float> x_tmp[size()];
|
| 316 |
+
__at_align__ c10::complex<float> y_tmp[size()];
|
| 317 |
+
store(x_tmp);
|
| 318 |
+
exp.store(y_tmp);
|
| 319 |
+
for (const auto i : c10::irange(size())) {
|
| 320 |
+
x_tmp[i] = std::pow(x_tmp[i], y_tmp[i]);
|
| 321 |
+
}
|
| 322 |
+
return loadu(x_tmp);
|
| 323 |
+
}
|
| 324 |
+
// Comparison using the _CMP_**_OQ predicate.
|
| 325 |
+
// `O`: get false if an operand is NaN
|
| 326 |
+
// `Q`: do not raise if an operand is NaN
|
| 327 |
+
Vectorized<c10::complex<float>> operator==(const Vectorized<c10::complex<float>>& other) const {
|
| 328 |
+
return _mm256_cmp_ps(values, other.values, _CMP_EQ_OQ);
|
| 329 |
+
}
|
| 330 |
+
Vectorized<c10::complex<float>> operator!=(const Vectorized<c10::complex<float>>& other) const {
|
| 331 |
+
return _mm256_cmp_ps(values, other.values, _CMP_NEQ_UQ);
|
| 332 |
+
}
|
| 333 |
+
Vectorized<c10::complex<float>> operator<(const Vectorized<c10::complex<float>>& /*other*/) const {
|
| 334 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 335 |
+
}
|
| 336 |
+
Vectorized<c10::complex<float>> operator<=(const Vectorized<c10::complex<float>>& /*other*/) const {
|
| 337 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 338 |
+
}
|
| 339 |
+
Vectorized<c10::complex<float>> operator>(const Vectorized<c10::complex<float>>& /*other*/) const {
|
| 340 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 341 |
+
}
|
| 342 |
+
Vectorized<c10::complex<float>> operator>=(const Vectorized<c10::complex<float>>& /*other*/) const {
|
| 343 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
Vectorized<c10::complex<float>> eq(const Vectorized<c10::complex<float>>& other) const;
|
| 347 |
+
Vectorized<c10::complex<float>> ne(const Vectorized<c10::complex<float>>& other) const;
|
| 348 |
+
};
|
| 349 |
+
|
| 350 |
+
template <> Vectorized<c10::complex<float>> inline operator+(const Vectorized<c10::complex<float>> &a, const Vectorized<c10::complex<float>> &b) {
|
| 351 |
+
return _mm256_add_ps(a, b);
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
template <> Vectorized<c10::complex<float>> inline operator-(const Vectorized<c10::complex<float>> &a, const Vectorized<c10::complex<float>> &b) {
|
| 355 |
+
return _mm256_sub_ps(a, b);
|
| 356 |
+
}
|
| 357 |
+
|
| 358 |
+
template <> Vectorized<c10::complex<float>> inline operator*(const Vectorized<c10::complex<float>> &a, const Vectorized<c10::complex<float>> &b) {
|
| 359 |
+
//(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
|
| 360 |
+
const __m256 sign_mask = _mm256_setr_ps(0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0);
|
| 361 |
+
auto ac_bd = _mm256_mul_ps(a, b); //ac bd
|
| 362 |
+
|
| 363 |
+
auto d_c = _mm256_permute_ps(b, 0xB1); //d c
|
| 364 |
+
d_c = _mm256_xor_ps(sign_mask, d_c); //d -c
|
| 365 |
+
auto ad_bc = _mm256_mul_ps(a, d_c); //ad -bc
|
| 366 |
+
|
| 367 |
+
auto ret = _mm256_hsub_ps(ac_bd, ad_bc); //ac - bd ad + bc
|
| 368 |
+
ret = _mm256_permute_ps(ret, 0xD8);
|
| 369 |
+
return ret;
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
template <> Vectorized<c10::complex<float>> inline operator/(const Vectorized<c10::complex<float>> &a, const Vectorized<c10::complex<float>> &b) {
|
| 373 |
+
//re + im*i = (a + bi) / (c + di)
|
| 374 |
+
auto mask = _mm256_set1_ps(-0.f);
|
| 375 |
+
auto fabs_cd = _mm256_andnot_ps(mask, b); // |c| |d|
|
| 376 |
+
auto fabs_dc = _mm256_permute_ps(fabs_cd, 0xB1); // |d| |c|
|
| 377 |
+
auto scale = _mm256_rcp_ps(_mm256_max_ps(fabs_cd, fabs_dc)); // 1/sc 1/sc
|
| 378 |
+
auto a2 = _mm256_mul_ps(a, scale); // a/sc b/sc
|
| 379 |
+
auto b2 = _mm256_mul_ps(b, scale); // c/sc d/sc
|
| 380 |
+
auto acbd2 = _mm256_mul_ps(a2, b2);
|
| 381 |
+
|
| 382 |
+
const __m256 sign_mask = _mm256_setr_ps(-0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0);
|
| 383 |
+
auto dc2 = _mm256_permute_ps(b2, 0xB1); // d/sc c/sc
|
| 384 |
+
dc2 = _mm256_xor_ps(sign_mask, dc2); // -d/|c,d| c/sc
|
| 385 |
+
auto adbc2 = _mm256_mul_ps(a2, dc2); //-ad/sc^2 bc/sc^2
|
| 386 |
+
auto res2 = _mm256_hadd_ps(acbd2, adbc2); //(ac+bd)/sc^2 (bc-ad)/sc^2
|
| 387 |
+
res2 = _mm256_permute_ps(res2, 0xD8);
|
| 388 |
+
|
| 389 |
+
// get the denominator
|
| 390 |
+
auto denom2 = Vectorized<c10::complex<float>>(b2).abs_2_(); // (c^2+d^2)/sc^2 (c^2+d^2)/sc^2
|
| 391 |
+
res2 = _mm256_div_ps(res2, denom2);
|
| 392 |
+
return res2;
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
// reciprocal. Implement this here so we can use multiplication.
|
| 396 |
+
inline Vectorized<c10::complex<float>> Vectorized<c10::complex<float>>::reciprocal() const {
|
| 397 |
+
//re + im*i = (a + bi) / (c + di)
|
| 398 |
+
//re = (ac + bd)/abs_2() = c/abs_2()
|
| 399 |
+
//im = (bc - ad)/abs_2() = d/abs_2()
|
| 400 |
+
const __m256 sign_mask = _mm256_setr_ps(0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0);
|
| 401 |
+
auto c_d = _mm256_xor_ps(sign_mask, values); //c -d
|
| 402 |
+
return _mm256_div_ps(c_d, abs_2_());
|
| 403 |
+
}
|
| 404 |
+
|
| 405 |
+
inline Vectorized<c10::complex<float>> Vectorized<c10::complex<float>>::atan() const {
|
| 406 |
+
// atan(x) = i/2 * ln((i + z)/(i - z))
|
| 407 |
+
const __m256 i = _mm256_setr_ps(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
|
| 408 |
+
const Vectorized i_half = _mm256_setr_ps(0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5);
|
| 409 |
+
|
| 410 |
+
auto sum = Vectorized(_mm256_add_ps(i, values)); // a 1+b
|
| 411 |
+
auto sub = Vectorized(_mm256_sub_ps(i, values)); // -a 1-b
|
| 412 |
+
auto ln = (sum/sub).log(); // ln((i + z)/(i - z))
|
| 413 |
+
return i_half*ln; // i/2*ln()
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
template <>
|
| 417 |
+
Vectorized<c10::complex<float>> inline maximum(const Vectorized<c10::complex<float>>& a, const Vectorized<c10::complex<float>>& b) {
|
| 418 |
+
auto abs_a = a.abs_2_();
|
| 419 |
+
auto abs_b = b.abs_2_();
|
| 420 |
+
auto mask = _mm256_cmp_ps(abs_a, abs_b, _CMP_LT_OQ);
|
| 421 |
+
auto max = _mm256_blendv_ps(a, b, mask);
|
| 422 |
+
// Exploit the fact that all-ones is a NaN.
|
| 423 |
+
auto isnan = _mm256_cmp_ps(abs_a, abs_b, _CMP_UNORD_Q);
|
| 424 |
+
return _mm256_or_ps(max, isnan);
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
template <>
|
| 428 |
+
Vectorized<c10::complex<float>> inline minimum(const Vectorized<c10::complex<float>>& a, const Vectorized<c10::complex<float>>& b) {
|
| 429 |
+
auto abs_a = a.abs_2_();
|
| 430 |
+
auto abs_b = b.abs_2_();
|
| 431 |
+
auto mask = _mm256_cmp_ps(abs_a, abs_b, _CMP_GT_OQ);
|
| 432 |
+
auto min = _mm256_blendv_ps(a, b, mask);
|
| 433 |
+
// Exploit the fact that all-ones is a NaN.
|
| 434 |
+
auto isnan = _mm256_cmp_ps(abs_a, abs_b, _CMP_UNORD_Q);
|
| 435 |
+
return _mm256_or_ps(min, isnan);
|
| 436 |
+
}
|
| 437 |
+
|
| 438 |
+
template <>
|
| 439 |
+
Vectorized<c10::complex<float>> inline operator&(const Vectorized<c10::complex<float>>& a, const Vectorized<c10::complex<float>>& b) {
|
| 440 |
+
return _mm256_and_ps(a, b);
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
template <>
|
| 444 |
+
Vectorized<c10::complex<float>> inline operator|(const Vectorized<c10::complex<float>>& a, const Vectorized<c10::complex<float>>& b) {
|
| 445 |
+
return _mm256_or_ps(a, b);
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
template <>
|
| 449 |
+
Vectorized<c10::complex<float>> inline operator^(const Vectorized<c10::complex<float>>& a, const Vectorized<c10::complex<float>>& b) {
|
| 450 |
+
return _mm256_xor_ps(a, b);
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
inline Vectorized<c10::complex<float>> Vectorized<c10::complex<float>>::eq(
|
| 454 |
+
const Vectorized<c10::complex<float>>& other) const {
|
| 455 |
+
auto eq = (*this == other); // compares real and imag individually
|
| 456 |
+
// If both real numbers and imag numbers are equal, then the complex numbers are equal
|
| 457 |
+
return (eq.real() & eq.imag()) & Vectorized<c10::complex<float>>(_mm256_set1_ps(1.0f));
|
| 458 |
+
}
|
| 459 |
+
|
| 460 |
+
inline Vectorized<c10::complex<float>> Vectorized<c10::complex<float>>::ne(
|
| 461 |
+
const Vectorized<c10::complex<float>>& other) const {
|
| 462 |
+
auto ne = (*this != other); // compares real and imag individually
|
| 463 |
+
// If either real numbers or imag numbers are not equal, then the complex numbers are not equal
|
| 464 |
+
return (ne.real() | ne.imag()) & Vectorized<c10::complex<float>>(_mm256_set1_ps(1.0f));
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
#endif
|
| 468 |
+
|
| 469 |
+
}} // namespace at::vec::CPU_CAPABILITY
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vec256_convert.h
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/cpu/vec/functional_bfloat16.h>
|
| 4 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 5 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 6 |
+
#include <ATen/cpu/vec/vec_convert.h>
|
| 7 |
+
|
| 8 |
+
namespace at::vec {
|
| 9 |
+
inline namespace CPU_CAPABILITY {
|
| 10 |
+
|
| 11 |
+
#if defined(CPU_CAPABILITY_AVX2) && !defined(_MSC_VER)
|
| 12 |
+
|
| 13 |
+
template <>
|
| 14 |
+
struct VecConvert<float, 1, BFloat16, 1> {
|
| 15 |
+
static inline VectorizedN<float, 1> apply(
|
| 16 |
+
const VectorizedN<BFloat16, 1>& src) {
|
| 17 |
+
VectorizedN<float, 1> result;
|
| 18 |
+
__m256 value;
|
| 19 |
+
cvtbf16_fp32(_mm256_castsi256_si128(src[0]), value);
|
| 20 |
+
result[0] = value;
|
| 21 |
+
return result;
|
| 22 |
+
}
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
template <>
|
| 26 |
+
struct VecConvert<float, 1, Half, 1> {
|
| 27 |
+
static inline VectorizedN<float, 1> apply(const VectorizedN<Half, 1>& src) {
|
| 28 |
+
VectorizedN<float, 1> result;
|
| 29 |
+
__m256 value;
|
| 30 |
+
cvtfp16_fp32(_mm256_castsi256_si128(src[0]), value);
|
| 31 |
+
result[0] = value;
|
| 32 |
+
return result;
|
| 33 |
+
}
|
| 34 |
+
};
|
| 35 |
+
|
| 36 |
+
template <>
|
| 37 |
+
struct VecConvert<BFloat16, 1, float, 1> {
|
| 38 |
+
static inline VectorizedN<BFloat16, 1> apply(
|
| 39 |
+
const VectorizedN<float, 1>& src) {
|
| 40 |
+
VectorizedN<BFloat16, 1> result;
|
| 41 |
+
result[0] = _mm256_castsi128_si256(cvtfp32_bf16(src[0]));
|
| 42 |
+
return result;
|
| 43 |
+
}
|
| 44 |
+
};
|
| 45 |
+
|
| 46 |
+
template <>
|
| 47 |
+
struct VecConvert<BFloat16, 1, float, 2> {
|
| 48 |
+
static inline VectorizedN<BFloat16, 1> apply(
|
| 49 |
+
const VectorizedN<float, 2>& src) {
|
| 50 |
+
VectorizedN<BFloat16, 1> result;
|
| 51 |
+
result[0] = convert_float_bfloat16(src[0], src[1]);
|
| 52 |
+
return result;
|
| 53 |
+
}
|
| 54 |
+
};
|
| 55 |
+
|
| 56 |
+
template <>
|
| 57 |
+
struct VecConvert<float, 2, BFloat16, 1> {
|
| 58 |
+
static inline VectorizedN<float, 2> apply(
|
| 59 |
+
const VectorizedN<BFloat16, 1>& src) {
|
| 60 |
+
VectorizedN<float, 2> result;
|
| 61 |
+
std::tie(result[0], result[1]) = convert_bfloat16_float(src[0]);
|
| 62 |
+
return result;
|
| 63 |
+
}
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
template <>
|
| 67 |
+
struct VecConvert<Half, 1, float, 1> {
|
| 68 |
+
static inline VectorizedN<Half, 1> apply(const VectorizedN<float, 1>& src) {
|
| 69 |
+
VectorizedN<Half, 1> result;
|
| 70 |
+
result[0] = _mm256_castsi128_si256(cvtfp32_fp16(src[0]));
|
| 71 |
+
return result;
|
| 72 |
+
}
|
| 73 |
+
};
|
| 74 |
+
|
| 75 |
+
template <>
|
| 76 |
+
struct VecConvert<Half, 1, float, 2> {
|
| 77 |
+
static inline VectorizedN<Half, 1> apply(const VectorizedN<float, 2>& src) {
|
| 78 |
+
VectorizedN<Half, 1> result;
|
| 79 |
+
result[0] = convert_float_half(src[0], src[1]);
|
| 80 |
+
return result;
|
| 81 |
+
}
|
| 82 |
+
};
|
| 83 |
+
|
| 84 |
+
template <>
|
| 85 |
+
struct VecConvert<float, 2, Half, 1> {
|
| 86 |
+
static inline VectorizedN<float, 2> apply(const VectorizedN<Half, 1>& src) {
|
| 87 |
+
VectorizedN<float, 2> result;
|
| 88 |
+
std::tie(result[0], result[1]) = convert_half_float(src[0]);
|
| 89 |
+
return result;
|
| 90 |
+
}
|
| 91 |
+
};
|
| 92 |
+
|
| 93 |
+
template <>
|
| 94 |
+
inline Vectorized<double> convert_to_fp_of_same_size<double>(
|
| 95 |
+
const Vectorized<int64_t>& src);
|
| 96 |
+
|
| 97 |
+
template <>
|
| 98 |
+
struct VecConvert<float, 1, int64_t, 2> {
|
| 99 |
+
static inline VectorizedN<float, 1> apply(
|
| 100 |
+
const VectorizedN<int64_t, 2>& src) {
|
| 101 |
+
auto low_double = at::vec::convert_to_fp_of_same_size<double>(src[0]);
|
| 102 |
+
auto low = _mm256_cvtpd_ps(low_double);
|
| 103 |
+
auto high_double = at::vec::convert_to_fp_of_same_size<double>(src[1]);
|
| 104 |
+
auto high = _mm256_cvtpd_ps(high_double);
|
| 105 |
+
return Vectorized<float>(
|
| 106 |
+
_mm256_insertf128_ps(_mm256_castps128_ps256(low), high, 1));
|
| 107 |
+
}
|
| 108 |
+
};
|
| 109 |
+
|
| 110 |
+
template <>
|
| 111 |
+
struct VecConvert<int64_t, 2, float, 1> {
|
| 112 |
+
static inline VectorizedN<int64_t, 2> apply(
|
| 113 |
+
const VectorizedN<float, 1>& src) {
|
| 114 |
+
// Scalarization is the most reliable way of converting fp to int64 on AVX2.
|
| 115 |
+
// Check: https://stackoverflow.com/questions/41144668
|
| 116 |
+
float buffer[8];
|
| 117 |
+
src.store(buffer);
|
| 118 |
+
at::vec::VectorizedN<int64_t, 2> result;
|
| 119 |
+
result[0] = Vectorized<int64_t>(
|
| 120 |
+
static_cast<int64_t>(buffer[0]),
|
| 121 |
+
static_cast<int64_t>(buffer[1]),
|
| 122 |
+
static_cast<int64_t>(buffer[2]),
|
| 123 |
+
static_cast<int64_t>(buffer[3]));
|
| 124 |
+
result[1] = Vectorized<int64_t>(
|
| 125 |
+
static_cast<int64_t>(buffer[4]),
|
| 126 |
+
static_cast<int64_t>(buffer[5]),
|
| 127 |
+
static_cast<int64_t>(buffer[6]),
|
| 128 |
+
static_cast<int64_t>(buffer[7]));
|
| 129 |
+
return result;
|
| 130 |
+
}
|
| 131 |
+
};
|
| 132 |
+
|
| 133 |
+
template <>
|
| 134 |
+
struct VecConvert<int32_t, 1, int64_t, 2> {
|
| 135 |
+
static inline VectorizedN<int32_t, 1> apply(
|
| 136 |
+
const VectorizedN<int64_t, 2>& src) {
|
| 137 |
+
auto low = _mm256_shuffle_epi32(src[0], _MM_SHUFFLE(2, 0, 2, 0));
|
| 138 |
+
auto high = _mm256_shuffle_epi32(src[1], _MM_SHUFFLE(2, 0, 2, 0));
|
| 139 |
+
auto low_perm = _mm256_permute4x64_epi64(low, _MM_SHUFFLE(3, 1, 2, 0));
|
| 140 |
+
auto high_perm = _mm256_permute4x64_epi64(high, _MM_SHUFFLE(3, 1, 2, 0));
|
| 141 |
+
return Vectorized<int32_t>(_mm256_blend_epi32(low_perm, high_perm, 0xF0));
|
| 142 |
+
}
|
| 143 |
+
};
|
| 144 |
+
|
| 145 |
+
template <>
|
| 146 |
+
struct VecConvert<int64_t, 2, int32_t, 1> {
|
| 147 |
+
static inline VectorizedN<int64_t, 2> apply(
|
| 148 |
+
const VectorizedN<int32_t, 1>& src) {
|
| 149 |
+
at::vec::VectorizedN<int64_t, 2> result;
|
| 150 |
+
result[0] = _mm256_cvtepi32_epi64(_mm256_castsi256_si128(src[0]));
|
| 151 |
+
result[1] = _mm256_cvtepi32_epi64(_mm256_extracti128_si256(src[0], 1));
|
| 152 |
+
return result;
|
| 153 |
+
}
|
| 154 |
+
};
|
| 155 |
+
|
| 156 |
+
template <>
|
| 157 |
+
struct VecConvert<int32_t, 1, int8_t, 1> {
|
| 158 |
+
static inline VectorizedN<int32_t, 1> apply(
|
| 159 |
+
const VectorizedN<int8_t, 1>& src) {
|
| 160 |
+
auto src128 = _mm256_castsi256_si128(src[0]);
|
| 161 |
+
return Vectorized<int32_t>(_mm256_cvtepi8_epi32(src128));
|
| 162 |
+
}
|
| 163 |
+
};
|
| 164 |
+
|
| 165 |
+
template <>
|
| 166 |
+
struct VecConvert<int32_t, 1, uint8_t, 1> {
|
| 167 |
+
static inline VectorizedN<int32_t, 1> apply(
|
| 168 |
+
const VectorizedN<uint8_t, 1>& src) {
|
| 169 |
+
auto src128 = _mm256_castsi256_si128(src[0]);
|
| 170 |
+
return Vectorized<int32_t>(_mm256_cvtepu8_epi32(src128));
|
| 171 |
+
}
|
| 172 |
+
};
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
template <>
|
| 176 |
+
struct VecConvert<int32_t, 1, float, 1> {
|
| 177 |
+
static inline VectorizedN<int32_t, 1> apply(
|
| 178 |
+
const VectorizedN<float, 1>& src) {
|
| 179 |
+
return Vectorized<int32_t>(_mm256_cvttps_epi32(src[0]));
|
| 180 |
+
}
|
| 181 |
+
};
|
| 182 |
+
|
| 183 |
+
template <>
|
| 184 |
+
struct VecConvert<float, 1, int32_t, 1> {
|
| 185 |
+
static inline VectorizedN<float, 1> apply(
|
| 186 |
+
const VectorizedN<int32_t, 1>& src) {
|
| 187 |
+
return Vectorized<float>(_mm256_cvtepi32_ps(src[0]));
|
| 188 |
+
}
|
| 189 |
+
};
|
| 190 |
+
|
| 191 |
+
template <>
|
| 192 |
+
struct VecConvert<int16_t, 1, uint8_t, 1> {
|
| 193 |
+
static inline VectorizedN<int16_t, 1> apply(
|
| 194 |
+
const VectorizedN<uint8_t, 1>& src) {
|
| 195 |
+
auto src128 = _mm256_castsi256_si128(src[0]);
|
| 196 |
+
return Vectorized<int16_t>(_mm256_cvtepu8_epi16(src128));
|
| 197 |
+
}
|
| 198 |
+
};
|
| 199 |
+
|
| 200 |
+
template <typename dst_t, typename src_t>
|
| 201 |
+
struct VecConvert<
|
| 202 |
+
dst_t,
|
| 203 |
+
1,
|
| 204 |
+
src_t,
|
| 205 |
+
1,
|
| 206 |
+
typename std::enable_if_t<
|
| 207 |
+
(is_reduced_floating_point_v<dst_t> && is_8bit_integer_v<src_t>) ||
|
| 208 |
+
(is_reduced_floating_point_v<src_t> && is_8bit_integer_v<dst_t>),
|
| 209 |
+
void>> {
|
| 210 |
+
static inline VectorizedN<dst_t, 1> apply(const VectorizedN<src_t, 1>& src) {
|
| 211 |
+
VectorizedN<float, 1> tmp_fp32 = VecConvert<float, 1, src_t, 1>::apply(src);
|
| 212 |
+
return VecConvert<dst_t, 1, float, 1>::apply(tmp_fp32);
|
| 213 |
+
}
|
| 214 |
+
};
|
| 215 |
+
|
| 216 |
+
template <typename dst_t>
|
| 217 |
+
struct VecConvert<
|
| 218 |
+
dst_t,
|
| 219 |
+
1,
|
| 220 |
+
float,
|
| 221 |
+
1,
|
| 222 |
+
typename std::enable_if_t<is_8bit_integer_v<dst_t>,
|
| 223 |
+
void>> {
|
| 224 |
+
static inline VectorizedN<dst_t, 1> apply(const VectorizedN<float, 1>& src) {
|
| 225 |
+
return convert_float_to_int8<dst_t>(src[0]);
|
| 226 |
+
}
|
| 227 |
+
};
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
template <typename dst_t>
|
| 231 |
+
struct VecConvert<
|
| 232 |
+
dst_t,
|
| 233 |
+
1,
|
| 234 |
+
int64_t,
|
| 235 |
+
2,
|
| 236 |
+
typename std::enable_if<
|
| 237 |
+
std::is_same_v<dst_t, int8_t> ||
|
| 238 |
+
std::is_same_v<dst_t, uint8_t>>::type> {
|
| 239 |
+
static inline VectorizedN<dst_t, 1> apply(
|
| 240 |
+
const VectorizedN<int64_t, 2>& src) {
|
| 241 |
+
return VecConvert<dst_t, 1, int32_t, 1>::apply(
|
| 242 |
+
VecConvert<int32_t, 1, int64_t, 2>::apply(src));
|
| 243 |
+
}
|
| 244 |
+
};
|
| 245 |
+
|
| 246 |
+
#endif /* defined(CPU_CAPABILITY_AVX2) && !defined(_MSC_VER) */
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
#if (defined(CPU_CAPABILITY_AVX2) && !defined(_MSC_VER)) || defined(CPU_CAPABILITY_NEON)
|
| 250 |
+
template <typename src_t>
|
| 251 |
+
struct VecConvert<
|
| 252 |
+
float,
|
| 253 |
+
1,
|
| 254 |
+
src_t,
|
| 255 |
+
1,
|
| 256 |
+
typename std::enable_if_t<is_8bit_integer_v<src_t>,
|
| 257 |
+
void>> {
|
| 258 |
+
static inline VectorizedN<float, 1> apply(const VectorizedN<src_t, 1>& src) {
|
| 259 |
+
return convert_int8_to_float<src_t>(src[0]);
|
| 260 |
+
}
|
| 261 |
+
};
|
| 262 |
+
#endif
|
| 263 |
+
|
| 264 |
+
#if defined(CPU_CAPABILITY_NEON)
|
| 265 |
+
template <>
|
| 266 |
+
struct VecConvert<float, 1, BFloat16, 1> {
|
| 267 |
+
static inline VectorizedN<float, 1> apply(
|
| 268 |
+
const VectorizedN<BFloat16, 1>& src) {
|
| 269 |
+
VectorizedN<float, 1> result;
|
| 270 |
+
uint16x8_t u16_8 = vld1q_u16(reinterpret_cast<const uint16_t*>(&src[0]));
|
| 271 |
+
int32x4_t shift = vdupq_n_s32(16);
|
| 272 |
+
auto u16_low1 = vget_low_u16(u16_8);
|
| 273 |
+
auto u16_high1 = vget_high_u16(u16_8);
|
| 274 |
+
float32x4_t f32x4_0 = vreinterpretq_f32_u32(vshlq_u32(vmovl_u16(u16_low1), shift));
|
| 275 |
+
float32x4_t f32x4_1 = vreinterpretq_f32_u32(vshlq_u32(vmovl_u16(u16_high1), shift));
|
| 276 |
+
result[0] = {f32x4_0, f32x4_1};
|
| 277 |
+
return result;
|
| 278 |
+
}
|
| 279 |
+
};
|
| 280 |
+
#endif
|
| 281 |
+
|
| 282 |
+
template <typename src_t>
|
| 283 |
+
struct VecConvert<
|
| 284 |
+
float,
|
| 285 |
+
1,
|
| 286 |
+
src_t,
|
| 287 |
+
1,
|
| 288 |
+
typename std::enable_if_t<is_reduced_floating_point_v<src_t>, void>> {
|
| 289 |
+
static inline VectorizedN<float, 1> apply(const VectorizedN<src_t, 1>& src) {
|
| 290 |
+
auto [res_vec1, res_vec2] = convert_to_float<src_t>(src[0]);
|
| 291 |
+
return res_vec1;
|
| 292 |
+
}
|
| 293 |
+
};
|
| 294 |
+
|
| 295 |
+
template <typename dst_t>
|
| 296 |
+
struct VecConvert<
|
| 297 |
+
dst_t,
|
| 298 |
+
1,
|
| 299 |
+
float,
|
| 300 |
+
1,
|
| 301 |
+
typename std::enable_if_t<is_reduced_floating_point_v<dst_t>, void>> {
|
| 302 |
+
static inline VectorizedN<dst_t, 1> apply(const VectorizedN<float, 1>& src) {
|
| 303 |
+
return convert_from_float<dst_t>(src[0], src[0]);
|
| 304 |
+
}
|
| 305 |
+
};
|
| 306 |
+
|
| 307 |
+
} // namespace CPU_CAPABILITY
|
| 308 |
+
} // namespace at::vec
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vec256_float.h
ADDED
|
@@ -0,0 +1,656 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// DO NOT DEFINE STATIC DATA IN THIS HEADER!
|
| 4 |
+
// See Note [Do not compile initializers with AVX]
|
| 5 |
+
|
| 6 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 7 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 8 |
+
#include <c10/util/irange.h>
|
| 9 |
+
#if defined(CPU_CAPABILITY_AVX2)
|
| 10 |
+
#define SLEEF_STATIC_LIBS
|
| 11 |
+
#include <sleef.h>
|
| 12 |
+
#endif
|
| 13 |
+
|
| 14 |
+
namespace at::vec {
|
| 15 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 16 |
+
inline namespace CPU_CAPABILITY {
|
| 17 |
+
|
| 18 |
+
#if defined(CPU_CAPABILITY_AVX2)
|
| 19 |
+
|
| 20 |
+
template <> class Vectorized<float> {
|
| 21 |
+
private:
|
| 22 |
+
__m256 values;
|
| 23 |
+
public:
|
| 24 |
+
using value_type = float;
|
| 25 |
+
using size_type = int;
|
| 26 |
+
static constexpr size_type size() {
|
| 27 |
+
return 8;
|
| 28 |
+
}
|
| 29 |
+
Vectorized() {}
|
| 30 |
+
Vectorized(__m256 v) : values(v) {}
|
| 31 |
+
Vectorized(float val) {
|
| 32 |
+
values = _mm256_set1_ps(val);
|
| 33 |
+
}
|
| 34 |
+
Vectorized(float val1, float val2, float val3, float val4,
|
| 35 |
+
float val5, float val6, float val7, float val8) {
|
| 36 |
+
values = _mm256_setr_ps(val1, val2, val3, val4, val5, val6, val7, val8);
|
| 37 |
+
}
|
| 38 |
+
operator __m256() const {
|
| 39 |
+
return values;
|
| 40 |
+
}
|
| 41 |
+
template <int64_t mask>
|
| 42 |
+
static Vectorized<float> blend(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 43 |
+
return _mm256_blend_ps(a.values, b.values, mask);
|
| 44 |
+
}
|
| 45 |
+
static Vectorized<float> blendv(const Vectorized<float>& a, const Vectorized<float>& b,
|
| 46 |
+
const Vectorized<float>& mask) {
|
| 47 |
+
return _mm256_blendv_ps(a.values, b.values, mask.values);
|
| 48 |
+
}
|
| 49 |
+
template<typename step_t>
|
| 50 |
+
static Vectorized<float> arange(float base = 0.f, step_t step = static_cast<step_t>(1)) {
|
| 51 |
+
return Vectorized<float>(
|
| 52 |
+
base, base + step, base + 2 * step, base + 3 * step,
|
| 53 |
+
base + 4 * step, base + 5 * step, base + 6 * step, base + 7 * step);
|
| 54 |
+
}
|
| 55 |
+
static Vectorized<float> set(const Vectorized<float>& a, const Vectorized<float>& b,
|
| 56 |
+
int64_t count = size()) {
|
| 57 |
+
switch (count) {
|
| 58 |
+
case 0:
|
| 59 |
+
return a;
|
| 60 |
+
case 1:
|
| 61 |
+
return blend<1>(a, b);
|
| 62 |
+
case 2:
|
| 63 |
+
return blend<3>(a, b);
|
| 64 |
+
case 3:
|
| 65 |
+
return blend<7>(a, b);
|
| 66 |
+
case 4:
|
| 67 |
+
return blend<15>(a, b);
|
| 68 |
+
case 5:
|
| 69 |
+
return blend<31>(a, b);
|
| 70 |
+
case 6:
|
| 71 |
+
return blend<63>(a, b);
|
| 72 |
+
case 7:
|
| 73 |
+
return blend<127>(a, b);
|
| 74 |
+
}
|
| 75 |
+
return b;
|
| 76 |
+
}
|
| 77 |
+
static Vectorized<float> loadu(const void* ptr, int64_t count = size()) {
|
| 78 |
+
if (count == size())
|
| 79 |
+
return _mm256_loadu_ps(reinterpret_cast<const float*>(ptr));
|
| 80 |
+
__at_align__ float tmp_values[size()];
|
| 81 |
+
// Ensure uninitialized memory does not change the output value See https://github.com/pytorch/pytorch/issues/32502
|
| 82 |
+
// for more details. We do not initialize arrays to zero using "={0}" because gcc would compile it to two
|
| 83 |
+
// instructions while a loop would be compiled to one instruction.
|
| 84 |
+
for (const auto i : c10::irange(size())) {
|
| 85 |
+
tmp_values[i] = 0.0;
|
| 86 |
+
}
|
| 87 |
+
std::memcpy(
|
| 88 |
+
tmp_values, reinterpret_cast<const float*>(ptr), count * sizeof(float));
|
| 89 |
+
return _mm256_loadu_ps(tmp_values);
|
| 90 |
+
}
|
| 91 |
+
void store(void* ptr, int64_t count = size()) const {
|
| 92 |
+
if (count == size()) {
|
| 93 |
+
_mm256_storeu_ps(reinterpret_cast<float*>(ptr), values);
|
| 94 |
+
} else if (count > 0) {
|
| 95 |
+
float tmp_values[size()];
|
| 96 |
+
_mm256_storeu_ps(reinterpret_cast<float*>(tmp_values), values);
|
| 97 |
+
std::memcpy(ptr, tmp_values, count * sizeof(float));
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
const float& operator[](int idx) const = delete;
|
| 101 |
+
float& operator[](int idx) = delete;
|
| 102 |
+
int zero_mask() const {
|
| 103 |
+
// returns an integer mask where all zero elements are translated to 1-bit and others are translated to 0-bit
|
| 104 |
+
__m256 cmp = _mm256_cmp_ps(values, _mm256_set1_ps(0.0f), _CMP_EQ_OQ);
|
| 105 |
+
return _mm256_movemask_ps(cmp);
|
| 106 |
+
}
|
| 107 |
+
Vectorized<float> isnan() const {
|
| 108 |
+
return _mm256_cmp_ps(values, _mm256_set1_ps(0.0f), _CMP_UNORD_Q);
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
bool has_inf_nan() const {
|
| 112 |
+
__m256 self_sub = _mm256_sub_ps(values, values);
|
| 113 |
+
return (_mm256_movemask_epi8(_mm256_castps_si256(self_sub)) & 0x77777777) != 0;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
Vectorized<float> map(float (*const f)(float)) const {
|
| 117 |
+
__at_align__ float tmp[size()];
|
| 118 |
+
store(tmp);
|
| 119 |
+
for (const auto i : c10::irange(size())) {
|
| 120 |
+
tmp[i] = f(tmp[i]);
|
| 121 |
+
}
|
| 122 |
+
return loadu(tmp);
|
| 123 |
+
}
|
| 124 |
+
Vectorized<float> abs() const {
|
| 125 |
+
auto mask = _mm256_set1_ps(-0.f);
|
| 126 |
+
return _mm256_andnot_ps(mask, values);
|
| 127 |
+
}
|
| 128 |
+
Vectorized<float> angle() const {
|
| 129 |
+
const auto zero_vec = _mm256_set1_ps(0.f);
|
| 130 |
+
const auto nan_vec = _mm256_set1_ps(NAN);
|
| 131 |
+
const auto not_nan_mask = _mm256_cmp_ps(values, values, _CMP_EQ_OQ);
|
| 132 |
+
const auto nan_mask = _mm256_cmp_ps(not_nan_mask, zero_vec, _CMP_EQ_OQ);
|
| 133 |
+
const auto pi = _mm256_set1_ps(c10::pi<float>);
|
| 134 |
+
|
| 135 |
+
const auto neg_mask = _mm256_cmp_ps(values, zero_vec, _CMP_LT_OQ);
|
| 136 |
+
auto angle = _mm256_blendv_ps(zero_vec, pi, neg_mask);
|
| 137 |
+
angle = _mm256_blendv_ps(angle, nan_vec, nan_mask);
|
| 138 |
+
return angle;
|
| 139 |
+
}
|
| 140 |
+
Vectorized<float> real() const {
|
| 141 |
+
return *this;
|
| 142 |
+
}
|
| 143 |
+
Vectorized<float> imag() const {
|
| 144 |
+
return _mm256_set1_ps(0);
|
| 145 |
+
}
|
| 146 |
+
Vectorized<float> conj() const {
|
| 147 |
+
return *this;
|
| 148 |
+
}
|
| 149 |
+
Vectorized<float> acos() const {
|
| 150 |
+
return Vectorized<float>(Sleef_acosf8_u10(values));
|
| 151 |
+
}
|
| 152 |
+
Vectorized<float> acosh() const {
|
| 153 |
+
return Vectorized<float>(Sleef_acoshf8_u10(values));
|
| 154 |
+
}
|
| 155 |
+
Vectorized<float> asin() const {
|
| 156 |
+
return Vectorized<float>(Sleef_asinf8_u10(values));
|
| 157 |
+
}
|
| 158 |
+
Vectorized<float> atan() const {
|
| 159 |
+
return Vectorized<float>(Sleef_atanf8_u10(values));
|
| 160 |
+
}
|
| 161 |
+
Vectorized<float> atanh() const {
|
| 162 |
+
return Vectorized<float>(Sleef_atanhf8_u10(values));
|
| 163 |
+
}
|
| 164 |
+
Vectorized<float> atan2(const Vectorized<float> &b) const {
|
| 165 |
+
return Vectorized<float>(Sleef_atan2f8_u10(values, b));
|
| 166 |
+
}
|
| 167 |
+
Vectorized<float> copysign(const Vectorized<float> &sign) const {
|
| 168 |
+
return Vectorized<float>(Sleef_copysignf8(values, sign));
|
| 169 |
+
}
|
| 170 |
+
Vectorized<float> erf() const {
|
| 171 |
+
// constants
|
| 172 |
+
const auto neg_zero_vec = _mm256_set1_ps(-0.f);
|
| 173 |
+
const auto one_vec = _mm256_set1_ps(1.0f);
|
| 174 |
+
const auto p = _mm256_set1_ps(0.3275911f);
|
| 175 |
+
const auto p1 = _mm256_set1_ps(0.254829592f);
|
| 176 |
+
const auto p2 = _mm256_set1_ps(-0.284496736f);
|
| 177 |
+
const auto p3 = _mm256_set1_ps(1.421413741f);
|
| 178 |
+
const auto p4 = _mm256_set1_ps(-1.453152027f);
|
| 179 |
+
const auto p5 = _mm256_set1_ps(1.061405429f);
|
| 180 |
+
// sign(x)
|
| 181 |
+
auto sign_mask = _mm256_and_ps(neg_zero_vec, values);
|
| 182 |
+
auto abs_vec = _mm256_xor_ps(sign_mask, values);
|
| 183 |
+
// t = 1 / (p * abs(x) + 1)
|
| 184 |
+
auto tmp0 = _mm256_fmadd_ps(p, abs_vec, one_vec);
|
| 185 |
+
auto t = _mm256_div_ps(one_vec, tmp0);
|
| 186 |
+
// r = p5 * t ^ 4 + p4 * t ^ 3 + p3 * t ^ 2 + p2 * t + p1
|
| 187 |
+
auto tmp1 = _mm256_fmadd_ps(p5, t, p4);
|
| 188 |
+
auto tmp2 = _mm256_fmadd_ps(tmp1, t, p3);
|
| 189 |
+
auto tmp3 = _mm256_fmadd_ps(tmp2, t, p2);
|
| 190 |
+
auto r = _mm256_fmadd_ps(tmp3, t, p1);
|
| 191 |
+
// - exp(- x * x)
|
| 192 |
+
auto pow_2 = _mm256_mul_ps(values, values);
|
| 193 |
+
auto neg_pow_2 = _mm256_xor_ps(neg_zero_vec, pow_2);
|
| 194 |
+
// auto tmp4 = exp(neg_pow_2);
|
| 195 |
+
auto tmp4 = Vectorized<float>(Sleef_expf8_u10(neg_pow_2));
|
| 196 |
+
auto tmp5 = _mm256_xor_ps(neg_zero_vec, tmp4);
|
| 197 |
+
// erf(x) = sign(x) * (1 - r * t * exp(- x * x))
|
| 198 |
+
auto tmp6 = _mm256_mul_ps(tmp5, t);
|
| 199 |
+
auto tmp7 = _mm256_fmadd_ps(tmp6, r, one_vec);
|
| 200 |
+
return _mm256_xor_ps(sign_mask, tmp7);
|
| 201 |
+
}
|
| 202 |
+
Vectorized<float> erfc() const {
|
| 203 |
+
return Vectorized<float>(Sleef_erfcf8_u15(values));
|
| 204 |
+
}
|
| 205 |
+
Vectorized<float> erfinv() const {
|
| 206 |
+
return map(calc_erfinv);
|
| 207 |
+
}
|
| 208 |
+
Vectorized<float> exp() const {
|
| 209 |
+
return Vectorized<float>(Sleef_expf8_u10(values));
|
| 210 |
+
}
|
| 211 |
+
Vectorized<float> exp2() const {
|
| 212 |
+
return Vectorized<float>(Sleef_exp2f8_u10(values));
|
| 213 |
+
}
|
| 214 |
+
Vectorized<float> expm1() const {
|
| 215 |
+
return Vectorized<float>(Sleef_expm1f8_u10(values));
|
| 216 |
+
}
|
| 217 |
+
Vectorized<float> exp_u20() const {
|
| 218 |
+
// A faster version of exp with ULP=20
|
| 219 |
+
static __m256 vec_factorial_1 =
|
| 220 |
+
_mm256_set1_ps(0.999999701f); // 1/factorial(1)
|
| 221 |
+
static __m256 vec_factorial_2 =
|
| 222 |
+
_mm256_set1_ps(0.499991506f); // 1/factorial(2)
|
| 223 |
+
static __m256 vec_factorial_3 =
|
| 224 |
+
_mm256_set1_ps(0.166676521f); // 1/factorial(3)
|
| 225 |
+
static __m256 vec_factorial_4 =
|
| 226 |
+
_mm256_set1_ps(0.0418978221f); // 1/factorial(4)
|
| 227 |
+
static __m256 vec_factorial_5 =
|
| 228 |
+
_mm256_set1_ps(0.00828929059f); // 1/factorial(5)
|
| 229 |
+
static __m256 vec_exp_log2ef =
|
| 230 |
+
_mm256_castsi256_ps(_mm256_set1_epi32(0x3fb8aa3b)); // log2(e)
|
| 231 |
+
static __m256 vec_half = _mm256_set1_ps(0.5f);
|
| 232 |
+
static __m256 vec_one = _mm256_set1_ps(1.f);
|
| 233 |
+
static __m256 vec_zero = _mm256_set1_ps(0.f);
|
| 234 |
+
static __m256 vec_two = _mm256_set1_ps(2.f);
|
| 235 |
+
static __m256 vec_ln2f = _mm256_castsi256_ps(_mm256_set1_epi32(0x3f317218)); // ln(2)
|
| 236 |
+
static __m256 vec_ln_flt_min = _mm256_castsi256_ps(_mm256_set1_epi32(0xc2aeac50));
|
| 237 |
+
static __m256 vec_ln_flt_max = _mm256_castsi256_ps(_mm256_set1_epi32(0x42b17218));
|
| 238 |
+
static __m256i vec_127 = _mm256_set1_epi32(0x0000007f);
|
| 239 |
+
static int n_mantissa_bits = 23;
|
| 240 |
+
|
| 241 |
+
// exp(x) =
|
| 242 |
+
// = exp(n * ln(2) + r) // divide x by ln(2) and get quot and rem
|
| 243 |
+
// = 2^n * exp(r) // simplify the exp(n*ln(2)) expression
|
| 244 |
+
|
| 245 |
+
auto less_ln_flt_min_mask =
|
| 246 |
+
_mm256_cmp_ps(values, vec_ln_flt_min, 1 /*_CMP_LT_OS*/);
|
| 247 |
+
auto vec_src = _mm256_min_ps(values, vec_ln_flt_max);
|
| 248 |
+
vec_src = _mm256_max_ps(vec_src, vec_ln_flt_min);
|
| 249 |
+
|
| 250 |
+
// fx = floorf(x * log2ef + 0.5)
|
| 251 |
+
auto vec_fx = _mm256_fmadd_ps(vec_src, vec_exp_log2ef, vec_half);
|
| 252 |
+
vec_fx = _mm256_floor_ps(vec_fx);
|
| 253 |
+
|
| 254 |
+
// x = x - fx * ln2
|
| 255 |
+
auto vec_exp_poly = _mm256_fnmadd_ps(vec_fx, vec_ln2f, vec_src);
|
| 256 |
+
|
| 257 |
+
// compute polynomial
|
| 258 |
+
auto vec_res =
|
| 259 |
+
_mm256_fmadd_ps(vec_exp_poly, vec_factorial_5, vec_factorial_4);
|
| 260 |
+
vec_res = _mm256_fmadd_ps(vec_exp_poly, vec_res, vec_factorial_3);
|
| 261 |
+
vec_res = _mm256_fmadd_ps(vec_exp_poly, vec_res, vec_factorial_2);
|
| 262 |
+
vec_res = _mm256_fmadd_ps(vec_exp_poly, vec_res, vec_factorial_1);
|
| 263 |
+
vec_res = _mm256_fmadd_ps(vec_exp_poly, vec_res, vec_one);
|
| 264 |
+
|
| 265 |
+
// compute 2^(n-1)
|
| 266 |
+
auto vec_exp_number = _mm256_sub_ps(vec_fx, vec_one);
|
| 267 |
+
auto vec_exp_number_i = _mm256_cvtps_epi32(vec_exp_number);
|
| 268 |
+
auto vec_two_pow_n_i = _mm256_add_epi32(vec_exp_number_i, vec_127);
|
| 269 |
+
vec_two_pow_n_i = _mm256_slli_epi32(vec_two_pow_n_i, n_mantissa_bits);
|
| 270 |
+
auto vec_two_pow_n = _mm256_castsi256_ps(vec_two_pow_n_i);
|
| 271 |
+
vec_two_pow_n =
|
| 272 |
+
_mm256_blendv_ps(vec_two_pow_n, vec_zero, less_ln_flt_min_mask);
|
| 273 |
+
|
| 274 |
+
// y = y * 2^n
|
| 275 |
+
vec_res = _mm256_mul_ps(vec_res, vec_two_pow_n);
|
| 276 |
+
vec_res = _mm256_mul_ps(vec_res, vec_two);
|
| 277 |
+
return vec_res;
|
| 278 |
+
}
|
| 279 |
+
Vectorized<float> fmod(const Vectorized<float>& q) const {
|
| 280 |
+
return Vectorized<float>(Sleef_fmodf8(values, q));
|
| 281 |
+
}
|
| 282 |
+
Vectorized<float> log() const {
|
| 283 |
+
return Vectorized<float>(Sleef_logf8_u10(values));
|
| 284 |
+
}
|
| 285 |
+
Vectorized<float> log2() const {
|
| 286 |
+
return Vectorized<float>(Sleef_log2f8_u10(values));
|
| 287 |
+
}
|
| 288 |
+
Vectorized<float> log10() const {
|
| 289 |
+
return Vectorized<float>(Sleef_log10f8_u10(values));
|
| 290 |
+
}
|
| 291 |
+
Vectorized<float> log1p() const {
|
| 292 |
+
return Vectorized<float>(Sleef_log1pf8_u10(values));
|
| 293 |
+
}
|
| 294 |
+
Vectorized<float> frac() const;
|
| 295 |
+
Vectorized<float> sin() const {
|
| 296 |
+
return Vectorized<float>(Sleef_sinf8_u35(values));
|
| 297 |
+
}
|
| 298 |
+
Vectorized<float> sinh() const {
|
| 299 |
+
return Vectorized<float>(Sleef_sinhf8_u10(values));
|
| 300 |
+
}
|
| 301 |
+
Vectorized<float> cos() const {
|
| 302 |
+
return Vectorized<float>(Sleef_cosf8_u35(values));
|
| 303 |
+
}
|
| 304 |
+
Vectorized<float> cosh() const {
|
| 305 |
+
return Vectorized<float>(Sleef_coshf8_u10(values));
|
| 306 |
+
}
|
| 307 |
+
Vectorized<float> ceil() const {
|
| 308 |
+
return _mm256_ceil_ps(values);
|
| 309 |
+
}
|
| 310 |
+
Vectorized<float> floor() const {
|
| 311 |
+
return _mm256_floor_ps(values);
|
| 312 |
+
}
|
| 313 |
+
Vectorized<float> hypot(const Vectorized<float> &b) const {
|
| 314 |
+
return Vectorized<float>(Sleef_hypotf8_u05(values, b));
|
| 315 |
+
}
|
| 316 |
+
Vectorized<float> i0() const {
|
| 317 |
+
return map(calc_i0);
|
| 318 |
+
}
|
| 319 |
+
Vectorized<float> i0e() const {
|
| 320 |
+
return map(calc_i0e);
|
| 321 |
+
}
|
| 322 |
+
Vectorized<float> digamma() const {
|
| 323 |
+
return map(calc_digamma);
|
| 324 |
+
}
|
| 325 |
+
Vectorized<float> igamma(const Vectorized<float> &x) const {
|
| 326 |
+
__at_align__ float tmp[size()];
|
| 327 |
+
__at_align__ float tmp_x[size()];
|
| 328 |
+
store(tmp);
|
| 329 |
+
x.store(tmp_x);
|
| 330 |
+
for (const auto i : c10::irange(size())) {
|
| 331 |
+
tmp[i] = calc_igamma(tmp[i], tmp_x[i]);
|
| 332 |
+
}
|
| 333 |
+
return loadu(tmp);
|
| 334 |
+
}
|
| 335 |
+
Vectorized<float> igammac(const Vectorized<float> &x) const {
|
| 336 |
+
__at_align__ float tmp[size()];
|
| 337 |
+
__at_align__ float tmp_x[size()];
|
| 338 |
+
store(tmp);
|
| 339 |
+
x.store(tmp_x);
|
| 340 |
+
for (const auto i : c10::irange(size())) {
|
| 341 |
+
tmp[i] = calc_igammac(tmp[i], tmp_x[i]);
|
| 342 |
+
}
|
| 343 |
+
return loadu(tmp);
|
| 344 |
+
}
|
| 345 |
+
Vectorized<float> neg() const {
|
| 346 |
+
return _mm256_xor_ps(_mm256_set1_ps(-0.f), values);
|
| 347 |
+
}
|
| 348 |
+
Vectorized<float> nextafter(const Vectorized<float> &b) const {
|
| 349 |
+
return Vectorized<float>(Sleef_nextafterf8(values, b));
|
| 350 |
+
}
|
| 351 |
+
Vectorized<float> round() const {
|
| 352 |
+
return _mm256_round_ps(values, (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
|
| 353 |
+
}
|
| 354 |
+
Vectorized<float> tan() const {
|
| 355 |
+
return Vectorized<float>(Sleef_tanf8_u10(values));
|
| 356 |
+
}
|
| 357 |
+
Vectorized<float> tanh() const {
|
| 358 |
+
return Vectorized<float>(Sleef_tanhf8_u10(values));
|
| 359 |
+
}
|
| 360 |
+
Vectorized<float> trunc() const {
|
| 361 |
+
return _mm256_round_ps(values, (_MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC));
|
| 362 |
+
}
|
| 363 |
+
Vectorized<float> lgamma() const {
|
| 364 |
+
return Vectorized<float>(Sleef_lgammaf8_u10(values));
|
| 365 |
+
}
|
| 366 |
+
Vectorized<float> sqrt() const {
|
| 367 |
+
return _mm256_sqrt_ps(values);
|
| 368 |
+
}
|
| 369 |
+
Vectorized<float> reciprocal() const {
|
| 370 |
+
return _mm256_div_ps(_mm256_set1_ps(1), values);
|
| 371 |
+
}
|
| 372 |
+
Vectorized<float> rsqrt() const {
|
| 373 |
+
return _mm256_div_ps(_mm256_set1_ps(1), _mm256_sqrt_ps(values));
|
| 374 |
+
}
|
| 375 |
+
Vectorized<float> pow(const Vectorized<float> &b) const {
|
| 376 |
+
return Vectorized<float>(Sleef_powf8_u10(values, b));
|
| 377 |
+
}
|
| 378 |
+
// Comparison using the _CMP_**_OQ predicate.
|
| 379 |
+
// `O`: get false if an operand is NaN
|
| 380 |
+
// `Q`: do not raise if an operand is NaN
|
| 381 |
+
Vectorized<float> operator==(const Vectorized<float>& other) const {
|
| 382 |
+
return _mm256_cmp_ps(values, other.values, _CMP_EQ_OQ);
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
Vectorized<float> operator!=(const Vectorized<float>& other) const {
|
| 386 |
+
return _mm256_cmp_ps(values, other.values, _CMP_NEQ_UQ);
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
Vectorized<float> operator<(const Vectorized<float>& other) const {
|
| 390 |
+
return _mm256_cmp_ps(values, other.values, _CMP_LT_OQ);
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
Vectorized<float> operator<=(const Vectorized<float>& other) const {
|
| 394 |
+
return _mm256_cmp_ps(values, other.values, _CMP_LE_OQ);
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
Vectorized<float> operator>(const Vectorized<float>& other) const {
|
| 398 |
+
return _mm256_cmp_ps(values, other.values, _CMP_GT_OQ);
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
Vectorized<float> operator>=(const Vectorized<float>& other) const {
|
| 402 |
+
return _mm256_cmp_ps(values, other.values, _CMP_GE_OQ);
|
| 403 |
+
}
|
| 404 |
+
|
| 405 |
+
Vectorized<float> eq(const Vectorized<float>& other) const;
|
| 406 |
+
Vectorized<float> ne(const Vectorized<float>& other) const;
|
| 407 |
+
Vectorized<float> gt(const Vectorized<float>& other) const;
|
| 408 |
+
Vectorized<float> ge(const Vectorized<float>& other) const;
|
| 409 |
+
Vectorized<float> lt(const Vectorized<float>& other) const;
|
| 410 |
+
Vectorized<float> le(const Vectorized<float>& other) const;
|
| 411 |
+
};
|
| 412 |
+
|
| 413 |
+
template <>
|
| 414 |
+
Vectorized<float> inline operator+(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 415 |
+
return _mm256_add_ps(a, b);
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
template <>
|
| 419 |
+
Vectorized<float> inline operator-(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 420 |
+
return _mm256_sub_ps(a, b);
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
template <>
|
| 424 |
+
Vectorized<float> inline operator*(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 425 |
+
return _mm256_mul_ps(a, b);
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
+
template <>
|
| 429 |
+
Vectorized<float> inline operator/(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 430 |
+
return _mm256_div_ps(a, b);
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
// frac. Implement this here so we can use subtraction
|
| 434 |
+
inline Vectorized<float> Vectorized<float>::frac() const {
|
| 435 |
+
return *this - this->trunc();
|
| 436 |
+
}
|
| 437 |
+
|
| 438 |
+
// Implements the IEEE 754 201X `maximum` operation, which propagates NaN if
|
| 439 |
+
// either input is a NaN.
|
| 440 |
+
template <>
|
| 441 |
+
Vectorized<float> inline maximum(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 442 |
+
Vectorized<float> max = _mm256_max_ps(a, b);
|
| 443 |
+
Vectorized<float> isnan = _mm256_cmp_ps(a, b, _CMP_UNORD_Q);
|
| 444 |
+
// Exploit the fact that all-ones is a NaN.
|
| 445 |
+
return _mm256_or_ps(max, isnan);
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
// Implements the IEEE 754 201X `minimum` operation, which propagates NaN if
|
| 449 |
+
// either input is a NaN.
|
| 450 |
+
template <>
|
| 451 |
+
Vectorized<float> inline minimum(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 452 |
+
Vectorized<float> min = _mm256_min_ps(a, b);
|
| 453 |
+
Vectorized<float> isnan = _mm256_cmp_ps(a, b, _CMP_UNORD_Q);
|
| 454 |
+
// Exploit the fact that all-ones is a NaN.
|
| 455 |
+
return _mm256_or_ps(min, isnan);
|
| 456 |
+
}
|
| 457 |
+
|
| 458 |
+
template <>
|
| 459 |
+
Vectorized<float> inline clamp(const Vectorized<float>& a, const Vectorized<float>& min, const Vectorized<float>& max) {
|
| 460 |
+
return _mm256_min_ps(max, _mm256_max_ps(min, a));
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
template <>
|
| 464 |
+
Vectorized<float> inline clamp_max(const Vectorized<float>& a, const Vectorized<float>& max) {
|
| 465 |
+
return _mm256_min_ps(max, a);
|
| 466 |
+
}
|
| 467 |
+
|
| 468 |
+
template <>
|
| 469 |
+
Vectorized<float> inline clamp_min(const Vectorized<float>& a, const Vectorized<float>& min) {
|
| 470 |
+
return _mm256_max_ps(min, a);
|
| 471 |
+
}
|
| 472 |
+
|
| 473 |
+
template <>
|
| 474 |
+
Vectorized<float> inline operator&(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 475 |
+
return _mm256_and_ps(a, b);
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
template <>
|
| 479 |
+
Vectorized<float> inline operator|(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 480 |
+
return _mm256_or_ps(a, b);
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
template <>
|
| 484 |
+
Vectorized<float> inline operator^(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 485 |
+
return _mm256_xor_ps(a, b);
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
inline Vectorized<float> Vectorized<float>::eq(const Vectorized<float>& other) const {
|
| 489 |
+
return (*this == other) & Vectorized<float>(1.0f);
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
inline Vectorized<float> Vectorized<float>::ne(const Vectorized<float>& other) const {
|
| 493 |
+
return (*this != other) & Vectorized<float>(1.0f);
|
| 494 |
+
}
|
| 495 |
+
|
| 496 |
+
inline Vectorized<float> Vectorized<float>::gt(const Vectorized<float>& other) const {
|
| 497 |
+
return (*this > other) & Vectorized<float>(1.0f);
|
| 498 |
+
}
|
| 499 |
+
|
| 500 |
+
inline Vectorized<float> Vectorized<float>::ge(const Vectorized<float>& other) const {
|
| 501 |
+
return (*this >= other) & Vectorized<float>(1.0f);
|
| 502 |
+
}
|
| 503 |
+
|
| 504 |
+
inline Vectorized<float> Vectorized<float>::lt(const Vectorized<float>& other) const {
|
| 505 |
+
return (*this < other) & Vectorized<float>(1.0f);
|
| 506 |
+
}
|
| 507 |
+
|
| 508 |
+
inline Vectorized<float> Vectorized<float>::le(const Vectorized<float>& other) const {
|
| 509 |
+
return (*this <= other) & Vectorized<float>(1.0f);
|
| 510 |
+
}
|
| 511 |
+
|
| 512 |
+
template <>
|
| 513 |
+
inline void convert(const float* src, float* dst, int64_t n) {
|
| 514 |
+
int64_t i;
|
| 515 |
+
#ifndef __msvc_cl__
|
| 516 |
+
#pragma unroll
|
| 517 |
+
#endif
|
| 518 |
+
for (i = 0; i <= (n - Vectorized<float>::size()); i += Vectorized<float>::size()) {
|
| 519 |
+
_mm256_storeu_ps(dst + i, _mm256_loadu_ps(src + i));
|
| 520 |
+
}
|
| 521 |
+
#ifndef __msvc_cl__
|
| 522 |
+
#pragma unroll
|
| 523 |
+
#endif
|
| 524 |
+
for (; i < n; i++) {
|
| 525 |
+
dst[i] = src[i];
|
| 526 |
+
}
|
| 527 |
+
}
|
| 528 |
+
|
| 529 |
+
|
| 530 |
+
template <>
|
| 531 |
+
Vectorized<float> inline fmadd(const Vectorized<float>& a, const Vectorized<float>& b, const Vectorized<float>& c) {
|
| 532 |
+
return _mm256_fmadd_ps(a, b, c);
|
| 533 |
+
}
|
| 534 |
+
|
| 535 |
+
template <>
|
| 536 |
+
Vectorized<float> inline fmsub(const Vectorized<float>& a, const Vectorized<float>& b, const Vectorized<float>& c) {
|
| 537 |
+
return _mm256_fmsub_ps(a, b, c);
|
| 538 |
+
}
|
| 539 |
+
|
| 540 |
+
// Used by Inductor CPP codegen
|
| 541 |
+
template<>
|
| 542 |
+
inline void transpose_mxn<float, 8, 8>(
|
| 543 |
+
const float* src,
|
| 544 |
+
int64_t ld_src,
|
| 545 |
+
float* dst,
|
| 546 |
+
int64_t ld_dst) {
|
| 547 |
+
// load from src to registers
|
| 548 |
+
// a: a0 a1 a2 a3 a4 a5 a6 a7
|
| 549 |
+
// b: b0 b1 b2 b3 b4 b5 b6 b7
|
| 550 |
+
// c: c0 c1 c2 c3 c4 c5 c6 c7
|
| 551 |
+
// d: d0 d1 d2 d3 d4 d5 d6 d7
|
| 552 |
+
// e: e0 e1 e2 e3 e4 e5 e6 e7
|
| 553 |
+
// f: f0 f1 f2 f3 f4 f5 f6 f7
|
| 554 |
+
// g: g0 g1 g2 g3 g4 g5 g6 g7
|
| 555 |
+
// h: h0 h1 h2 h3 h4 h5 h6 h7
|
| 556 |
+
__m256 a = _mm256_loadu_ps(&src[0 * ld_src]);
|
| 557 |
+
__m256 b = _mm256_loadu_ps(&src[1 * ld_src]);
|
| 558 |
+
__m256 c = _mm256_loadu_ps(&src[2 * ld_src]);
|
| 559 |
+
__m256 d = _mm256_loadu_ps(&src[3 * ld_src]);
|
| 560 |
+
__m256 e = _mm256_loadu_ps(&src[4 * ld_src]);
|
| 561 |
+
__m256 f = _mm256_loadu_ps(&src[5 * ld_src]);
|
| 562 |
+
__m256 g = _mm256_loadu_ps(&src[6 * ld_src]);
|
| 563 |
+
__m256 h = _mm256_loadu_ps(&src[7 * ld_src]);
|
| 564 |
+
|
| 565 |
+
__m256 ta, tb, tc, td, te, tf, tg, th;
|
| 566 |
+
// unpacking and interleaving 32-bit elements
|
| 567 |
+
// a0 b0 a1 b1 a4 b4 a5 b5
|
| 568 |
+
// a2 b2 a3 b3 a6 b6 a7 b7
|
| 569 |
+
// c0 d0 c1 d1 ...
|
| 570 |
+
// c2 d2 c3 d3 ...
|
| 571 |
+
// e0 f0 e1 f1 ...
|
| 572 |
+
// e2 f2 e3 f3 ...
|
| 573 |
+
// g0 h0 g1 h1 ...
|
| 574 |
+
// g2 h2 g3 h3 ...
|
| 575 |
+
ta = _mm256_unpacklo_ps(a, b);
|
| 576 |
+
tb = _mm256_unpackhi_ps(a, b);
|
| 577 |
+
tc = _mm256_unpacklo_ps(c, d);
|
| 578 |
+
td = _mm256_unpackhi_ps(c, d);
|
| 579 |
+
te = _mm256_unpacklo_ps(e, f);
|
| 580 |
+
tf = _mm256_unpackhi_ps(e, f);
|
| 581 |
+
tg = _mm256_unpacklo_ps(g, h);
|
| 582 |
+
th = _mm256_unpackhi_ps(g, h);
|
| 583 |
+
|
| 584 |
+
// unpacking and interleaving 64-bit elements
|
| 585 |
+
// a0 b0 c0 d0 a4 b4 c4 d4
|
| 586 |
+
// a1 b1 c1 d1 ...
|
| 587 |
+
// a2 b2 c2 d2 ...
|
| 588 |
+
// a3 b3 c3 d3 ...
|
| 589 |
+
// e0 f0 g0 h0 e4 f4 g4 h4
|
| 590 |
+
// e1 f1 g1 h1 ...
|
| 591 |
+
// e2 f2 g2 h2 ...
|
| 592 |
+
// e3 f3 g3 h3 ...
|
| 593 |
+
a = _mm256_castpd_ps(
|
| 594 |
+
_mm256_unpacklo_pd(_mm256_castps_pd(ta), _mm256_castps_pd(tc)));
|
| 595 |
+
b = _mm256_castpd_ps(
|
| 596 |
+
_mm256_unpackhi_pd(_mm256_castps_pd(ta), _mm256_castps_pd(tc)));
|
| 597 |
+
c = _mm256_castpd_ps(
|
| 598 |
+
_mm256_unpacklo_pd(_mm256_castps_pd(tb), _mm256_castps_pd(td)));
|
| 599 |
+
d = _mm256_castpd_ps(
|
| 600 |
+
_mm256_unpackhi_pd(_mm256_castps_pd(tb), _mm256_castps_pd(td)));
|
| 601 |
+
e = _mm256_castpd_ps(
|
| 602 |
+
_mm256_unpacklo_pd(_mm256_castps_pd(te), _mm256_castps_pd(tg)));
|
| 603 |
+
f = _mm256_castpd_ps(
|
| 604 |
+
_mm256_unpackhi_pd(_mm256_castps_pd(te), _mm256_castps_pd(tg)));
|
| 605 |
+
g = _mm256_castpd_ps(
|
| 606 |
+
_mm256_unpacklo_pd(_mm256_castps_pd(tf), _mm256_castps_pd(th)));
|
| 607 |
+
h = _mm256_castpd_ps(
|
| 608 |
+
_mm256_unpackhi_pd(_mm256_castps_pd(tf), _mm256_castps_pd(th)));
|
| 609 |
+
|
| 610 |
+
// shuffle 128-bits (composed of 4 32-bit elements)
|
| 611 |
+
// a0 b0 c0 d0 e0 f0 g0 h0
|
| 612 |
+
// a1 b1 c1 d1 ...
|
| 613 |
+
// a2 b2 c2 d2 ...
|
| 614 |
+
// a3 b3 c3 d3 ...
|
| 615 |
+
// a4 b4 c4 d4 ...
|
| 616 |
+
// a5 b5 c5 d5 ...
|
| 617 |
+
// a6 b6 c6 d6 ...
|
| 618 |
+
// a7 b7 c7 d7 ...
|
| 619 |
+
ta = _mm256_permute2f128_ps(a, e, 0x20);
|
| 620 |
+
tb = _mm256_permute2f128_ps(b, f, 0x20);
|
| 621 |
+
tc = _mm256_permute2f128_ps(c, g, 0x20);
|
| 622 |
+
td = _mm256_permute2f128_ps(d, h, 0x20);
|
| 623 |
+
te = _mm256_permute2f128_ps(a, e, 0x31);
|
| 624 |
+
tf = _mm256_permute2f128_ps(b, f, 0x31);
|
| 625 |
+
tg = _mm256_permute2f128_ps(c, g, 0x31);
|
| 626 |
+
th = _mm256_permute2f128_ps(d, h, 0x31);
|
| 627 |
+
|
| 628 |
+
// store from registers to dst
|
| 629 |
+
_mm256_storeu_ps(&dst[0 * ld_dst], ta);
|
| 630 |
+
_mm256_storeu_ps(&dst[1 * ld_dst], tb);
|
| 631 |
+
_mm256_storeu_ps(&dst[2 * ld_dst], tc);
|
| 632 |
+
_mm256_storeu_ps(&dst[3 * ld_dst], td);
|
| 633 |
+
_mm256_storeu_ps(&dst[4 * ld_dst], te);
|
| 634 |
+
_mm256_storeu_ps(&dst[5 * ld_dst], tf);
|
| 635 |
+
_mm256_storeu_ps(&dst[6 * ld_dst], tg);
|
| 636 |
+
_mm256_storeu_ps(&dst[7 * ld_dst], th);
|
| 637 |
+
}
|
| 638 |
+
|
| 639 |
+
template<>
|
| 640 |
+
inline void transpose_mxn<float, 16, 16>(
|
| 641 |
+
const float* src,
|
| 642 |
+
int64_t ld_src,
|
| 643 |
+
float* dst,
|
| 644 |
+
int64_t ld_dst) {
|
| 645 |
+
transpose_mxn<float, 8, 8>(
|
| 646 |
+
src , ld_src, dst, ld_dst);
|
| 647 |
+
transpose_mxn<float, 8, 8>(
|
| 648 |
+
src + 8, ld_src, dst + 8 * ld_dst, ld_dst);
|
| 649 |
+
transpose_mxn<float, 8, 8>(
|
| 650 |
+
src + 8 * ld_src, ld_src, dst + 8, ld_dst);
|
| 651 |
+
transpose_mxn<float, 8, 8>(
|
| 652 |
+
src + 8 * ld_src + 8, ld_src, dst + 8 * ld_dst + 8, ld_dst);
|
| 653 |
+
}
|
| 654 |
+
#endif
|
| 655 |
+
|
| 656 |
+
}} // namespace at::vec::CPU_CAPABILITY
|
vllm/lib/python3.10/site-packages/torch/include/ATen/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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// DO NOT DEFINE STATIC DATA IN THIS HEADER!
|
| 4 |
+
// See Note [Do not compile initializers with AVX]
|
| 5 |
+
|
| 6 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 7 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 8 |
+
#include <c10/util/irange.h>
|
| 9 |
+
|
| 10 |
+
#if defined(__aarch64__) && defined(AT_BUILD_ARM_VEC256_WITH_SLEEF)
|
| 11 |
+
#include <sleef.h>
|
| 12 |
+
#endif
|
| 13 |
+
|
| 14 |
+
// Sleef offers vectorized versions of some transcedentals
|
| 15 |
+
// such as sin, cos, tan etc..
|
| 16 |
+
// However for now opting for STL, since we are not building
|
| 17 |
+
// with Sleef for mobile yet.
|
| 18 |
+
|
| 19 |
+
namespace at::vec {
|
| 20 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 21 |
+
inline namespace CPU_CAPABILITY {
|
| 22 |
+
|
| 23 |
+
// Right now contains only aarch64 implementation.
|
| 24 |
+
// Due to follow two reasons aarch32 is not currently supported.
|
| 25 |
+
// 1. Due to difference in ISA been aarch32 and aarch64, intrinsics
|
| 26 |
+
// that work for aarch64 dont work for aarch32.
|
| 27 |
+
// 2. Android NDK r21 has problems with compiling aarch32.
|
| 28 |
+
// Clang seg faults.
|
| 29 |
+
// https://github.com/android/ndk/issues/1248
|
| 30 |
+
// https://bugs.llvm.org/show_bug.cgi?id=45824
|
| 31 |
+
// Most likely we will do aarch32 support with inline asm.
|
| 32 |
+
#if defined(__aarch64__)
|
| 33 |
+
|
| 34 |
+
#ifdef __BIG_ENDIAN__
|
| 35 |
+
#error "Big endian is not supported."
|
| 36 |
+
#endif
|
| 37 |
+
|
| 38 |
+
#if defined(AT_BUILD_ARM_VEC256_WITH_SLEEF)
|
| 39 |
+
#define USE_SLEEF(sleef_code, non_sleef_code) sleef_code
|
| 40 |
+
#else
|
| 41 |
+
#define USE_SLEEF(sleef_code, non_sleef_code) non_sleef_code
|
| 42 |
+
#endif
|
| 43 |
+
|
| 44 |
+
template<int index, bool mask_val>
|
| 45 |
+
struct BlendRegs {
|
| 46 |
+
static float32x4_t impl(
|
| 47 |
+
const float32x4_t& a, const float32x4_t& b, float32x4_t& res);
|
| 48 |
+
};
|
| 49 |
+
|
| 50 |
+
template<int index>
|
| 51 |
+
struct BlendRegs<index, true>{
|
| 52 |
+
static float32x4_t impl(
|
| 53 |
+
const float32x4_t& a, const float32x4_t& b, float32x4_t& res) {
|
| 54 |
+
return vsetq_lane_f32(vgetq_lane_f32(b, index), res, index);
|
| 55 |
+
}
|
| 56 |
+
};
|
| 57 |
+
|
| 58 |
+
template<int index>
|
| 59 |
+
struct BlendRegs<index, false>{
|
| 60 |
+
static float32x4_t impl(
|
| 61 |
+
const float32x4_t& a, const float32x4_t& b, float32x4_t& res) {
|
| 62 |
+
return vsetq_lane_f32(vgetq_lane_f32(a, index), res, index);
|
| 63 |
+
}
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
template <> class Vectorized<float> {
|
| 67 |
+
private:
|
| 68 |
+
float32x4x2_t values;
|
| 69 |
+
public:
|
| 70 |
+
using value_type = float;
|
| 71 |
+
using size_type = int;
|
| 72 |
+
static constexpr size_type size() {
|
| 73 |
+
return 8;
|
| 74 |
+
}
|
| 75 |
+
Vectorized() {}
|
| 76 |
+
Vectorized(float32x4x2_t v) : values(v) {}
|
| 77 |
+
Vectorized(float val) : values{vdupq_n_f32(val), vdupq_n_f32(val) } {}
|
| 78 |
+
Vectorized(float val0, float val1, float val2, float val3,
|
| 79 |
+
float val4, float val5, float val6, float val7) :
|
| 80 |
+
values{val0, val1, val2, val3, val4, val5, val6, val7} {}
|
| 81 |
+
Vectorized(float32x4_t val0, float32x4_t val1) : values{val0, val1} {}
|
| 82 |
+
operator float32x4x2_t() const {
|
| 83 |
+
return values;
|
| 84 |
+
}
|
| 85 |
+
template <int64_t mask>
|
| 86 |
+
static Vectorized<float> blend(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 87 |
+
Vectorized<float> vec;
|
| 88 |
+
// 0.
|
| 89 |
+
vec.values.val[0] =
|
| 90 |
+
BlendRegs<0, (mask & 0x01)!=0>::impl(
|
| 91 |
+
a.values.val[0], b.values.val[0], vec.values.val[0]);
|
| 92 |
+
vec.values.val[0] =
|
| 93 |
+
BlendRegs<1, (mask & 0x02)!=0>::impl(
|
| 94 |
+
a.values.val[0], b.values.val[0], vec.values.val[0]);
|
| 95 |
+
vec.values.val[0] =
|
| 96 |
+
BlendRegs<2, (mask & 0x04)!=0>::impl(
|
| 97 |
+
a.values.val[0], b.values.val[0], vec.values.val[0]);
|
| 98 |
+
vec.values.val[0] =
|
| 99 |
+
BlendRegs<3, (mask & 0x08)!=0>::impl(
|
| 100 |
+
a.values.val[0], b.values.val[0], vec.values.val[0]);
|
| 101 |
+
// 1.
|
| 102 |
+
vec.values.val[1] =
|
| 103 |
+
BlendRegs<0, (mask & 0x10)!=0>::impl(
|
| 104 |
+
a.values.val[1], b.values.val[1], vec.values.val[1]);
|
| 105 |
+
vec.values.val[1] =
|
| 106 |
+
BlendRegs<1, (mask & 0x20)!=0>::impl(
|
| 107 |
+
a.values.val[1], b.values.val[1], vec.values.val[1]);
|
| 108 |
+
vec.values.val[1] =
|
| 109 |
+
BlendRegs<2, (mask & 0x40)!=0>::impl(
|
| 110 |
+
a.values.val[1], b.values.val[1], vec.values.val[1]);
|
| 111 |
+
vec.values.val[1] =
|
| 112 |
+
BlendRegs<3, (mask & 0x80)!=0>::impl(
|
| 113 |
+
a.values.val[1], b.values.val[1], vec.values.val[1]);
|
| 114 |
+
return vec;
|
| 115 |
+
}
|
| 116 |
+
static Vectorized<float> blendv(const Vectorized<float>& a, const Vectorized<float>& b,
|
| 117 |
+
const Vectorized<float>& mask) {
|
| 118 |
+
// TODO
|
| 119 |
+
// NB: This requires that each value, i.e., each uint value,
|
| 120 |
+
// of the mask either all be zeros or all be 1s.
|
| 121 |
+
// We perhaps need some kind of an assert?
|
| 122 |
+
// But that will affect performance.
|
| 123 |
+
Vectorized<float> vec(mask.values);
|
| 124 |
+
vec.values.val[0] = vbslq_f32(
|
| 125 |
+
vreinterpretq_u32_f32(vec.values.val[0]),
|
| 126 |
+
b.values.val[0],
|
| 127 |
+
a.values.val[0]);
|
| 128 |
+
vec.values.val[1] = vbslq_f32(
|
| 129 |
+
vreinterpretq_u32_f32(vec.values.val[1]),
|
| 130 |
+
b.values.val[1],
|
| 131 |
+
a.values.val[1]);
|
| 132 |
+
return vec;
|
| 133 |
+
}
|
| 134 |
+
template<typename step_t>
|
| 135 |
+
static Vectorized<float> arange(float base = 0.f, step_t step = static_cast<step_t>(1)) {
|
| 136 |
+
const Vectorized<float> base_vec(base);
|
| 137 |
+
const Vectorized<float> step_vec(step);
|
| 138 |
+
const Vectorized<float> step_sizes(0, 1, 2, 3, 4, 5, 6, 7);
|
| 139 |
+
return fmadd(step_sizes, step_vec, base_vec);
|
| 140 |
+
}
|
| 141 |
+
static Vectorized<float> set(const Vectorized<float>& a, const Vectorized<float>& b,
|
| 142 |
+
int64_t count = size()) {
|
| 143 |
+
switch (count) {
|
| 144 |
+
case 0:
|
| 145 |
+
return a;
|
| 146 |
+
case 1:
|
| 147 |
+
{
|
| 148 |
+
Vectorized<float> vec;
|
| 149 |
+
static uint32x4_t mask_low = {0xFFFFFFFF, 0x0, 0x0, 0x0};
|
| 150 |
+
vec.values.val[0] = vreinterpretq_f32_u32(mask_low);
|
| 151 |
+
vec.values.val[1] = a.values.val[1];
|
| 152 |
+
vec.values.val[0] = vbslq_f32(
|
| 153 |
+
vreinterpretq_u32_f32(vec.values.val[0]),
|
| 154 |
+
b.values.val[0],
|
| 155 |
+
a.values.val[0]);
|
| 156 |
+
return vec;
|
| 157 |
+
}
|
| 158 |
+
case 2:
|
| 159 |
+
{
|
| 160 |
+
Vectorized<float> vec;
|
| 161 |
+
static uint32x4_t mask_low = {0xFFFFFFFF, 0xFFFFFFFF, 0x0, 0x0};
|
| 162 |
+
vec.values.val[0] = vreinterpretq_f32_u32(mask_low);
|
| 163 |
+
vec.values.val[1] = a.values.val[1];
|
| 164 |
+
vec.values.val[0] = vbslq_f32(
|
| 165 |
+
vreinterpretq_u32_f32(vec.values.val[0]),
|
| 166 |
+
b.values.val[0],
|
| 167 |
+
a.values.val[0]);
|
| 168 |
+
return vec;
|
| 169 |
+
}
|
| 170 |
+
case 3:
|
| 171 |
+
{
|
| 172 |
+
Vectorized<float> vec;
|
| 173 |
+
static uint32x4_t mask_low = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0};
|
| 174 |
+
vec.values.val[0] = vreinterpretq_f32_u32(mask_low);
|
| 175 |
+
vec.values.val[1] = a.values.val[1];
|
| 176 |
+
vec.values.val[0] = vbslq_f32(
|
| 177 |
+
vreinterpretq_u32_f32(vec.values.val[0]),
|
| 178 |
+
b.values.val[0],
|
| 179 |
+
a.values.val[0]);
|
| 180 |
+
return vec;
|
| 181 |
+
}
|
| 182 |
+
case 4:
|
| 183 |
+
return Vectorized<float>(b.values.val[0], a.values.val[1]);
|
| 184 |
+
case 5:
|
| 185 |
+
{
|
| 186 |
+
Vectorized<float> vec;
|
| 187 |
+
static uint32x4_t mask_high = {0xFFFFFFFF, 0x0, 0x0, 0x0};
|
| 188 |
+
vec.values.val[0] = b.values.val[0];
|
| 189 |
+
vec.values.val[1] = vreinterpretq_f32_u32(mask_high);
|
| 190 |
+
vec.values.val[1] = vbslq_f32(
|
| 191 |
+
vreinterpretq_u32_f32(vec.values.val[1]),
|
| 192 |
+
b.values.val[1],
|
| 193 |
+
a.values.val[1]);
|
| 194 |
+
return vec;
|
| 195 |
+
}
|
| 196 |
+
case 6:
|
| 197 |
+
{
|
| 198 |
+
Vectorized<float> vec;
|
| 199 |
+
static uint32x4_t mask_high = {0xFFFFFFFF, 0xFFFFFFFF, 0x0, 0x0};
|
| 200 |
+
vec.values.val[0] = b.values.val[0];
|
| 201 |
+
vec.values.val[1] = vreinterpretq_f32_u32(mask_high);
|
| 202 |
+
vec.values.val[1] = vbslq_f32(
|
| 203 |
+
vreinterpretq_u32_f32(vec.values.val[1]),
|
| 204 |
+
b.values.val[1],
|
| 205 |
+
a.values.val[1]);
|
| 206 |
+
return vec;
|
| 207 |
+
}
|
| 208 |
+
case 7:
|
| 209 |
+
{
|
| 210 |
+
Vectorized<float> vec;
|
| 211 |
+
static uint32x4_t mask_high = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0};
|
| 212 |
+
vec.values.val[0] = b.values.val[0];
|
| 213 |
+
vec.values.val[1] = vreinterpretq_f32_u32(mask_high);
|
| 214 |
+
vec.values.val[1] = vbslq_f32(
|
| 215 |
+
vreinterpretq_u32_f32(vec.values.val[1]),
|
| 216 |
+
b.values.val[1],
|
| 217 |
+
a.values.val[1]);
|
| 218 |
+
return vec;
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
+
return b;
|
| 222 |
+
}
|
| 223 |
+
static Vectorized<float> loadu(const void* ptr, int64_t count = size()) {
|
| 224 |
+
if (count == size()) {
|
| 225 |
+
return vld1q_f32_x2(reinterpret_cast<const float*>(ptr));
|
| 226 |
+
}
|
| 227 |
+
else if (count == (size() >> 1)) {
|
| 228 |
+
Vectorized<float> res;
|
| 229 |
+
res.values.val[0] = vld1q_f32(reinterpret_cast<const float*>(ptr));
|
| 230 |
+
res.values.val[1] = vdupq_n_f32(0.f);
|
| 231 |
+
return res;
|
| 232 |
+
}
|
| 233 |
+
else {
|
| 234 |
+
__at_align__ float tmp_values[size()];
|
| 235 |
+
for (const auto i : c10::irange(size())) {
|
| 236 |
+
tmp_values[i] = 0.0;
|
| 237 |
+
}
|
| 238 |
+
std::memcpy(
|
| 239 |
+
tmp_values,
|
| 240 |
+
reinterpret_cast<const float*>(ptr),
|
| 241 |
+
count * sizeof(float));
|
| 242 |
+
return vld1q_f32_x2(reinterpret_cast<const float*>(tmp_values));
|
| 243 |
+
}
|
| 244 |
+
}
|
| 245 |
+
void store(void* ptr, int64_t count = size()) const {
|
| 246 |
+
if (count == size()) {
|
| 247 |
+
vst1q_f32_x2(reinterpret_cast<float*>(ptr), values);
|
| 248 |
+
}
|
| 249 |
+
else if (count == (size() >> 1)) {
|
| 250 |
+
vst1q_f32(reinterpret_cast<float*>(ptr), values.val[0]);
|
| 251 |
+
}
|
| 252 |
+
else {
|
| 253 |
+
float tmp_values[size()];
|
| 254 |
+
vst1q_f32_x2(reinterpret_cast<float*>(tmp_values), values);
|
| 255 |
+
std::memcpy(ptr, tmp_values, count * sizeof(float));
|
| 256 |
+
}
|
| 257 |
+
}
|
| 258 |
+
inline const float32x4_t& get_low() const {
|
| 259 |
+
return values.val[0];
|
| 260 |
+
}
|
| 261 |
+
inline float32x4_t& get_low() {
|
| 262 |
+
return values.val[0];
|
| 263 |
+
}
|
| 264 |
+
inline const float32x4_t& get_high() const {
|
| 265 |
+
return values.val[1];
|
| 266 |
+
}
|
| 267 |
+
inline float32x4_t& get_high() {
|
| 268 |
+
return values.val[1];
|
| 269 |
+
}
|
| 270 |
+
// Very slow implementation of indexing.
|
| 271 |
+
// Only required because vec256_qint refers to this.
|
| 272 |
+
// Once we specialize that implementation for ARM
|
| 273 |
+
// this should be removed. TODO (kimishpatel)
|
| 274 |
+
float operator[](int idx) const {
|
| 275 |
+
__at_align__ float tmp[size()];
|
| 276 |
+
store(tmp);
|
| 277 |
+
return tmp[idx];
|
| 278 |
+
}
|
| 279 |
+
float operator[](int idx) {
|
| 280 |
+
__at_align__ float tmp[size()];
|
| 281 |
+
store(tmp);
|
| 282 |
+
return tmp[idx];
|
| 283 |
+
}
|
| 284 |
+
// For boolean version where we want to if any 1/all zero
|
| 285 |
+
// etc. can be done faster in a different way.
|
| 286 |
+
int zero_mask() const {
|
| 287 |
+
__at_align__ float tmp[size()];
|
| 288 |
+
store(tmp);
|
| 289 |
+
int mask = 0;
|
| 290 |
+
for (int i = 0; i < size(); ++ i) {
|
| 291 |
+
if (tmp[i] == 0.f) {
|
| 292 |
+
mask |= (1 << i);
|
| 293 |
+
}
|
| 294 |
+
}
|
| 295 |
+
return mask;
|
| 296 |
+
}
|
| 297 |
+
Vectorized<float> isnan() const {
|
| 298 |
+
__at_align__ float tmp[size()];
|
| 299 |
+
__at_align__ float res[size()];
|
| 300 |
+
store(tmp);
|
| 301 |
+
for (const auto i : c10::irange(size())) {
|
| 302 |
+
if (_isnan(tmp[i])) {
|
| 303 |
+
std::memset(static_cast<void*>(&res[i]), 0xFF, sizeof(float));
|
| 304 |
+
} else {
|
| 305 |
+
std::memset(static_cast<void*>(&res[i]), 0, sizeof(float));
|
| 306 |
+
}
|
| 307 |
+
}
|
| 308 |
+
return loadu(res);
|
| 309 |
+
};
|
| 310 |
+
bool has_inf_nan() const {
|
| 311 |
+
__at_align__ float tmp[size()];
|
| 312 |
+
store(tmp);
|
| 313 |
+
for (const auto i : c10::irange(size())) {
|
| 314 |
+
if(_isnan(tmp[i]) || _isinf(tmp[i])) {
|
| 315 |
+
return true;
|
| 316 |
+
}
|
| 317 |
+
}
|
| 318 |
+
return false;
|
| 319 |
+
}
|
| 320 |
+
Vectorized<float> map(float (*const f)(float)) const {
|
| 321 |
+
__at_align__ float tmp[size()];
|
| 322 |
+
store(tmp);
|
| 323 |
+
for (const auto i : c10::irange(size())) {
|
| 324 |
+
tmp[i] = f(tmp[i]);
|
| 325 |
+
}
|
| 326 |
+
return loadu(tmp);
|
| 327 |
+
}
|
| 328 |
+
Vectorized<float> abs() const {
|
| 329 |
+
return Vectorized<float>(vabsq_f32(values.val[0]), vabsq_f32(values.val[1]));
|
| 330 |
+
}
|
| 331 |
+
Vectorized<float> angle() const {
|
| 332 |
+
auto zero = Vectorized<float>(0);
|
| 333 |
+
auto pi = Vectorized<float>(c10::pi<float>);
|
| 334 |
+
auto tmp = blendv(zero, pi, *this < zero);
|
| 335 |
+
return blendv(tmp, *this, isnan());
|
| 336 |
+
}
|
| 337 |
+
Vectorized<float> real() const {
|
| 338 |
+
return *this;
|
| 339 |
+
}
|
| 340 |
+
Vectorized<float> imag() const {
|
| 341 |
+
return Vectorized<float>(0.f);
|
| 342 |
+
}
|
| 343 |
+
Vectorized<float> conj() const {
|
| 344 |
+
return *this;
|
| 345 |
+
}
|
| 346 |
+
Vectorized<float> acos() const {
|
| 347 |
+
return USE_SLEEF(
|
| 348 |
+
Vectorized<float>(Sleef_acosf4_u10(values.val[0]), Sleef_acosf4_u10(values.val[1])),
|
| 349 |
+
map(std::acos)
|
| 350 |
+
);
|
| 351 |
+
}
|
| 352 |
+
Vectorized<float> acosh() const {
|
| 353 |
+
return USE_SLEEF(
|
| 354 |
+
Vectorized<float>(Sleef_acoshf4_u10(values.val[0]), Sleef_acoshf4_u10(values.val[1])),
|
| 355 |
+
map(std::acosh)
|
| 356 |
+
);
|
| 357 |
+
}
|
| 358 |
+
Vectorized<float> asin() const {
|
| 359 |
+
return USE_SLEEF(
|
| 360 |
+
Vectorized<float>(Sleef_asinf4_u10(values.val[0]), Sleef_asinf4_u10(values.val[1])),
|
| 361 |
+
map(std::asin)
|
| 362 |
+
);
|
| 363 |
+
}
|
| 364 |
+
Vectorized<float> atan() const {
|
| 365 |
+
return USE_SLEEF(
|
| 366 |
+
Vectorized<float>(Sleef_atanf4_u10(values.val[0]), Sleef_atanf4_u10(values.val[1])),
|
| 367 |
+
map(std::atan)
|
| 368 |
+
);
|
| 369 |
+
}
|
| 370 |
+
Vectorized<float> atanh() const {
|
| 371 |
+
return USE_SLEEF(
|
| 372 |
+
Vectorized<float>(Sleef_atanhf4_u10(values.val[0]), Sleef_atanhf4_u10(values.val[1])),
|
| 373 |
+
map(std::atanh)
|
| 374 |
+
);
|
| 375 |
+
}
|
| 376 |
+
Vectorized<float> atan2(const Vectorized<float> &exp) const {
|
| 377 |
+
USE_SLEEF(
|
| 378 |
+
{
|
| 379 |
+
return Vectorized<float>(Sleef_atan2f4_u10(values.val[0], exp.values.val[0]),
|
| 380 |
+
Sleef_atan2f4_u10(values.val[1], exp.values.val[1]));
|
| 381 |
+
},
|
| 382 |
+
{
|
| 383 |
+
__at_align__ float tmp[size()];
|
| 384 |
+
__at_align__ float tmp_exp[size()];
|
| 385 |
+
store(tmp);
|
| 386 |
+
exp.store(tmp_exp);
|
| 387 |
+
for (const auto i : c10::irange(size())) {
|
| 388 |
+
tmp[i] = std::atan2(tmp[i], tmp_exp[i]);
|
| 389 |
+
}
|
| 390 |
+
return loadu(tmp);
|
| 391 |
+
}
|
| 392 |
+
)
|
| 393 |
+
}
|
| 394 |
+
Vectorized<float> copysign(const Vectorized<float> &sign) const {
|
| 395 |
+
USE_SLEEF(
|
| 396 |
+
{
|
| 397 |
+
return Vectorized<float>(Sleef_copysignf4(values.val[0], sign.values.val[0]),
|
| 398 |
+
Sleef_copysignf4(values.val[1], sign.values.val[1]));
|
| 399 |
+
},
|
| 400 |
+
{
|
| 401 |
+
__at_align__ float tmp[size()];
|
| 402 |
+
__at_align__ float tmp_sign[size()];
|
| 403 |
+
store(tmp);
|
| 404 |
+
sign.store(tmp_sign);
|
| 405 |
+
for (size_type i = 0; i < size(); i++) {
|
| 406 |
+
tmp[i] = std::copysign(tmp[i], tmp_sign[i]);
|
| 407 |
+
}
|
| 408 |
+
return loadu(tmp);
|
| 409 |
+
}
|
| 410 |
+
)
|
| 411 |
+
}
|
| 412 |
+
Vectorized<float> erf() const;
|
| 413 |
+
Vectorized<float> erfc() const {
|
| 414 |
+
return USE_SLEEF(
|
| 415 |
+
Vectorized<float>(Sleef_erfcf4_u15(values.val[0]), Sleef_erfcf4_u15(values.val[1])),
|
| 416 |
+
map(std::erfc)
|
| 417 |
+
);
|
| 418 |
+
}
|
| 419 |
+
Vectorized<float> erfinv() const {
|
| 420 |
+
return map(calc_erfinv);
|
| 421 |
+
}
|
| 422 |
+
Vectorized<float> exp() const {
|
| 423 |
+
return USE_SLEEF(
|
| 424 |
+
Vectorized<float>(Sleef_expf4_u10(values.val[0]), Sleef_expf4_u10(values.val[1])),
|
| 425 |
+
map(std::exp)
|
| 426 |
+
);
|
| 427 |
+
}
|
| 428 |
+
Vectorized<float> exp2() const {
|
| 429 |
+
return USE_SLEEF(
|
| 430 |
+
Vectorized<float>(Sleef_exp2f4_u10(values.val[0]), Sleef_exp2f4_u10(values.val[1])),
|
| 431 |
+
map(std::exp2)
|
| 432 |
+
);
|
| 433 |
+
}
|
| 434 |
+
Vectorized<float> expm1() const {
|
| 435 |
+
return USE_SLEEF(
|
| 436 |
+
Vectorized<float>(Sleef_expm1f4_u10(values.val[0]), Sleef_expm1f4_u10(values.val[1])),
|
| 437 |
+
map(std::expm1)
|
| 438 |
+
);
|
| 439 |
+
}
|
| 440 |
+
Vectorized<float> exp_u20() const {
|
| 441 |
+
return exp();
|
| 442 |
+
}
|
| 443 |
+
Vectorized<float> fmod(const Vectorized<float>& q) const {
|
| 444 |
+
USE_SLEEF(
|
| 445 |
+
{
|
| 446 |
+
return Vectorized<float>(Sleef_fmodf4(values.val[0], q.values.val[0]),
|
| 447 |
+
Sleef_fmodf4(values.val[1], q.values.val[1]));
|
| 448 |
+
},
|
| 449 |
+
{
|
| 450 |
+
__at_align__ float tmp[size()];
|
| 451 |
+
__at_align__ float tmp_q[size()];
|
| 452 |
+
store(tmp);
|
| 453 |
+
q.store(tmp_q);
|
| 454 |
+
for (const auto i : c10::irange(size())) {
|
| 455 |
+
tmp[i] = std::fmod(tmp[i], tmp_q[i]);
|
| 456 |
+
}
|
| 457 |
+
return loadu(tmp);
|
| 458 |
+
}
|
| 459 |
+
)
|
| 460 |
+
}
|
| 461 |
+
Vectorized<float> hypot(const Vectorized<float> &b) const {
|
| 462 |
+
USE_SLEEF(
|
| 463 |
+
{
|
| 464 |
+
return Vectorized<float>(Sleef_hypotf4_u05(values.val[0], b.values.val[0]),
|
| 465 |
+
Sleef_hypotf4_u05(values.val[1], b.values.val[1]));
|
| 466 |
+
},
|
| 467 |
+
{
|
| 468 |
+
__at_align__ float tmp[size()];
|
| 469 |
+
__at_align__ float tmp_b[size()];
|
| 470 |
+
store(tmp);
|
| 471 |
+
b.store(tmp_b);
|
| 472 |
+
for (const auto i : c10::irange(size())) {
|
| 473 |
+
tmp[i] = std::hypot(tmp[i], tmp_b[i]);
|
| 474 |
+
}
|
| 475 |
+
return loadu(tmp);
|
| 476 |
+
}
|
| 477 |
+
)
|
| 478 |
+
}
|
| 479 |
+
Vectorized<float> i0() const {
|
| 480 |
+
return map(calc_i0);
|
| 481 |
+
}
|
| 482 |
+
Vectorized<float> i0e() const {
|
| 483 |
+
return map(calc_i0e);
|
| 484 |
+
}
|
| 485 |
+
Vectorized<float> digamma() const {
|
| 486 |
+
return map(calc_digamma);
|
| 487 |
+
}
|
| 488 |
+
Vectorized<float> igamma(const Vectorized<float> &x) const {
|
| 489 |
+
__at_align__ float tmp[size()];
|
| 490 |
+
__at_align__ float tmp_x[size()];
|
| 491 |
+
store(tmp);
|
| 492 |
+
x.store(tmp_x);
|
| 493 |
+
for (const auto i : c10::irange(size())) {
|
| 494 |
+
tmp[i] = calc_igamma(tmp[i], tmp_x[i]);
|
| 495 |
+
}
|
| 496 |
+
return loadu(tmp);
|
| 497 |
+
}
|
| 498 |
+
Vectorized<float> igammac(const Vectorized<float> &x) const {
|
| 499 |
+
__at_align__ float tmp[size()];
|
| 500 |
+
__at_align__ float tmp_x[size()];
|
| 501 |
+
store(tmp);
|
| 502 |
+
x.store(tmp_x);
|
| 503 |
+
for (const auto i : c10::irange(size())) {
|
| 504 |
+
tmp[i] = calc_igammac(tmp[i], tmp_x[i]);
|
| 505 |
+
}
|
| 506 |
+
return loadu(tmp);
|
| 507 |
+
}
|
| 508 |
+
Vectorized<float> log() const {
|
| 509 |
+
return USE_SLEEF(
|
| 510 |
+
Vectorized<float>(Sleef_logf4_u10(values.val[0]), Sleef_logf4_u10(values.val[1])),
|
| 511 |
+
map(std::log)
|
| 512 |
+
);
|
| 513 |
+
}
|
| 514 |
+
Vectorized<float> log10() const {
|
| 515 |
+
return USE_SLEEF(
|
| 516 |
+
Vectorized<float>(Sleef_log10f4_u10(values.val[0]), Sleef_log10f4_u10(values.val[1])),
|
| 517 |
+
map(std::log10)
|
| 518 |
+
);
|
| 519 |
+
}
|
| 520 |
+
Vectorized<float> log1p() const {
|
| 521 |
+
return USE_SLEEF(
|
| 522 |
+
Vectorized<float>(Sleef_log1pf4_u10(values.val[0]), Sleef_log1pf4_u10(values.val[1])),
|
| 523 |
+
map(std::log1p)
|
| 524 |
+
);
|
| 525 |
+
}
|
| 526 |
+
Vectorized<float> log2() const {
|
| 527 |
+
return USE_SLEEF(
|
| 528 |
+
Vectorized<float>(Sleef_log2f4_u10(values.val[0]), Sleef_log2f4_u10(values.val[1])),
|
| 529 |
+
map(std::log2)
|
| 530 |
+
);
|
| 531 |
+
}
|
| 532 |
+
Vectorized<float> nextafter(const Vectorized<float> &b) const {
|
| 533 |
+
USE_SLEEF(
|
| 534 |
+
{
|
| 535 |
+
return Vectorized<float>(Sleef_nextafterf4(values.val[0], b.values.val[0]),
|
| 536 |
+
Sleef_nextafterf4(values.val[1], b.values.val[1]));
|
| 537 |
+
},
|
| 538 |
+
{
|
| 539 |
+
__at_align__ float tmp[size()];
|
| 540 |
+
__at_align__ float tmp_b[size()];
|
| 541 |
+
store(tmp);
|
| 542 |
+
b.store(tmp_b);
|
| 543 |
+
for (const auto i : c10::irange(size())) {
|
| 544 |
+
tmp[i] = std::nextafter(tmp[i], tmp_b[i]);
|
| 545 |
+
}
|
| 546 |
+
return loadu(tmp);
|
| 547 |
+
}
|
| 548 |
+
)
|
| 549 |
+
}
|
| 550 |
+
Vectorized<float> frac() const;
|
| 551 |
+
Vectorized<float> sin() const {
|
| 552 |
+
return USE_SLEEF(
|
| 553 |
+
Vectorized<float>(Sleef_sinf4_u10(values.val[0]), Sleef_sinf4_u10(values.val[1])),
|
| 554 |
+
map(std::sin)
|
| 555 |
+
);
|
| 556 |
+
}
|
| 557 |
+
Vectorized<float> sinh() const {
|
| 558 |
+
return USE_SLEEF(
|
| 559 |
+
Vectorized<float>(Sleef_sinhf4_u10(values.val[0]), Sleef_sinhf4_u10(values.val[1])),
|
| 560 |
+
map(std::sinh)
|
| 561 |
+
);
|
| 562 |
+
}
|
| 563 |
+
Vectorized<float> cos() const {
|
| 564 |
+
return USE_SLEEF(
|
| 565 |
+
Vectorized<float>(Sleef_cosf4_u10(values.val[0]), Sleef_cosf4_u10(values.val[1])),
|
| 566 |
+
map(std::cos)
|
| 567 |
+
);
|
| 568 |
+
}
|
| 569 |
+
Vectorized<float> cosh() const {
|
| 570 |
+
return USE_SLEEF(
|
| 571 |
+
Vectorized<float>(Sleef_coshf4_u10(values.val[0]), Sleef_coshf4_u10(values.val[1])),
|
| 572 |
+
map(std::cosh)
|
| 573 |
+
);
|
| 574 |
+
}
|
| 575 |
+
Vectorized<float> ceil() const {
|
| 576 |
+
return map(at::native::ceil_impl);
|
| 577 |
+
}
|
| 578 |
+
Vectorized<float> floor() const {
|
| 579 |
+
return map(at::native::floor_impl);
|
| 580 |
+
}
|
| 581 |
+
Vectorized<float> neg() const {
|
| 582 |
+
return Vectorized<float>(
|
| 583 |
+
vnegq_f32(values.val[0]),
|
| 584 |
+
vnegq_f32(values.val[1]));
|
| 585 |
+
}
|
| 586 |
+
Vectorized<float> round() const {
|
| 587 |
+
// We do not use std::round because we would like to round midway numbers to the nearest even integer.
|
| 588 |
+
return map(at::native::round_impl);
|
| 589 |
+
}
|
| 590 |
+
Vectorized<float> tan() const {
|
| 591 |
+
return USE_SLEEF(
|
| 592 |
+
Vectorized<float>(Sleef_tanf4_u10(values.val[0]), Sleef_tanf4_u10(values.val[1])),
|
| 593 |
+
map(std::tan)
|
| 594 |
+
);
|
| 595 |
+
}
|
| 596 |
+
Vectorized<float> tanh() const {
|
| 597 |
+
return USE_SLEEF(
|
| 598 |
+
Vectorized<float>(Sleef_tanhf4_u10(values.val[0]), Sleef_tanhf4_u10(values.val[1])),
|
| 599 |
+
map(std::tanh)
|
| 600 |
+
);
|
| 601 |
+
}
|
| 602 |
+
Vectorized<float> trunc() const {
|
| 603 |
+
float32x4_t r0 = vrndq_f32(values.val[0]);
|
| 604 |
+
float32x4_t r1 = vrndq_f32(values.val[1]);
|
| 605 |
+
return Vectorized<float>(r0, r1);
|
| 606 |
+
}
|
| 607 |
+
Vectorized<float> lgamma() const {
|
| 608 |
+
return USE_SLEEF(
|
| 609 |
+
Vectorized<float>(Sleef_lgammaf4_u10(values.val[0]), Sleef_lgammaf4_u10(values.val[1])),
|
| 610 |
+
map(std::lgamma)
|
| 611 |
+
);
|
| 612 |
+
}
|
| 613 |
+
Vectorized<float> sqrt() const {
|
| 614 |
+
return Vectorized<float>(
|
| 615 |
+
vsqrtq_f32(values.val[0]),
|
| 616 |
+
vsqrtq_f32(values.val[1]));
|
| 617 |
+
}
|
| 618 |
+
Vectorized<float> reciprocal() const {
|
| 619 |
+
auto r0 = vdivq_f32(vdupq_n_f32(1.0f), values.val[0]);
|
| 620 |
+
auto r1 = vdivq_f32(vdupq_n_f32(1.0f), values.val[1]);
|
| 621 |
+
return Vectorized<float>(r0, r1);
|
| 622 |
+
}
|
| 623 |
+
Vectorized<float> rsqrt() const {
|
| 624 |
+
return this->sqrt().reciprocal();
|
| 625 |
+
}
|
| 626 |
+
Vectorized<float> pow(const Vectorized<float> &exp) const {
|
| 627 |
+
USE_SLEEF(
|
| 628 |
+
{
|
| 629 |
+
return Vectorized<float>(Sleef_powf4_u10(values.val[0], exp.values.val[0]),
|
| 630 |
+
Sleef_powf4_u10(values.val[1], exp.values.val[1]));
|
| 631 |
+
},
|
| 632 |
+
{
|
| 633 |
+
__at_align__ float tmp[size()];
|
| 634 |
+
__at_align__ float tmp_exp[size()];
|
| 635 |
+
store(tmp);
|
| 636 |
+
exp.store(tmp_exp);
|
| 637 |
+
for (const auto i : c10::irange(size())) {
|
| 638 |
+
tmp[i] = std::pow(tmp[i], tmp_exp[i]);
|
| 639 |
+
}
|
| 640 |
+
return loadu(tmp);
|
| 641 |
+
}
|
| 642 |
+
)
|
| 643 |
+
}
|
| 644 |
+
Vectorized<float> operator==(const Vectorized<float>& other) const {
|
| 645 |
+
float32x4_t r0 =
|
| 646 |
+
vreinterpretq_f32_u32(vceqq_f32(values.val[0], other.values.val[0]));
|
| 647 |
+
float32x4_t r1 =
|
| 648 |
+
vreinterpretq_f32_u32(vceqq_f32(values.val[1], other.values.val[1]));
|
| 649 |
+
return Vectorized<float>(r0, r1);
|
| 650 |
+
}
|
| 651 |
+
|
| 652 |
+
Vectorized<float> operator!=(const Vectorized<float>& other) const {
|
| 653 |
+
float32x4_t r0 = vreinterpretq_f32_u32(
|
| 654 |
+
vmvnq_u32(vceqq_f32(values.val[0], other.values.val[0])));
|
| 655 |
+
float32x4_t r1 = vreinterpretq_f32_u32(
|
| 656 |
+
vmvnq_u32(vceqq_f32(values.val[1], other.values.val[1])));
|
| 657 |
+
return Vectorized<float>(r0, r1);
|
| 658 |
+
}
|
| 659 |
+
|
| 660 |
+
Vectorized<float> operator<(const Vectorized<float>& other) const {
|
| 661 |
+
float32x4_t r0 =
|
| 662 |
+
vreinterpretq_f32_u32(vcltq_f32(values.val[0], other.values.val[0]));
|
| 663 |
+
float32x4_t r1 =
|
| 664 |
+
vreinterpretq_f32_u32(vcltq_f32(values.val[1], other.values.val[1]));
|
| 665 |
+
return Vectorized<float>(r0, r1);
|
| 666 |
+
}
|
| 667 |
+
|
| 668 |
+
Vectorized<float> operator<=(const Vectorized<float>& other) const {
|
| 669 |
+
float32x4_t r0 =
|
| 670 |
+
vreinterpretq_f32_u32(vcleq_f32(values.val[0], other.values.val[0]));
|
| 671 |
+
float32x4_t r1 =
|
| 672 |
+
vreinterpretq_f32_u32(vcleq_f32(values.val[1], other.values.val[1]));
|
| 673 |
+
return Vectorized<float>(r0, r1);
|
| 674 |
+
}
|
| 675 |
+
|
| 676 |
+
Vectorized<float> operator>(const Vectorized<float>& other) const {
|
| 677 |
+
float32x4_t r0 =
|
| 678 |
+
vreinterpretq_f32_u32(vcgtq_f32(values.val[0], other.values.val[0]));
|
| 679 |
+
float32x4_t r1 =
|
| 680 |
+
vreinterpretq_f32_u32(vcgtq_f32(values.val[1], other.values.val[1]));
|
| 681 |
+
return Vectorized<float>(r0, r1);
|
| 682 |
+
}
|
| 683 |
+
|
| 684 |
+
Vectorized<float> operator>=(const Vectorized<float>& other) const {
|
| 685 |
+
float32x4_t r0 =
|
| 686 |
+
vreinterpretq_f32_u32(vcgeq_f32(values.val[0], other.values.val[0]));
|
| 687 |
+
float32x4_t r1 =
|
| 688 |
+
vreinterpretq_f32_u32(vcgeq_f32(values.val[1], other.values.val[1]));
|
| 689 |
+
return Vectorized<float>(r0, r1);
|
| 690 |
+
}
|
| 691 |
+
|
| 692 |
+
Vectorized<float> eq(const Vectorized<float>& other) const;
|
| 693 |
+
Vectorized<float> ne(const Vectorized<float>& other) const;
|
| 694 |
+
Vectorized<float> gt(const Vectorized<float>& other) const;
|
| 695 |
+
Vectorized<float> ge(const Vectorized<float>& other) const;
|
| 696 |
+
Vectorized<float> lt(const Vectorized<float>& other) const;
|
| 697 |
+
Vectorized<float> le(const Vectorized<float>& other) const;
|
| 698 |
+
};
|
| 699 |
+
|
| 700 |
+
template <>
|
| 701 |
+
Vectorized<float> inline operator+(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 702 |
+
float32x4_t r0 = vaddq_f32(a.get_low(), b.get_low());
|
| 703 |
+
float32x4_t r1 = vaddq_f32(a.get_high(), b.get_high());
|
| 704 |
+
return Vectorized<float>(r0, r1);
|
| 705 |
+
}
|
| 706 |
+
|
| 707 |
+
template <>
|
| 708 |
+
Vectorized<float> inline operator-(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 709 |
+
float32x4_t r0 = vsubq_f32(a.get_low(), b.get_low());
|
| 710 |
+
float32x4_t r1 = vsubq_f32(a.get_high(), b.get_high());
|
| 711 |
+
return Vectorized<float>(r0, r1);
|
| 712 |
+
}
|
| 713 |
+
|
| 714 |
+
template <>
|
| 715 |
+
Vectorized<float> inline operator*(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 716 |
+
float32x4_t r0 = vmulq_f32(a.get_low(), b.get_low());
|
| 717 |
+
float32x4_t r1 = vmulq_f32(a.get_high(), b.get_high());
|
| 718 |
+
return Vectorized<float>(r0, r1);
|
| 719 |
+
}
|
| 720 |
+
|
| 721 |
+
template <>
|
| 722 |
+
Vectorized<float> inline operator/(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 723 |
+
float32x4_t r0 = vdivq_f32(a.get_low(), b.get_low());
|
| 724 |
+
float32x4_t r1 = vdivq_f32(a.get_high(), b.get_high());
|
| 725 |
+
return Vectorized<float>(r0, r1);
|
| 726 |
+
}
|
| 727 |
+
|
| 728 |
+
// frac. Implement this here so we can use subtraction
|
| 729 |
+
inline Vectorized<float> Vectorized<float>::frac() const {
|
| 730 |
+
return *this - this->trunc();
|
| 731 |
+
}
|
| 732 |
+
|
| 733 |
+
//Added sleef Implementation for Maximum
|
| 734 |
+
Vectorized<float> inline maximum(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 735 |
+
if(!a.has_inf_nan() && !b.has_inf_nan()){
|
| 736 |
+
return USE_SLEEF(
|
| 737 |
+
Vectorized<float>(Sleef_fmaxf4(a.get_low(), b.get_low()),Sleef_fmaxf4(a.get_high(), b.get_high())),
|
| 738 |
+
Vectorized<float>(vmaxq_f32(a.get_low(), b.get_low()),vmaxq_f32(a.get_high(), b.get_high())));
|
| 739 |
+
}
|
| 740 |
+
else{
|
| 741 |
+
return Vectorized<float>(vmaxq_f32(a.get_low(), b.get_low()),vmaxq_f32(a.get_high(), b.get_high()));
|
| 742 |
+
}
|
| 743 |
+
}
|
| 744 |
+
|
| 745 |
+
// Implements the IEEE 754 201X `minimum` operation, which propagates NaN if
|
| 746 |
+
// either input is a NaN.
|
| 747 |
+
template <>
|
| 748 |
+
Vectorized<float> inline minimum(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 749 |
+
float32x4_t r0 = vminq_f32(a.get_low(), b.get_low());
|
| 750 |
+
float32x4_t r1 = vminq_f32(a.get_high(), b.get_high());
|
| 751 |
+
return Vectorized<float>(r0, r1);
|
| 752 |
+
}
|
| 753 |
+
|
| 754 |
+
template <>
|
| 755 |
+
Vectorized<float> inline clamp(const Vectorized<float>& a, const Vectorized<float>& min, const Vectorized<float>& max) {
|
| 756 |
+
return minimum(max, maximum(min, a));
|
| 757 |
+
}
|
| 758 |
+
|
| 759 |
+
template <>
|
| 760 |
+
Vectorized<float> inline clamp_max(const Vectorized<float>& a, const Vectorized<float>& max) {
|
| 761 |
+
return minimum(max, a);
|
| 762 |
+
}
|
| 763 |
+
|
| 764 |
+
template <>
|
| 765 |
+
Vectorized<float> inline clamp_min(const Vectorized<float>& a, const Vectorized<float>& min) {
|
| 766 |
+
return maximum(min, a);
|
| 767 |
+
}
|
| 768 |
+
|
| 769 |
+
template <>
|
| 770 |
+
Vectorized<float> inline operator&(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 771 |
+
float32x4_t r0 = vreinterpretq_f32_u32(vandq_u32(
|
| 772 |
+
vreinterpretq_u32_f32(a.get_low()),
|
| 773 |
+
vreinterpretq_u32_f32(b.get_low())));
|
| 774 |
+
float32x4_t r1 = vreinterpretq_f32_u32(vandq_u32(
|
| 775 |
+
vreinterpretq_u32_f32(a.get_high()),
|
| 776 |
+
vreinterpretq_u32_f32(b.get_high())));
|
| 777 |
+
return Vectorized<float>(r0, r1);
|
| 778 |
+
}
|
| 779 |
+
|
| 780 |
+
template <>
|
| 781 |
+
Vectorized<float> inline operator|(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 782 |
+
float32x4_t r0 = vreinterpretq_f32_u32(vorrq_u32(
|
| 783 |
+
vreinterpretq_u32_f32(a.get_low()),
|
| 784 |
+
vreinterpretq_u32_f32(b.get_low())));
|
| 785 |
+
float32x4_t r1 = vreinterpretq_f32_u32(vorrq_u32(
|
| 786 |
+
vreinterpretq_u32_f32(a.get_high()),
|
| 787 |
+
vreinterpretq_u32_f32(b.get_high())));
|
| 788 |
+
return Vectorized<float>(r0, r1);
|
| 789 |
+
}
|
| 790 |
+
|
| 791 |
+
template <>
|
| 792 |
+
Vectorized<float> inline operator^(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 793 |
+
float32x4_t r0 = vreinterpretq_f32_u32(veorq_u32(
|
| 794 |
+
vreinterpretq_u32_f32(a.get_low()),
|
| 795 |
+
vreinterpretq_u32_f32(b.get_low())));
|
| 796 |
+
float32x4_t r1 = vreinterpretq_f32_u32(veorq_u32(
|
| 797 |
+
vreinterpretq_u32_f32(a.get_high()),
|
| 798 |
+
vreinterpretq_u32_f32(b.get_high())));
|
| 799 |
+
return Vectorized<float>(r0, r1);
|
| 800 |
+
}
|
| 801 |
+
|
| 802 |
+
inline Vectorized<float> Vectorized<float>::eq(const Vectorized<float>& other) const {
|
| 803 |
+
return (*this == other) & Vectorized<float>(1.0f);
|
| 804 |
+
}
|
| 805 |
+
|
| 806 |
+
inline Vectorized<float> Vectorized<float>::ne(const Vectorized<float>& other) const {
|
| 807 |
+
return (*this != other) & Vectorized<float>(1.0f);
|
| 808 |
+
}
|
| 809 |
+
|
| 810 |
+
inline Vectorized<float> Vectorized<float>::gt(const Vectorized<float>& other) const {
|
| 811 |
+
return (*this > other) & Vectorized<float>(1.0f);
|
| 812 |
+
}
|
| 813 |
+
|
| 814 |
+
inline Vectorized<float> Vectorized<float>::ge(const Vectorized<float>& other) const {
|
| 815 |
+
return (*this >= other) & Vectorized<float>(1.0f);
|
| 816 |
+
}
|
| 817 |
+
|
| 818 |
+
inline Vectorized<float> Vectorized<float>::lt(const Vectorized<float>& other) const {
|
| 819 |
+
return (*this < other) & Vectorized<float>(1.0f);
|
| 820 |
+
}
|
| 821 |
+
|
| 822 |
+
inline Vectorized<float> Vectorized<float>::le(const Vectorized<float>& other) const {
|
| 823 |
+
return (*this <= other) & Vectorized<float>(1.0f);
|
| 824 |
+
}
|
| 825 |
+
|
| 826 |
+
template <>
|
| 827 |
+
inline void convert(const float* src, int32_t* dst, int64_t n) {
|
| 828 |
+
int64_t i;
|
| 829 |
+
#ifndef __msvc_cl__
|
| 830 |
+
#pragma unroll
|
| 831 |
+
#endif
|
| 832 |
+
for (i = 0; i <= (n - Vectorized<float>::size()); i += Vectorized<float>::size()) {
|
| 833 |
+
vst1q_s32(dst + i, vcvtq_s32_f32(vld1q_f32(src + i)));
|
| 834 |
+
vst1q_s32(dst + i + 4, vcvtq_s32_f32(vld1q_f32(src + i + 4)));
|
| 835 |
+
}
|
| 836 |
+
#ifndef __msvc_cl__
|
| 837 |
+
#pragma unroll
|
| 838 |
+
#endif
|
| 839 |
+
for (; i < n; i++) {
|
| 840 |
+
dst[i] = static_cast<int32_t>(src[i]);
|
| 841 |
+
}
|
| 842 |
+
}
|
| 843 |
+
|
| 844 |
+
template <>
|
| 845 |
+
inline void convert(const int32_t* src, float* dst, int64_t n) {
|
| 846 |
+
int64_t i;
|
| 847 |
+
#ifndef __msvc_cl__
|
| 848 |
+
#pragma unroll
|
| 849 |
+
#endif
|
| 850 |
+
for (i = 0; i <= (n - Vectorized<float>::size()); i += Vectorized<float>::size()) {
|
| 851 |
+
vst1q_f32(dst + i, vcvtq_f32_s32(vld1q_s32(src + i)));
|
| 852 |
+
vst1q_f32(dst + i + 4, vcvtq_f32_s32(vld1q_s32(src + i + 4)));
|
| 853 |
+
}
|
| 854 |
+
#ifndef __msvc_cl__
|
| 855 |
+
#pragma unroll
|
| 856 |
+
#endif
|
| 857 |
+
for (; i < n; i++) {
|
| 858 |
+
dst[i] = static_cast<float>(src[i]);
|
| 859 |
+
}
|
| 860 |
+
}
|
| 861 |
+
|
| 862 |
+
template <>
|
| 863 |
+
Vectorized<float> inline fmadd(const Vectorized<float>& a, const Vectorized<float>& b, const Vectorized<float>& c) {
|
| 864 |
+
float32x4_t r0 = vfmaq_f32(c.get_low(), a.get_low(), b.get_low());
|
| 865 |
+
float32x4_t r1 = vfmaq_f32(c.get_high(), a.get_high(), b.get_high());
|
| 866 |
+
return Vectorized<float>(r0, r1);
|
| 867 |
+
}
|
| 868 |
+
|
| 869 |
+
template <>
|
| 870 |
+
Vectorized<float> inline fmsub(const Vectorized<float>& a, const Vectorized<float>& b, const Vectorized<float>& c) {
|
| 871 |
+
float32x4_t r0 = vfmsq_f32(c.get_low(), a.get_low(), b.get_low());
|
| 872 |
+
float32x4_t r1 = vfmsq_f32(c.get_high(), a.get_high(), b.get_high());
|
| 873 |
+
return Vectorized<float>(r0, r1);
|
| 874 |
+
}
|
| 875 |
+
|
| 876 |
+
inline Vectorized<float> Vectorized<float>::erf() const{
|
| 877 |
+
// constants
|
| 878 |
+
const Vectorized<float> neg_zero_vec(-0.f);
|
| 879 |
+
const Vectorized<float> one_vec(1.0f);
|
| 880 |
+
const Vectorized<float> p(0.3275911f);
|
| 881 |
+
const Vectorized<float> p1(0.254829592f);
|
| 882 |
+
const Vectorized<float> p2(-0.284496736f);
|
| 883 |
+
const Vectorized<float> p3(1.421413741f);
|
| 884 |
+
const Vectorized<float> p4(-1.453152027f);
|
| 885 |
+
const Vectorized<float> p5(1.061405429f);
|
| 886 |
+
// sign(x)
|
| 887 |
+
auto sign_mask = neg_zero_vec & *this;
|
| 888 |
+
auto abs_vec = this->abs();
|
| 889 |
+
// t = 1 / (p * abs(x) + 1)
|
| 890 |
+
auto tmp0 = fmadd(p, abs_vec, one_vec);
|
| 891 |
+
auto t = one_vec / tmp0;
|
| 892 |
+
// r = p5 * t ^ 4 + p4 * t ^ 3 + p3 * t ^ 2 + p2 * t + p1
|
| 893 |
+
auto tmp1 = fmadd(p5, t, p4);
|
| 894 |
+
auto tmp2 = fmadd(tmp1, t, p3);
|
| 895 |
+
auto tmp3 = fmadd(tmp2, t, p2);
|
| 896 |
+
auto r = fmadd(tmp3, t, p1);
|
| 897 |
+
// - exp(- x * x)
|
| 898 |
+
auto pow_2 = (*this) * (*this);
|
| 899 |
+
auto neg_pow_2 = pow_2 ^ neg_zero_vec;
|
| 900 |
+
auto tmp4 = neg_pow_2.map(std::exp); // This can be swapped for a faster implementation of exp.
|
| 901 |
+
auto tmp5 = tmp4 ^ neg_zero_vec;
|
| 902 |
+
// erf(x) = sign(x) * (1 - r * t * exp(- x * x))
|
| 903 |
+
auto tmp6 = t * tmp5;
|
| 904 |
+
auto tmp7 = fmadd(tmp6, r, one_vec);
|
| 905 |
+
return tmp7 ^ sign_mask;
|
| 906 |
+
}
|
| 907 |
+
#endif /* defined(aarch64) */
|
| 908 |
+
|
| 909 |
+
}} // namespace at::vec::CPU_CAPABILITY
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vec256_mask.h
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 4 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 5 |
+
#include <ATen/cpu/vec/vec_mask.h>
|
| 6 |
+
|
| 7 |
+
namespace at::vec {
|
| 8 |
+
inline namespace CPU_CAPABILITY {
|
| 9 |
+
|
| 10 |
+
#if defined(CPU_CAPABILITY_AVX2) && !defined(_MSC_VER)
|
| 11 |
+
|
| 12 |
+
template <typename T, int dst_n, typename mask_t, int mask_n>
|
| 13 |
+
struct VecMaskLoad<
|
| 14 |
+
T,
|
| 15 |
+
dst_n,
|
| 16 |
+
mask_t,
|
| 17 |
+
mask_n,
|
| 18 |
+
typename std::enable_if_t<
|
| 19 |
+
(mask_n == dst_n * 2 && dst_n >= 1) &&
|
| 20 |
+
(std::is_same_v<T, float> || std::is_same_v<T, int32_t>),
|
| 21 |
+
void>> {
|
| 22 |
+
static inline VectorizedN<T, dst_n> apply(
|
| 23 |
+
const T* ptr,
|
| 24 |
+
const VecMask<mask_t, mask_n>& vec_mask) {
|
| 25 |
+
VectorizedN<mask_t, 2> tmp_vec;
|
| 26 |
+
VectorizedN<T, dst_n> result;
|
| 27 |
+
for (int i = 0; i < dst_n; i++) {
|
| 28 |
+
tmp_vec[0] = vec_mask[2 * i];
|
| 29 |
+
tmp_vec[1] = vec_mask[2 * i + 1];
|
| 30 |
+
auto int64_mask = VecMask<mask_t, 2>(tmp_vec).template cast<int64_t, 2>();
|
| 31 |
+
auto int_mask = int64_mask.template cast<int, 1>()[0];
|
| 32 |
+
if constexpr (std::is_same_v<T, float>) {
|
| 33 |
+
result[i] = Vectorized<T>(
|
| 34 |
+
_mm256_maskload_ps(ptr + i * Vectorized<T>::size(), int_mask));
|
| 35 |
+
} else {
|
| 36 |
+
result[i] = Vectorized<T>(
|
| 37 |
+
_mm256_maskload_epi32(ptr + i * Vectorized<T>::size(), int_mask));
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
return result;
|
| 41 |
+
}
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
template <typename T, int dst_n, typename mask_t>
|
| 45 |
+
struct VecMaskLoad<
|
| 46 |
+
T,
|
| 47 |
+
dst_n,
|
| 48 |
+
mask_t,
|
| 49 |
+
dst_n,
|
| 50 |
+
typename std::enable_if_t<
|
| 51 |
+
std::is_same_v<T, float> || std::is_same_v<T, int32_t>,
|
| 52 |
+
void>> {
|
| 53 |
+
static inline VectorizedN<T, dst_n> apply(
|
| 54 |
+
const T* ptr,
|
| 55 |
+
const VecMask<mask_t, dst_n>& vec_mask) {
|
| 56 |
+
VectorizedN<T, dst_n> result;
|
| 57 |
+
#ifndef _MSC_VER
|
| 58 |
+
#pragma unroll
|
| 59 |
+
#endif
|
| 60 |
+
for (int i = 0; i < dst_n; i++) {
|
| 61 |
+
auto tmp_mask = VecMask<mask_t, 1>(vec_mask[i]);
|
| 62 |
+
auto int_mask = tmp_mask.template cast<int, 1>()[0];
|
| 63 |
+
if constexpr (std::is_same_v<T, float>) {
|
| 64 |
+
result[i] = Vectorized<T>(
|
| 65 |
+
_mm256_maskload_ps(ptr + i * Vectorized<T>::size(), int_mask));
|
| 66 |
+
} else {
|
| 67 |
+
result[i] = Vectorized<T>(
|
| 68 |
+
_mm256_maskload_epi32(ptr + i * Vectorized<T>::size(), int_mask));
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
return result;
|
| 72 |
+
}
|
| 73 |
+
};
|
| 74 |
+
|
| 75 |
+
template <typename T, typename mask_t>
|
| 76 |
+
struct VecMaskLoad<
|
| 77 |
+
T,
|
| 78 |
+
2,
|
| 79 |
+
mask_t,
|
| 80 |
+
1,
|
| 81 |
+
typename std::enable_if_t<
|
| 82 |
+
std::is_same_v<T, int64_t> || std::is_same_v<T, double>>> {
|
| 83 |
+
static inline VectorizedN<T, 2> apply(
|
| 84 |
+
const T* ptr,
|
| 85 |
+
const VecMask<mask_t, 1>& vec_mask) {
|
| 86 |
+
auto int64_mask = vec_mask.template cast<int64_t, 2>();
|
| 87 |
+
auto result = at::vec::VectorizedN<T, 2>();
|
| 88 |
+
if constexpr (std::is_same_v<T, double>) {
|
| 89 |
+
result[0] = _mm256_maskload_pd(ptr, int64_mask[0]);
|
| 90 |
+
result[1] = _mm256_maskload_pd(
|
| 91 |
+
ptr + at::vec::Vectorized<T>::size(), int64_mask[1]);
|
| 92 |
+
} else {
|
| 93 |
+
result[0] = _mm256_maskload_epi64(
|
| 94 |
+
reinterpret_cast<const long long*>(ptr), int64_mask[0]);
|
| 95 |
+
result[1] = _mm256_maskload_epi64(
|
| 96 |
+
reinterpret_cast<const long long*>(
|
| 97 |
+
ptr + at::vec::Vectorized<T>::size()),
|
| 98 |
+
int64_mask[1]);
|
| 99 |
+
}
|
| 100 |
+
return result;
|
| 101 |
+
}
|
| 102 |
+
};
|
| 103 |
+
|
| 104 |
+
// TODO: add specialization of VecMaskLoad for bfloat16/half and int8/uint8
|
| 105 |
+
|
| 106 |
+
template <int N>
|
| 107 |
+
struct VecMaskCast<float, N, int, N> {
|
| 108 |
+
static inline VecMask<float, N> apply(const VecMask<int, N>& vec_mask) {
|
| 109 |
+
VectorizedN<float, N> result;
|
| 110 |
+
#ifndef _MSC_VER
|
| 111 |
+
#pragma unroll
|
| 112 |
+
#endif
|
| 113 |
+
for (int i = 0; i < N; ++i) {
|
| 114 |
+
result[i] = _mm256_castsi256_ps(vec_mask[i]);
|
| 115 |
+
}
|
| 116 |
+
return result;
|
| 117 |
+
}
|
| 118 |
+
};
|
| 119 |
+
|
| 120 |
+
template <int N>
|
| 121 |
+
struct VecMaskCast<int, N, float, N> {
|
| 122 |
+
static inline VecMask<int, N> apply(const VecMask<float, N>& vec_mask) {
|
| 123 |
+
VectorizedN<int, N> result;
|
| 124 |
+
#ifndef _MSC_VER
|
| 125 |
+
#pragma unroll
|
| 126 |
+
#endif
|
| 127 |
+
for (int i = 0; i < N; ++i) {
|
| 128 |
+
result[i] = _mm256_castps_si256(vec_mask[i]);
|
| 129 |
+
}
|
| 130 |
+
return result;
|
| 131 |
+
}
|
| 132 |
+
};
|
| 133 |
+
|
| 134 |
+
template <int N>
|
| 135 |
+
struct VecMaskCast<int64_t, N, double, N> {
|
| 136 |
+
static inline VecMask<int64_t, N> apply(const VecMask<double, N>& vec_mask) {
|
| 137 |
+
VectorizedN<int64_t, N> result;
|
| 138 |
+
#ifndef _MSC_VER
|
| 139 |
+
#pragma unroll
|
| 140 |
+
#endif
|
| 141 |
+
for (int i = 0; i < N; ++i) {
|
| 142 |
+
result[i] = _mm256_castpd_si256(vec_mask[i]);
|
| 143 |
+
}
|
| 144 |
+
return result;
|
| 145 |
+
}
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
template <int N>
|
| 149 |
+
struct VecMaskCast<double, N, int64_t, N> {
|
| 150 |
+
static inline VecMask<double, N> apply(const VecMask<int64_t, N>& vec_mask) {
|
| 151 |
+
VectorizedN<double, N> result;
|
| 152 |
+
#ifndef _MSC_VER
|
| 153 |
+
#pragma unroll
|
| 154 |
+
#endif
|
| 155 |
+
for (int i = 0; i < N; ++i) {
|
| 156 |
+
result[i] = _mm256_castsi256_pd(vec_mask[i]);
|
| 157 |
+
}
|
| 158 |
+
return result;
|
| 159 |
+
}
|
| 160 |
+
};
|
| 161 |
+
|
| 162 |
+
template <int dst_n, typename mask_t, int mask_n>
|
| 163 |
+
struct VecMaskCast<
|
| 164 |
+
int64_t,
|
| 165 |
+
dst_n,
|
| 166 |
+
mask_t,
|
| 167 |
+
mask_n,
|
| 168 |
+
typename std::enable_if_t<
|
| 169 |
+
(dst_n == 2 * mask_n) &&
|
| 170 |
+
(std::is_same_v<mask_t, float> || std::is_same_v<mask_t, int>),
|
| 171 |
+
void>> {
|
| 172 |
+
static inline VecMask<int64_t, dst_n> apply(
|
| 173 |
+
const VecMask<mask_t, mask_n>& vec_mask) {
|
| 174 |
+
VectorizedN<int64_t, dst_n> result;
|
| 175 |
+
auto int_mask = vec_mask.template cast<int, mask_n>();
|
| 176 |
+
#ifndef _MSC_VER
|
| 177 |
+
#pragma unroll
|
| 178 |
+
#endif
|
| 179 |
+
for (int i = 0; i < mask_n; ++i) {
|
| 180 |
+
auto int64_vec =
|
| 181 |
+
convert<int64_t, 2, int, 1>(VectorizedN<int, 1>(int_mask[i]));
|
| 182 |
+
result[2 * i] = int64_vec[0];
|
| 183 |
+
result[2 * i + 1] = int64_vec[1];
|
| 184 |
+
}
|
| 185 |
+
return VecMask<int64_t, dst_n>(result);
|
| 186 |
+
}
|
| 187 |
+
};
|
| 188 |
+
|
| 189 |
+
template <typename dst_t, int dst_n, int mask_n>
|
| 190 |
+
struct VecMaskCast<
|
| 191 |
+
dst_t,
|
| 192 |
+
dst_n,
|
| 193 |
+
int64_t,
|
| 194 |
+
mask_n,
|
| 195 |
+
typename std::enable_if_t<
|
| 196 |
+
(mask_n == 2 * dst_n) &&
|
| 197 |
+
(std::is_same_v<dst_t, float> || std::is_same_v<dst_t, int>),
|
| 198 |
+
void>> {
|
| 199 |
+
static inline VecMask<dst_t, dst_n> apply(
|
| 200 |
+
const VecMask<int64_t, mask_n>& vec_mask) {
|
| 201 |
+
VectorizedN<int, dst_n> result;
|
| 202 |
+
VectorizedN<int64_t, 2> int64_vec;
|
| 203 |
+
for (int i = 0; i < dst_n; ++i) {
|
| 204 |
+
int64_vec[0] = vec_mask[2 * i];
|
| 205 |
+
int64_vec[1] = vec_mask[2 * i + 1];
|
| 206 |
+
result[i] = convert<int, 1, int64_t, 2>(int64_vec);
|
| 207 |
+
}
|
| 208 |
+
return VecMask<int, dst_n>(result).template cast<dst_t, dst_n>();
|
| 209 |
+
}
|
| 210 |
+
};
|
| 211 |
+
|
| 212 |
+
template <>
|
| 213 |
+
struct VecMaskCast<double, 2, float, 1> {
|
| 214 |
+
static inline VecMask<double, 2> apply(const VecMask<float, 1>& vec_mask) {
|
| 215 |
+
auto int64_mask = VecMaskCast<int64_t, 2, float, 1>::apply(vec_mask);
|
| 216 |
+
return VecMaskCast<double, 2, int64_t, 2>::apply(int64_mask);
|
| 217 |
+
}
|
| 218 |
+
};
|
| 219 |
+
template <>
|
| 220 |
+
struct VecMaskCast<float, 1, double, 2> {
|
| 221 |
+
static inline VecMask<float, 1> apply(const VecMask<double, 2>& vec_mask) {
|
| 222 |
+
auto int64_mask = VecMaskCast<int64_t, 2, double, 2>::apply(vec_mask);
|
| 223 |
+
return VecMaskCast<float, 1, int64_t, 2>::apply(int64_mask);
|
| 224 |
+
}
|
| 225 |
+
};
|
| 226 |
+
|
| 227 |
+
template <>
|
| 228 |
+
inline bool VecMask<int, 1>::all_zero() const {
|
| 229 |
+
return _mm256_testz_si256(mask_[0], mask_[0]);
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
template <>
|
| 233 |
+
inline bool VecMask<int, 1>::is_masked(int i) const {
|
| 234 |
+
return _mm256_movemask_ps(_mm256_castsi256_ps(mask_[0])) & (1 << i);
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
template <>
|
| 238 |
+
inline bool VecMask<int, 1>::all_masked() const {
|
| 239 |
+
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(mask_[0]));
|
| 240 |
+
return mask == 0xff;
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
template <int N>
|
| 244 |
+
struct VecMaskCheck<int64_t, N> {
|
| 245 |
+
static inline bool all_zero(const VectorizedN<int64_t, N>& vec_mask) {
|
| 246 |
+
bool all_zero = true;
|
| 247 |
+
for (int i = 0; i < N; ++i) {
|
| 248 |
+
all_zero = all_zero && (_mm256_testz_si256(vec_mask[i], vec_mask[i]) > 0);
|
| 249 |
+
if (!all_zero) {
|
| 250 |
+
return all_zero;
|
| 251 |
+
}
|
| 252 |
+
}
|
| 253 |
+
return all_zero;
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
static inline bool is_masked(const VectorizedN<int64_t, N>& vec_mask, int i) {
|
| 257 |
+
for (int j = 0; j < N; ++j) {
|
| 258 |
+
if (i < (j + 1) * 4) {
|
| 259 |
+
return _mm256_movemask_pd(_mm256_castsi256_pd(vec_mask[j])) &
|
| 260 |
+
(1 << (i - j * 4));
|
| 261 |
+
}
|
| 262 |
+
}
|
| 263 |
+
return false;
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
static inline bool all_masked(const VectorizedN<int64_t, N>& vec_mask) {
|
| 267 |
+
bool all_masked = true;
|
| 268 |
+
for (int i = 0; i < N; ++i) {
|
| 269 |
+
all_masked = all_masked &&
|
| 270 |
+
(_mm256_movemask_pd(_mm256_castsi256_pd(vec_mask[i])) == 0x0f);
|
| 271 |
+
if (!all_masked) {
|
| 272 |
+
return all_masked;
|
| 273 |
+
}
|
| 274 |
+
}
|
| 275 |
+
return all_masked;
|
| 276 |
+
}
|
| 277 |
+
};
|
| 278 |
+
|
| 279 |
+
#define VEC_MASK_METHOD_WITH_CAST_TO_INT( \
|
| 280 |
+
T, N, return_type, method, args_def, args) \
|
| 281 |
+
template <> \
|
| 282 |
+
inline return_type VecMask<T, N>::method args_def const { \
|
| 283 |
+
return cast<int, 1>().method args; \
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
VEC_MASK_METHOD_WITH_CAST_TO_INT(float, 1, bool, all_zero, (), ())
|
| 287 |
+
VEC_MASK_METHOD_WITH_CAST_TO_INT(int64_t, 2, bool, all_zero, (), ())
|
| 288 |
+
VEC_MASK_METHOD_WITH_CAST_TO_INT(float, 1, bool, is_masked, (int i), (i))
|
| 289 |
+
VEC_MASK_METHOD_WITH_CAST_TO_INT(int64_t, 2, bool, is_masked, (int i), (i))
|
| 290 |
+
VEC_MASK_METHOD_WITH_CAST_TO_INT(float, 1, bool, all_masked, (), ())
|
| 291 |
+
VEC_MASK_METHOD_WITH_CAST_TO_INT(int64_t, 2, bool, all_masked, (), ())
|
| 292 |
+
|
| 293 |
+
#undef VEC_MASK_DEFINE_METHOD_WITH_CAST_TO_INT
|
| 294 |
+
|
| 295 |
+
#endif
|
| 296 |
+
|
| 297 |
+
} // namespace CPU_CAPABILITY
|
| 298 |
+
} // namespace at::vec
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vec256_qint.h
ADDED
|
@@ -0,0 +1,1369 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// DO NOT DEFINE STATIC DATA IN THIS HEADER!
|
| 4 |
+
// See Note [Do not compile initializers with AVX]
|
| 5 |
+
|
| 6 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 7 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 8 |
+
#include <ATen/native/quantized/AffineQuantizerBase.h>
|
| 9 |
+
|
| 10 |
+
#include <c10/util/irange.h>
|
| 11 |
+
#include <c10/util/qint32.h>
|
| 12 |
+
#include <c10/util/qint8.h>
|
| 13 |
+
#include <c10/util/quint8.h>
|
| 14 |
+
|
| 15 |
+
#include <array>
|
| 16 |
+
#include <cmath>
|
| 17 |
+
|
| 18 |
+
// This file defines Vectorized<> for the quantized types.
|
| 19 |
+
//
|
| 20 |
+
//
|
| 21 |
+
// Currently, we simply use these classes as efficient converters between
|
| 22 |
+
// the quantized types and Vectorized<float>, usually in bandwidth-bound cases
|
| 23 |
+
// where doing the arithmetic in full-precision is acceptable (e.g.
|
| 24 |
+
// elementwise operators).
|
| 25 |
+
//
|
| 26 |
+
//
|
| 27 |
+
// Conversions are as follows:
|
| 28 |
+
// Vectorized<qint8> -> 4x Vectorized<float>
|
| 29 |
+
// Vectorized<quint8> -> 4x Vectorized<float>
|
| 30 |
+
// Vectorized<qint32> -> 1x Vectorized<float>
|
| 31 |
+
//
|
| 32 |
+
// The size of the returned float vector is specified by the special
|
| 33 |
+
// constexpr function float_num_vecs. The type of the value returned
|
| 34 |
+
// from dequantize (and expected as an argument to quantize) is
|
| 35 |
+
// specified by float_vec_return_type.
|
| 36 |
+
//
|
| 37 |
+
// When writing kernels with these vectors, it is expected that floating-
|
| 38 |
+
// point operations will be carried out in a loop over Vectorized<T>::float_num_vecs
|
| 39 |
+
// iterations.
|
| 40 |
+
|
| 41 |
+
namespace at::vec {
|
| 42 |
+
inline namespace CPU_CAPABILITY {
|
| 43 |
+
|
| 44 |
+
#if defined(CPU_CAPABILITY_AVX2)
|
| 45 |
+
|
| 46 |
+
#ifdef _MSC_VER
|
| 47 |
+
__declspec(align(64)) struct Vectorizedqi {
|
| 48 |
+
protected:
|
| 49 |
+
__m256i vals;
|
| 50 |
+
#else
|
| 51 |
+
struct Vectorizedqi {
|
| 52 |
+
protected:
|
| 53 |
+
__m256i vals __attribute__((aligned(64)));
|
| 54 |
+
#endif
|
| 55 |
+
|
| 56 |
+
public:
|
| 57 |
+
Vectorizedqi() {}
|
| 58 |
+
Vectorizedqi(__m256i v) : vals(v) {}
|
| 59 |
+
operator __m256i() const {
|
| 60 |
+
return vals;
|
| 61 |
+
}
|
| 62 |
+
};
|
| 63 |
+
|
| 64 |
+
template <typename T>
|
| 65 |
+
__m256i pack_saturate_and_clamp(
|
| 66 |
+
__m256i first,
|
| 67 |
+
__m256i second,
|
| 68 |
+
T min_val,
|
| 69 |
+
T max_val);
|
| 70 |
+
|
| 71 |
+
template <>
|
| 72 |
+
inline __m256i pack_saturate_and_clamp<int32_t>(
|
| 73 |
+
__m256i /*first*/,
|
| 74 |
+
__m256i /*second*/,
|
| 75 |
+
int32_t /*min_val*/,
|
| 76 |
+
int32_t /*max_val*/) {
|
| 77 |
+
// This function is for linkage only, will not be used
|
| 78 |
+
AT_ERROR("pack_saturate_and_clamp<int32_t> is not supported");
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
template <>
|
| 82 |
+
inline __m256i pack_saturate_and_clamp<int8_t>(
|
| 83 |
+
__m256i first,
|
| 84 |
+
__m256i second,
|
| 85 |
+
int8_t min_val,
|
| 86 |
+
int8_t max_val) {
|
| 87 |
+
__m256i packed_and_sat = _mm256_packs_epi16(first, second);
|
| 88 |
+
return _mm256_max_epi8(
|
| 89 |
+
_mm256_set1_epi8(min_val),
|
| 90 |
+
_mm256_min_epi8(packed_and_sat, _mm256_set1_epi8(max_val)));
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
template <>
|
| 94 |
+
inline __m256i pack_saturate_and_clamp<uint8_t>(
|
| 95 |
+
__m256i first,
|
| 96 |
+
__m256i second,
|
| 97 |
+
uint8_t min_val,
|
| 98 |
+
uint8_t max_val) {
|
| 99 |
+
__m256i packed_and_sat = _mm256_packus_epi16(first, second);
|
| 100 |
+
return _mm256_max_epu8(
|
| 101 |
+
_mm256_set1_epi8(min_val),
|
| 102 |
+
_mm256_min_epu8(packed_and_sat, _mm256_set1_epi8(max_val)));
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
template <typename T>
|
| 106 |
+
typename std::enable_if_t<std::is_same_v<T, uint8_t> || std::is_same_v<T, int8_t>, at::vec::Vectorized<float>>
|
| 107 |
+
inline convert_int8_to_float(at::vec::Vectorized<T> src) {
|
| 108 |
+
// Note: this function only convert inputs number of elements equal to at::vec::Vectorized<float>.size()
|
| 109 |
+
// Only handle first 8*8 bits
|
| 110 |
+
__m128i input_128 = _mm256_castsi256_si128(src);
|
| 111 |
+
// Convert from 8*uint8/int8 to 8*int32
|
| 112 |
+
__m256i input_256_int32;
|
| 113 |
+
if constexpr (std::is_same_v<T, uint8_t>)
|
| 114 |
+
input_256_int32 = _mm256_cvtepu8_epi32(input_128);
|
| 115 |
+
else
|
| 116 |
+
input_256_int32 = _mm256_cvtepi8_epi32(input_128);
|
| 117 |
+
// Convert from 8*int32 to 8*float
|
| 118 |
+
return _mm256_cvtepi32_ps(input_256_int32);
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
template <typename T>
|
| 122 |
+
typename std::enable_if_t<std::is_same_v<T, uint8_t> || std::is_same_v<T, int8_t>, at::vec::Vectorized<T>>
|
| 123 |
+
inline convert_float_to_int8(at::vec::Vectorized<float> src) {
|
| 124 |
+
// Convert from float32 to int32 with truncation
|
| 125 |
+
__m256i x_values_int32 = _mm256_cvttps_epi32(src);
|
| 126 |
+
|
| 127 |
+
// Convert from int32 to int16 using signed saturation
|
| 128 |
+
__m256i xy_packed_v = _mm256_packs_epi32(x_values_int32, x_values_int32);
|
| 129 |
+
|
| 130 |
+
constexpr auto min_val = std::numeric_limits<T>::min();
|
| 131 |
+
constexpr auto max_val = std::numeric_limits<T>::max();
|
| 132 |
+
|
| 133 |
+
// Convert from int16 to uint8/int8 using unsigned saturation
|
| 134 |
+
__m256i xyzw_clamped_v = pack_saturate_and_clamp<T>(
|
| 135 |
+
xy_packed_v, xy_packed_v, min_val, max_val);
|
| 136 |
+
__m256i permute_mask_v =
|
| 137 |
+
_mm256_set_epi32(0x07, 0x03, 0x06, 0x02, 0x05, 0x01, 0x04, 0x00);
|
| 138 |
+
return _mm256_permutevar8x32_epi32(xyzw_clamped_v, permute_mask_v);
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
template <typename T>
|
| 142 |
+
__FORCE_INLINE void QuantizeAvx2(
|
| 143 |
+
const float* src,
|
| 144 |
+
T* dst,
|
| 145 |
+
int len,
|
| 146 |
+
float inverse_scale,
|
| 147 |
+
int64_t zero_point) {
|
| 148 |
+
constexpr int VLEN = 8;
|
| 149 |
+
constexpr auto min_val = std::numeric_limits<T>::min();
|
| 150 |
+
constexpr auto max_val = std::numeric_limits<T>::max();
|
| 151 |
+
const __m256i min_v = _mm256_set1_epi32(min_val);
|
| 152 |
+
const __m256i max_v = _mm256_set1_epi32(max_val);
|
| 153 |
+
// This is the largest int32 value < int32_max exactly representable in float
|
| 154 |
+
constexpr int32_t int32_float_max_val =
|
| 155 |
+
std::numeric_limits<int32_t>::max() - 127;
|
| 156 |
+
int i = 0;
|
| 157 |
+
__m256 inverse_scale_v = _mm256_set1_ps(inverse_scale);
|
| 158 |
+
// clang-format off
|
| 159 |
+
static const __m256i shuffle_mask_v = _mm256_set_epi8(
|
| 160 |
+
0xff, 0xff, 0xff, 0xff,
|
| 161 |
+
0xff, 0xff, 0xff, 0xff,
|
| 162 |
+
0xff, 0xff, 0xff, 0xff,
|
| 163 |
+
0x0c, 0x08, 0x04, 0x00,
|
| 164 |
+
0xff, 0xff, 0xff, 0xff,
|
| 165 |
+
0xff, 0xff, 0xff, 0xff,
|
| 166 |
+
0xff, 0xff, 0xff, 0xff,
|
| 167 |
+
0x0c, 0x08, 0x04, 0x00);
|
| 168 |
+
// clang-format on
|
| 169 |
+
__m256i permute_mask_v =
|
| 170 |
+
_mm256_set_epi32(0x07, 0x03, 0x06, 0x02, 0x05, 0x01, 0x04, 0x00);
|
| 171 |
+
__m256i permute_mask_l8_v =
|
| 172 |
+
_mm256_set_epi32(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00);
|
| 173 |
+
int len_aligned = len / (VLEN * 4) * (VLEN * 4);
|
| 174 |
+
for (; i < len_aligned; i += 4 * VLEN) {
|
| 175 |
+
// x
|
| 176 |
+
__m256 x_vals = _mm256_load_ps(src + i);
|
| 177 |
+
__m256 x_transformed_v = _mm256_mul_ps(x_vals, inverse_scale_v);
|
| 178 |
+
// If the floating point value is greater than int32_max,
|
| 179 |
+
// _mm256_cvtps_epi32 converts them to -ve. Clip at int32_float_max_val to
|
| 180 |
+
// Clip at int32_float_max_val to avoid this.
|
| 181 |
+
x_transformed_v =
|
| 182 |
+
_mm256_min_ps(x_transformed_v, _mm256_set1_ps(int32_float_max_val));
|
| 183 |
+
// y
|
| 184 |
+
__m256 y_vals = _mm256_load_ps(src + i + VLEN);
|
| 185 |
+
__m256 y_transformed_v = _mm256_mul_ps(y_vals, inverse_scale_v);
|
| 186 |
+
y_transformed_v =
|
| 187 |
+
_mm256_min_ps(y_transformed_v, _mm256_set1_ps(int32_float_max_val));
|
| 188 |
+
// z
|
| 189 |
+
__m256 z_vals = _mm256_load_ps(src + i + 2 * VLEN);
|
| 190 |
+
__m256 z_transformed_v = _mm256_mul_ps(z_vals, inverse_scale_v);
|
| 191 |
+
z_transformed_v =
|
| 192 |
+
_mm256_min_ps(z_transformed_v, _mm256_set1_ps(int32_float_max_val));
|
| 193 |
+
// w
|
| 194 |
+
__m256 w_vals = _mm256_load_ps(src + i + 3 * VLEN);
|
| 195 |
+
__m256 w_transformed_v = _mm256_mul_ps(w_vals, inverse_scale_v);
|
| 196 |
+
w_transformed_v =
|
| 197 |
+
_mm256_min_ps(w_transformed_v, _mm256_set1_ps(int32_float_max_val));
|
| 198 |
+
|
| 199 |
+
__m256i x_rounded_v = _mm256_cvtps_epi32(x_transformed_v);
|
| 200 |
+
__m256i y_rounded_v = _mm256_cvtps_epi32(y_transformed_v);
|
| 201 |
+
__m256i z_rounded_v = _mm256_cvtps_epi32(z_transformed_v);
|
| 202 |
+
__m256i w_rounded_v = _mm256_cvtps_epi32(w_transformed_v);
|
| 203 |
+
|
| 204 |
+
// add zero point
|
| 205 |
+
x_rounded_v = _mm256_add_epi32(x_rounded_v, _mm256_set1_epi32(zero_point));
|
| 206 |
+
y_rounded_v = _mm256_add_epi32(y_rounded_v, _mm256_set1_epi32(zero_point));
|
| 207 |
+
z_rounded_v = _mm256_add_epi32(z_rounded_v, _mm256_set1_epi32(zero_point));
|
| 208 |
+
w_rounded_v = _mm256_add_epi32(w_rounded_v, _mm256_set1_epi32(zero_point));
|
| 209 |
+
|
| 210 |
+
__m256i xy_packed_v = _mm256_packs_epi32(x_rounded_v, y_rounded_v);
|
| 211 |
+
__m256i zw_packed_v = _mm256_packs_epi32(z_rounded_v, w_rounded_v);
|
| 212 |
+
__m256i xyzw_clamped_v =
|
| 213 |
+
pack_saturate_and_clamp<T>(xy_packed_v, zw_packed_v, min_val, max_val);
|
| 214 |
+
|
| 215 |
+
xyzw_clamped_v =
|
| 216 |
+
_mm256_permutevar8x32_epi32(xyzw_clamped_v, permute_mask_v);
|
| 217 |
+
_mm256_storeu_si256(reinterpret_cast<__m256i*>(dst + i), xyzw_clamped_v);
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
// Additional 8-lane AVX2 version to take advantage when len is smaller
|
| 221 |
+
// based on fbgemm::QuantizeAvx2 (https://github.com/pytorch/FBGEMM)
|
| 222 |
+
for (; i < len / VLEN * VLEN; i += VLEN) {
|
| 223 |
+
__m256 x_vals = _mm256_load_ps(src + i);
|
| 224 |
+
__m256 x_transformed_v = _mm256_mul_ps(x_vals, inverse_scale_v);
|
| 225 |
+
x_transformed_v =
|
| 226 |
+
_mm256_min_ps(x_transformed_v, _mm256_set1_ps(int32_float_max_val));
|
| 227 |
+
__m256i x_rounded_v = _mm256_cvtps_epi32(x_transformed_v);
|
| 228 |
+
x_rounded_v = _mm256_add_epi32(x_rounded_v, _mm256_set1_epi32(zero_point));
|
| 229 |
+
__m256i x_clipped_v =
|
| 230 |
+
_mm256_max_epi32(min_v, _mm256_min_epi32(max_v, x_rounded_v));
|
| 231 |
+
|
| 232 |
+
x_clipped_v = _mm256_shuffle_epi8(x_clipped_v, shuffle_mask_v);
|
| 233 |
+
x_clipped_v = _mm256_permutevar8x32_epi32(x_clipped_v, permute_mask_l8_v);
|
| 234 |
+
_mm_storel_epi64(
|
| 235 |
+
reinterpret_cast<__m128i*>(dst + i),
|
| 236 |
+
_mm256_castsi256_si128(x_clipped_v));
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
for (; i < len; ++i) {
|
| 240 |
+
float transformed = src[i] * inverse_scale;
|
| 241 |
+
|
| 242 |
+
// Not exactly the same behavior as the vectorized code.
|
| 243 |
+
// The vectorized code above always rounds to even in halfway cases
|
| 244 |
+
// (https://software.intel.com/en-us/node/523819), but std::nearbyint
|
| 245 |
+
// does the same only when the current rounding mode is FE_TONEAREST.
|
| 246 |
+
// However, in practice, this should not be a problem because most cases
|
| 247 |
+
// use the default rounding mode FE_TONEAREST.
|
| 248 |
+
// Note that we cannot implement the same behavior as the vectorized code
|
| 249 |
+
// using std::round because it does rounding away from zero in halfway
|
| 250 |
+
// cases.
|
| 251 |
+
transformed = zero_point + std::nearbyint(transformed);
|
| 252 |
+
float clipped =
|
| 253 |
+
std::min(std::max(transformed, float(min_val)), float(max_val));
|
| 254 |
+
dst[i] = clipped;
|
| 255 |
+
}
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
template<>
|
| 259 |
+
struct Vectorized<c10::qint32> : public Vectorizedqi {
|
| 260 |
+
using size_type = int;
|
| 261 |
+
static constexpr size_type size() {
|
| 262 |
+
return 8;
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
static constexpr int float_num_vecs() {
|
| 266 |
+
return 1;
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
static constexpr int int_num_vecs() {
|
| 270 |
+
return 1;
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
using float_vec_return_type = std::array<Vectorized<float>, 1>;
|
| 274 |
+
using int_vec_return_type = std::array<Vectorized<c10::qint32>, 1>;
|
| 275 |
+
using value_type = c10::qint32::underlying;
|
| 276 |
+
|
| 277 |
+
public:
|
| 278 |
+
using Vectorizedqi::Vectorizedqi;
|
| 279 |
+
Vectorized() {}
|
| 280 |
+
|
| 281 |
+
Vectorized(__m256i vals_) { vals = vals_;}
|
| 282 |
+
|
| 283 |
+
// Broadcast constructor
|
| 284 |
+
Vectorized(const c10::qint32& val) {
|
| 285 |
+
value_type uw = val.val_;
|
| 286 |
+
vals = _mm256_set1_epi32(uw);
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
void store(void* ptr, int count = size()) const {
|
| 290 |
+
if (count != size()) {
|
| 291 |
+
memcpy(ptr, &vals, count * sizeof(value_type));
|
| 292 |
+
} else {
|
| 293 |
+
_mm256_storeu_si256((__m256i*)ptr, vals);
|
| 294 |
+
}
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
static Vectorized<c10::qint32> loadu(const void* ptr) {
|
| 298 |
+
return Vectorized<c10::qint32>(ptr);
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
static Vectorized<c10::qint32> loadu(const void* ptr, int64_t count) {
|
| 302 |
+
__at_align__ value_type tmp_values[size()];
|
| 303 |
+
// Ensure uninitialized memory does not change the output value See https://github.com/pytorch/pytorch/issues/32502
|
| 304 |
+
// for more details. We do not initialize arrays to zero using "={0}" because gcc would compile it to two
|
| 305 |
+
// instructions while a loop would be compiled to one instruction.
|
| 306 |
+
for (const auto i : c10::irange(size())) {
|
| 307 |
+
tmp_values[i] = 0;
|
| 308 |
+
}
|
| 309 |
+
std::memcpy(
|
| 310 |
+
tmp_values, reinterpret_cast<const value_type*>(ptr), count * sizeof(value_type));
|
| 311 |
+
return _mm256_loadu_si256((const __m256i*)tmp_values);
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
float_vec_return_type dequantize(
|
| 315 |
+
Vectorized<float> scale,
|
| 316 |
+
Vectorized<float> /*zero_point*/,
|
| 317 |
+
Vectorized<float> scale_zp_premul) const {
|
| 318 |
+
__m256 float_vals = _mm256_cvtepi32_ps(vals);
|
| 319 |
+
return {vec::fmadd(scale, Vectorized<float>(float_vals), scale_zp_premul)};
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
float_vec_return_type dequantize(
|
| 323 |
+
Vectorized<float> scale,
|
| 324 |
+
Vectorized<float> zero_point) const {
|
| 325 |
+
__m256 float_vals = _mm256_cvtepi32_ps(vals);
|
| 326 |
+
return {(Vectorized<float>(float_vals) - zero_point) * scale};
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
static Vectorized<c10::qint32> quantize(
|
| 330 |
+
const float_vec_return_type& rhs,
|
| 331 |
+
float scale,
|
| 332 |
+
int32_t zero_point,
|
| 333 |
+
float /*inverse_scale*/) {
|
| 334 |
+
Vectorized<c10::qint32> retval;
|
| 335 |
+
auto rhs_data = (__m256)rhs[0];
|
| 336 |
+
at::native::quantize_vec<c10::qint32, /*precision=*/32>(
|
| 337 |
+
scale, zero_point, (float*)&rhs_data, (c10::qint32*)&retval.vals, 8);
|
| 338 |
+
return retval;
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
Vectorized<c10::qint32> maximum(Vectorized<c10::qint32> b) const {
|
| 342 |
+
return _mm256_max_epi32(vals, b.vals);
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
Vectorized<c10::qint32> minimum(Vectorized<c10::qint32> b) const {
|
| 346 |
+
return _mm256_min_epi32(vals, b.vals);
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
Vectorized<c10::qint32> relu(Vectorized<c10::qint32> zero_point) const {
|
| 350 |
+
return maximum(zero_point);
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
Vectorized<c10::qint32> relu6(
|
| 354 |
+
Vectorized<c10::qint32> zero_point,
|
| 355 |
+
Vectorized<c10::qint32> q_six) {
|
| 356 |
+
return _mm256_min_epi32(
|
| 357 |
+
_mm256_max_epi32(vals, zero_point.vals), q_six.vals);
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
int_vec_return_type widening_subtract(Vectorized<c10::qint32> b) const {
|
| 361 |
+
return {_mm256_sub_epi32(vals, b)};
|
| 362 |
+
}
|
| 363 |
+
|
| 364 |
+
static Vectorized<c10::qint32> requantize_from_int(
|
| 365 |
+
const int_vec_return_type& inp,
|
| 366 |
+
float multiplier,
|
| 367 |
+
int32_t zero_point) {
|
| 368 |
+
__m256 multiplier_v = _mm256_set1_ps(multiplier);
|
| 369 |
+
__m256i zero_point_v = _mm256_set1_epi32(zero_point);
|
| 370 |
+
|
| 371 |
+
__m256 scaled = _mm256_mul_ps(_mm256_cvtepi32_ps(inp[0]), multiplier_v);
|
| 372 |
+
__m256i rounded = _mm256_cvtps_epi32(scaled);
|
| 373 |
+
return _mm256_add_epi32(rounded, zero_point_v);
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
private:
|
| 377 |
+
// Load from memory constructor
|
| 378 |
+
Vectorized(const void* ptr) {
|
| 379 |
+
vals = _mm256_loadu_si256((const __m256i*)ptr);
|
| 380 |
+
}
|
| 381 |
+
};
|
| 382 |
+
|
| 383 |
+
template <>
|
| 384 |
+
Vectorized<c10::qint32> inline maximum(const Vectorized<c10::qint32>& a, const Vectorized<c10::qint32>& b) {
|
| 385 |
+
return a.maximum(b);
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
template <>
|
| 389 |
+
Vectorized<c10::qint32> inline operator*(
|
| 390 |
+
const Vectorized<c10::qint32>& a,
|
| 391 |
+
const Vectorized<c10::qint32>& b) {
|
| 392 |
+
return _mm256_mullo_epi32(a, b);
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
template <>
|
| 396 |
+
Vectorized<c10::qint32> inline operator+(
|
| 397 |
+
const Vectorized<c10::qint32>& a,
|
| 398 |
+
const Vectorized<c10::qint32>& b) {
|
| 399 |
+
return _mm256_add_epi32(a, b);
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
/*
|
| 403 |
+
* Convert values from int32 back to int8/uint8
|
| 404 |
+
*/
|
| 405 |
+
template <typename T>
|
| 406 |
+
__m256i RequantizeAvx2(
|
| 407 |
+
const std::array<Vectorized<c10::qint32>, 4>& inp,
|
| 408 |
+
__m256 multiplier,
|
| 409 |
+
__m256i zp) {
|
| 410 |
+
static_assert(
|
| 411 |
+
std::is_same_v<T, int8_t> || std::is_same_v<T, uint8_t>,
|
| 412 |
+
"Only int8_t/uint8_t are supported");
|
| 413 |
+
constexpr auto min_val = std::numeric_limits<T>::min();
|
| 414 |
+
constexpr auto max_val = std::numeric_limits<T>::max();
|
| 415 |
+
__m256i permute_mask_v =
|
| 416 |
+
_mm256_set_epi32(0x07, 0x03, 0x06, 0x02, 0x05, 0x01, 0x04, 0x00);
|
| 417 |
+
__m256 x_scaled_v = _mm256_mul_ps(_mm256_cvtepi32_ps(inp[0]), multiplier);
|
| 418 |
+
__m256 y_scaled_v = _mm256_mul_ps(_mm256_cvtepi32_ps(inp[1]), multiplier);
|
| 419 |
+
__m256 z_scaled_v = _mm256_mul_ps(_mm256_cvtepi32_ps(inp[2]), multiplier);
|
| 420 |
+
__m256 w_scaled_v = _mm256_mul_ps(_mm256_cvtepi32_ps(inp[3]), multiplier);
|
| 421 |
+
|
| 422 |
+
__m256i x_rounded_v = _mm256_cvtps_epi32(x_scaled_v);
|
| 423 |
+
__m256i y_rounded_v = _mm256_cvtps_epi32(y_scaled_v);
|
| 424 |
+
__m256i z_rounded_v = _mm256_cvtps_epi32(z_scaled_v);
|
| 425 |
+
__m256i w_rounded_v = _mm256_cvtps_epi32(w_scaled_v);
|
| 426 |
+
|
| 427 |
+
/* Add zero point */
|
| 428 |
+
__m256i x_v = _mm256_add_epi32(x_rounded_v, zp);
|
| 429 |
+
__m256i y_v = _mm256_add_epi32(y_rounded_v, zp);
|
| 430 |
+
__m256i z_v = _mm256_add_epi32(z_rounded_v, zp);
|
| 431 |
+
__m256i w_v = _mm256_add_epi32(w_rounded_v, zp);
|
| 432 |
+
|
| 433 |
+
/* Pack to int16_t and saturate */
|
| 434 |
+
__m256i xy_packed_v = _mm256_packs_epi32(x_v, y_v);
|
| 435 |
+
__m256i zw_packed_v = _mm256_packs_epi32(z_v, w_v);
|
| 436 |
+
|
| 437 |
+
__m256i xyzw_clamped_v =
|
| 438 |
+
pack_saturate_and_clamp<T>(xy_packed_v, zw_packed_v, min_val, max_val);
|
| 439 |
+
|
| 440 |
+
/*
|
| 441 |
+
* xyzw_clamped_v has results in the following layout so we need to
|
| 442 |
+
* permute: x0-3 y0-3 z0-3 w0-3 x4-7 y4-7 z4-7 w4-7
|
| 443 |
+
*/
|
| 444 |
+
xyzw_clamped_v = _mm256_permutevar8x32_epi32(xyzw_clamped_v, permute_mask_v);
|
| 445 |
+
return xyzw_clamped_v;
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
template<>
|
| 449 |
+
struct Vectorized<c10::qint8> : public Vectorizedqi {
|
| 450 |
+
static constexpr int size() {
|
| 451 |
+
return 32;
|
| 452 |
+
}
|
| 453 |
+
|
| 454 |
+
static constexpr int float_num_vecs() {
|
| 455 |
+
return 4;
|
| 456 |
+
}
|
| 457 |
+
|
| 458 |
+
static constexpr int int_num_vecs() {
|
| 459 |
+
return 4;
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
using float_vec_return_type = std::array<Vectorized<float>, 4>;
|
| 463 |
+
using int_vec_return_type = std::array<Vectorized<c10::qint32>, 4>;
|
| 464 |
+
using value_type = typename c10::qint8::underlying;
|
| 465 |
+
|
| 466 |
+
public:
|
| 467 |
+
using Vectorizedqi::Vectorizedqi;
|
| 468 |
+
|
| 469 |
+
Vectorized() {}
|
| 470 |
+
Vectorized(__m256i vals_) { vals = vals_;}
|
| 471 |
+
|
| 472 |
+
// Broadcast constructor
|
| 473 |
+
Vectorized(const c10::qint8& val) {
|
| 474 |
+
value_type uw = val.val_;
|
| 475 |
+
vals = _mm256_set1_epi8(uw);
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
// This is needed because the compiler emits awful code for the default
|
| 479 |
+
// constructor for moving the enum
|
| 480 |
+
// NOLINTNEXTLINE(clang-diagnostic-deprecated-copy)
|
| 481 |
+
C10_CLANG_DIAGNOSTIC_PUSH()
|
| 482 |
+
#if C10_CLANG_HAS_WARNING("-Wdeprecated-copy")
|
| 483 |
+
C10_CLANG_DIAGNOSTIC_IGNORE("-Wdeprecated-copy")
|
| 484 |
+
#endif
|
| 485 |
+
Vectorized(const Vectorized<c10::qint8>& other) : Vectorizedqi(other.vals) { }
|
| 486 |
+
C10_CLANG_DIAGNOSTIC_POP()
|
| 487 |
+
|
| 488 |
+
void store(void* ptr, int count = size()) const {
|
| 489 |
+
if (count != size()) {
|
| 490 |
+
memcpy(ptr, &vals, count * sizeof(value_type));
|
| 491 |
+
} else {
|
| 492 |
+
_mm256_storeu_si256((__m256i*)ptr, vals);
|
| 493 |
+
}
|
| 494 |
+
}
|
| 495 |
+
|
| 496 |
+
static Vectorized<c10::qint8> loadu(const void* ptr) {
|
| 497 |
+
return Vectorized<c10::qint8>(ptr);
|
| 498 |
+
}
|
| 499 |
+
|
| 500 |
+
static Vectorized<c10::qint8> loadu(const void* ptr, int64_t count) {
|
| 501 |
+
__at_align__ value_type tmp_values[size()];
|
| 502 |
+
// Ensure uninitialized memory does not change the output value See https://github.com/pytorch/pytorch/issues/32502
|
| 503 |
+
// for more details. We do not initialize arrays to zero using "={0}" because gcc would compile it to two
|
| 504 |
+
// instructions while a loop would be compiled to one instruction.
|
| 505 |
+
for (const auto i : c10::irange(size())) {
|
| 506 |
+
tmp_values[i] = 0;
|
| 507 |
+
}
|
| 508 |
+
std::memcpy(
|
| 509 |
+
tmp_values, reinterpret_cast<const value_type*>(ptr), count * sizeof(value_type));
|
| 510 |
+
return _mm256_loadu_si256((const __m256i*)tmp_values);
|
| 511 |
+
}
|
| 512 |
+
|
| 513 |
+
private:
|
| 514 |
+
__m256i cvtepi8_epi32(__m128i epi8_vals) const {
|
| 515 |
+
return _mm256_cvtepi8_epi32(epi8_vals);
|
| 516 |
+
}
|
| 517 |
+
|
| 518 |
+
public:
|
| 519 |
+
float_vec_return_type dequantize(
|
| 520 |
+
Vectorized<float> scale,
|
| 521 |
+
Vectorized<float> /*zero_point*/,
|
| 522 |
+
Vectorized<float> scale_neg_zp_premul) const {
|
| 523 |
+
__m128i int_val0 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 0));
|
| 524 |
+
__m128i int_val1 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 1));
|
| 525 |
+
__m128i int_val2 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 2));
|
| 526 |
+
__m128i int_val3 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 3));
|
| 527 |
+
|
| 528 |
+
__m256 float_val0 = _mm256_cvtepi32_ps(cvtepi8_epi32(int_val0));
|
| 529 |
+
__m256 float_val1 = _mm256_cvtepi32_ps(cvtepi8_epi32(int_val1));
|
| 530 |
+
__m256 float_val2 = _mm256_cvtepi32_ps(cvtepi8_epi32(int_val2));
|
| 531 |
+
__m256 float_val3 = _mm256_cvtepi32_ps(cvtepi8_epi32(int_val3));
|
| 532 |
+
|
| 533 |
+
auto val0 =
|
| 534 |
+
vec::fmadd(scale, Vectorized<float>(float_val0), scale_neg_zp_premul);
|
| 535 |
+
auto val1 =
|
| 536 |
+
vec::fmadd(scale, Vectorized<float>(float_val1), scale_neg_zp_premul);
|
| 537 |
+
auto val2 =
|
| 538 |
+
vec::fmadd(scale, Vectorized<float>(float_val2), scale_neg_zp_premul);
|
| 539 |
+
auto val3 =
|
| 540 |
+
vec::fmadd(scale, Vectorized<float>(float_val3), scale_neg_zp_premul);
|
| 541 |
+
return {val0, val1, val2, val3};
|
| 542 |
+
}
|
| 543 |
+
|
| 544 |
+
float_vec_return_type dequantize(
|
| 545 |
+
Vectorized<float> scale,
|
| 546 |
+
Vectorized<float> zero_point) const {
|
| 547 |
+
__m128i int_val0 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 0));
|
| 548 |
+
__m128i int_val1 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 1));
|
| 549 |
+
__m128i int_val2 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 2));
|
| 550 |
+
__m128i int_val3 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 3));
|
| 551 |
+
|
| 552 |
+
__m256 float_val0 = _mm256_cvtepi32_ps(cvtepi8_epi32(int_val0));
|
| 553 |
+
__m256 float_val1 = _mm256_cvtepi32_ps(cvtepi8_epi32(int_val1));
|
| 554 |
+
__m256 float_val2 = _mm256_cvtepi32_ps(cvtepi8_epi32(int_val2));
|
| 555 |
+
__m256 float_val3 = _mm256_cvtepi32_ps(cvtepi8_epi32(int_val3));
|
| 556 |
+
|
| 557 |
+
auto val0 = (Vectorized<float>(float_val0) - zero_point) * scale;
|
| 558 |
+
auto val1 = (Vectorized<float>(float_val1) - zero_point) * scale;
|
| 559 |
+
auto val2 = (Vectorized<float>(float_val2) - zero_point) * scale;
|
| 560 |
+
auto val3 = (Vectorized<float>(float_val3) - zero_point) * scale;
|
| 561 |
+
return {val0, val1, val2, val3};
|
| 562 |
+
}
|
| 563 |
+
|
| 564 |
+
static Vectorized<c10::qint8> quantize(
|
| 565 |
+
const float_vec_return_type& rhs,
|
| 566 |
+
float /*scale*/,
|
| 567 |
+
int32_t zero_point,
|
| 568 |
+
float inverse_scale) {
|
| 569 |
+
auto* rhs_data = (float*)rhs.data();
|
| 570 |
+
int8_t quantized_values[32];
|
| 571 |
+
QuantizeAvx2<value_type>(
|
| 572 |
+
rhs_data, quantized_values, 32, inverse_scale, zero_point);
|
| 573 |
+
return Vectorized<c10::qint8>::loadu(quantized_values);
|
| 574 |
+
}
|
| 575 |
+
|
| 576 |
+
Vectorized<c10::qint8> maximum(Vectorized<c10::qint8> b) const {
|
| 577 |
+
return _mm256_max_epi8(vals, b.vals);
|
| 578 |
+
}
|
| 579 |
+
|
| 580 |
+
Vectorized<c10::qint8> minimum(Vectorized<c10::qint8> b) const {
|
| 581 |
+
return _mm256_min_epi8(vals, b.vals);
|
| 582 |
+
}
|
| 583 |
+
|
| 584 |
+
Vectorized<c10::qint8> relu(Vectorized<c10::qint8> zero_point) const {
|
| 585 |
+
return maximum(zero_point);
|
| 586 |
+
}
|
| 587 |
+
|
| 588 |
+
Vectorized<c10::qint8> relu6(
|
| 589 |
+
Vectorized<c10::qint8> zero_point,
|
| 590 |
+
Vectorized<c10::qint8> q_six) {
|
| 591 |
+
return _mm256_min_epi8(
|
| 592 |
+
_mm256_max_epi8(vals, zero_point.vals), q_six.vals);
|
| 593 |
+
}
|
| 594 |
+
|
| 595 |
+
int_vec_return_type widening_subtract(Vectorized<c10::qint8> b) const {
|
| 596 |
+
__m128i int_val0 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 0));
|
| 597 |
+
__m128i int_val1 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 1));
|
| 598 |
+
__m128i int_val2 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 2));
|
| 599 |
+
__m128i int_val3 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 3));
|
| 600 |
+
|
| 601 |
+
__m256i int32_val0 = cvtepi8_epi32(int_val0);
|
| 602 |
+
__m256i int32_val1 = cvtepi8_epi32(int_val1);
|
| 603 |
+
__m256i int32_val2 = cvtepi8_epi32(int_val2);
|
| 604 |
+
__m256i int32_val3 = cvtepi8_epi32(int_val3);
|
| 605 |
+
|
| 606 |
+
__m128i int_b0 = _mm_set1_epi64x(_mm256_extract_epi64(b, 0));
|
| 607 |
+
__m128i int_b1 = _mm_set1_epi64x(_mm256_extract_epi64(b, 1));
|
| 608 |
+
__m128i int_b2 = _mm_set1_epi64x(_mm256_extract_epi64(b, 2));
|
| 609 |
+
__m128i int_b3 = _mm_set1_epi64x(_mm256_extract_epi64(b, 3));
|
| 610 |
+
|
| 611 |
+
__m256i int32_b0 = cvtepi8_epi32(int_b0);
|
| 612 |
+
__m256i int32_b1 = cvtepi8_epi32(int_b1);
|
| 613 |
+
__m256i int32_b2 = cvtepi8_epi32(int_b2);
|
| 614 |
+
__m256i int32_b3 = cvtepi8_epi32(int_b3);
|
| 615 |
+
|
| 616 |
+
__m256i res_0 = _mm256_sub_epi32(int32_val0, int32_b0);
|
| 617 |
+
__m256i res_1 = _mm256_sub_epi32(int32_val1, int32_b1);
|
| 618 |
+
__m256i res_2 = _mm256_sub_epi32(int32_val2, int32_b2);
|
| 619 |
+
__m256i res_3 = _mm256_sub_epi32(int32_val3, int32_b3);
|
| 620 |
+
|
| 621 |
+
return {Vectorized<c10::qint32>(res_0),
|
| 622 |
+
Vectorized<c10::qint32>(res_1),
|
| 623 |
+
Vectorized<c10::qint32>(res_2),
|
| 624 |
+
Vectorized<c10::qint32>(res_3)};
|
| 625 |
+
}
|
| 626 |
+
|
| 627 |
+
static Vectorized<c10::qint8> requantize_from_int(
|
| 628 |
+
const int_vec_return_type& inp,
|
| 629 |
+
float multiplier,
|
| 630 |
+
int32_t zero_point) {
|
| 631 |
+
__m256 multiplier_v = _mm256_set1_ps(multiplier);
|
| 632 |
+
__m256i zero_point_v = _mm256_set1_epi32(zero_point);
|
| 633 |
+
return RequantizeAvx2<value_type>(inp, multiplier_v, zero_point_v);
|
| 634 |
+
}
|
| 635 |
+
|
| 636 |
+
private:
|
| 637 |
+
// Load from memory constructor
|
| 638 |
+
Vectorized(const void* ptr) {
|
| 639 |
+
vals = _mm256_loadu_si256((const __m256i*)ptr);
|
| 640 |
+
}
|
| 641 |
+
};
|
| 642 |
+
|
| 643 |
+
template <>
|
| 644 |
+
Vectorized<c10::qint8> inline maximum(const Vectorized<c10::qint8>& a, const Vectorized<c10::qint8>& b) {
|
| 645 |
+
return a.maximum(b);
|
| 646 |
+
}
|
| 647 |
+
|
| 648 |
+
template<>
|
| 649 |
+
struct Vectorized<c10::quint8> : public Vectorizedqi {
|
| 650 |
+
static constexpr int size() {
|
| 651 |
+
return 32;
|
| 652 |
+
}
|
| 653 |
+
|
| 654 |
+
static constexpr int float_num_vecs() {
|
| 655 |
+
return 4;
|
| 656 |
+
}
|
| 657 |
+
|
| 658 |
+
static constexpr int int_num_vecs() {
|
| 659 |
+
return 4;
|
| 660 |
+
}
|
| 661 |
+
|
| 662 |
+
using float_vec_return_type = std::array<Vectorized<float>, 4>;
|
| 663 |
+
using int_vec_return_type = std::array<Vectorized<c10::qint32>, 4>;
|
| 664 |
+
using value_type = typename c10::quint8::underlying;
|
| 665 |
+
|
| 666 |
+
public:
|
| 667 |
+
using Vectorizedqi::Vectorizedqi;
|
| 668 |
+
Vectorized() {}
|
| 669 |
+
|
| 670 |
+
Vectorized(__m256i vals_) { vals = vals_;}
|
| 671 |
+
|
| 672 |
+
// Broadcast constructor
|
| 673 |
+
Vectorized(const c10::quint8& val) {
|
| 674 |
+
value_type uw = val.val_;
|
| 675 |
+
vals = _mm256_set1_epi8(uw);
|
| 676 |
+
}
|
| 677 |
+
|
| 678 |
+
// NOLINTNEXTLINE(clang-diagnostic-deprecated-copy)
|
| 679 |
+
C10_CLANG_DIAGNOSTIC_PUSH()
|
| 680 |
+
#if C10_CLANG_HAS_WARNING("-Wdeprecated-copy")
|
| 681 |
+
C10_CLANG_DIAGNOSTIC_IGNORE("-Wdeprecated-copy")
|
| 682 |
+
#endif
|
| 683 |
+
Vectorized(const Vectorized<c10::quint8>& other) : Vectorizedqi(other.vals) { }
|
| 684 |
+
C10_CLANG_DIAGNOSTIC_POP()
|
| 685 |
+
|
| 686 |
+
void store(void* ptr, int count = size()) const {
|
| 687 |
+
if (count != size()) {
|
| 688 |
+
memcpy(ptr, &vals, count * sizeof(value_type));
|
| 689 |
+
} else {
|
| 690 |
+
_mm256_storeu_si256((__m256i*)ptr, vals);
|
| 691 |
+
}
|
| 692 |
+
}
|
| 693 |
+
|
| 694 |
+
static Vectorized<c10::quint8> loadu(const void* ptr) {
|
| 695 |
+
return Vectorized<c10::quint8>(ptr);
|
| 696 |
+
}
|
| 697 |
+
|
| 698 |
+
static Vectorized<c10::quint8> loadu(const void* ptr, int64_t count) {
|
| 699 |
+
__at_align__ value_type tmp_values[size()];
|
| 700 |
+
// Ensure uninitialized memory does not change the output value See https://github.com/pytorch/pytorch/issues/32502
|
| 701 |
+
// for more details. We do not initialize arrays to zero using "={0}" because gcc would compile it to two
|
| 702 |
+
// instructions while a loop would be compiled to one instruction.
|
| 703 |
+
for (const auto i : c10::irange(size())) {
|
| 704 |
+
tmp_values[i] = 0;
|
| 705 |
+
}
|
| 706 |
+
std::memcpy(
|
| 707 |
+
tmp_values, reinterpret_cast<const value_type*>(ptr), count * sizeof(value_type));
|
| 708 |
+
return _mm256_loadu_si256((const __m256i*)tmp_values);
|
| 709 |
+
}
|
| 710 |
+
|
| 711 |
+
private:
|
| 712 |
+
__m256i cvtepu8_epi32(__m128i epu8_vals) const {
|
| 713 |
+
return _mm256_cvtepu8_epi32(epu8_vals);
|
| 714 |
+
}
|
| 715 |
+
|
| 716 |
+
public:
|
| 717 |
+
float_vec_return_type dequantize(
|
| 718 |
+
Vectorized<float> scale,
|
| 719 |
+
Vectorized<float> /*zero_point*/,
|
| 720 |
+
Vectorized<float> scale_zp_premul) const {
|
| 721 |
+
__m128i int_val0 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 0));
|
| 722 |
+
__m128i int_val1 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 1));
|
| 723 |
+
__m128i int_val2 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 2));
|
| 724 |
+
__m128i int_val3 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 3));
|
| 725 |
+
|
| 726 |
+
__m256 float_val0 = _mm256_cvtepi32_ps(cvtepu8_epi32(int_val0));
|
| 727 |
+
__m256 float_val1 = _mm256_cvtepi32_ps(cvtepu8_epi32(int_val1));
|
| 728 |
+
__m256 float_val2 = _mm256_cvtepi32_ps(cvtepu8_epi32(int_val2));
|
| 729 |
+
__m256 float_val3 = _mm256_cvtepi32_ps(cvtepu8_epi32(int_val3));
|
| 730 |
+
|
| 731 |
+
auto val0 =
|
| 732 |
+
vec::fmadd(scale, Vectorized<float>(float_val0), scale_zp_premul);
|
| 733 |
+
auto val1 =
|
| 734 |
+
vec::fmadd(scale, Vectorized<float>(float_val1), scale_zp_premul);
|
| 735 |
+
auto val2 =
|
| 736 |
+
vec::fmadd(scale, Vectorized<float>(float_val2), scale_zp_premul);
|
| 737 |
+
auto val3 =
|
| 738 |
+
vec::fmadd(scale, Vectorized<float>(float_val3), scale_zp_premul);
|
| 739 |
+
return {val0, val1, val2, val3};
|
| 740 |
+
}
|
| 741 |
+
|
| 742 |
+
float_vec_return_type dequantize(
|
| 743 |
+
Vectorized<float> scale,
|
| 744 |
+
Vectorized<float> zero_point) const {
|
| 745 |
+
__m128i int_val0 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 0));
|
| 746 |
+
__m128i int_val1 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 1));
|
| 747 |
+
__m128i int_val2 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 2));
|
| 748 |
+
__m128i int_val3 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 3));
|
| 749 |
+
|
| 750 |
+
__m256 float_val0 = _mm256_cvtepi32_ps(cvtepu8_epi32(int_val0));
|
| 751 |
+
__m256 float_val1 = _mm256_cvtepi32_ps(cvtepu8_epi32(int_val1));
|
| 752 |
+
__m256 float_val2 = _mm256_cvtepi32_ps(cvtepu8_epi32(int_val2));
|
| 753 |
+
__m256 float_val3 = _mm256_cvtepi32_ps(cvtepu8_epi32(int_val3));
|
| 754 |
+
|
| 755 |
+
auto val0 = (Vectorized<float>(float_val0) - zero_point) * scale;
|
| 756 |
+
auto val1 = (Vectorized<float>(float_val1) - zero_point) * scale;
|
| 757 |
+
auto val2 = (Vectorized<float>(float_val2) - zero_point) * scale;
|
| 758 |
+
auto val3 = (Vectorized<float>(float_val3) - zero_point) * scale;
|
| 759 |
+
return {val0, val1, val2, val3};
|
| 760 |
+
}
|
| 761 |
+
|
| 762 |
+
static Vectorized<c10::quint8> quantize(
|
| 763 |
+
const float_vec_return_type& rhs,
|
| 764 |
+
float /*scale*/,
|
| 765 |
+
int32_t zero_point,
|
| 766 |
+
float inverse_scale) {
|
| 767 |
+
auto* rhs_data = (float*)rhs.data();
|
| 768 |
+
uint8_t quantized_values[32];
|
| 769 |
+
QuantizeAvx2<value_type>(
|
| 770 |
+
rhs_data, quantized_values, 32, inverse_scale, zero_point);
|
| 771 |
+
return Vectorized<c10::quint8>::loadu(quantized_values);
|
| 772 |
+
}
|
| 773 |
+
|
| 774 |
+
Vectorized<c10::quint8> maximum(Vectorized<c10::quint8> b) const {
|
| 775 |
+
return _mm256_max_epu8(vals, b.vals);
|
| 776 |
+
}
|
| 777 |
+
|
| 778 |
+
Vectorized<c10::quint8> minimum(Vectorized<c10::quint8> b) const {
|
| 779 |
+
return _mm256_min_epu8(vals, b.vals);
|
| 780 |
+
}
|
| 781 |
+
|
| 782 |
+
Vectorized<c10::quint8> relu(Vectorized<c10::quint8> zero_point) const {
|
| 783 |
+
return maximum(zero_point);
|
| 784 |
+
}
|
| 785 |
+
|
| 786 |
+
Vectorized<c10::quint8> relu6(
|
| 787 |
+
Vectorized<c10::quint8> zero_point,
|
| 788 |
+
Vectorized<c10::quint8> q_six) {
|
| 789 |
+
return _mm256_min_epu8(
|
| 790 |
+
_mm256_max_epu8(vals, zero_point.vals), q_six.vals);
|
| 791 |
+
}
|
| 792 |
+
|
| 793 |
+
int_vec_return_type widening_subtract(Vectorized<c10::quint8> b) const {
|
| 794 |
+
__m128i int_val0 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 0));
|
| 795 |
+
__m128i int_val1 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 1));
|
| 796 |
+
__m128i int_val2 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 2));
|
| 797 |
+
__m128i int_val3 = _mm_set1_epi64x(_mm256_extract_epi64(vals, 3));
|
| 798 |
+
|
| 799 |
+
__m256i int32_val0 = cvtepu8_epi32(int_val0);
|
| 800 |
+
__m256i int32_val1 = cvtepu8_epi32(int_val1);
|
| 801 |
+
__m256i int32_val2 = cvtepu8_epi32(int_val2);
|
| 802 |
+
__m256i int32_val3 = cvtepu8_epi32(int_val3);
|
| 803 |
+
|
| 804 |
+
__m128i int_b0 = _mm_set1_epi64x(_mm256_extract_epi64(b, 0));
|
| 805 |
+
__m128i int_b1 = _mm_set1_epi64x(_mm256_extract_epi64(b, 1));
|
| 806 |
+
__m128i int_b2 = _mm_set1_epi64x(_mm256_extract_epi64(b, 2));
|
| 807 |
+
__m128i int_b3 = _mm_set1_epi64x(_mm256_extract_epi64(b, 3));
|
| 808 |
+
|
| 809 |
+
__m256i int32_b0 = cvtepu8_epi32(int_b0);
|
| 810 |
+
__m256i int32_b1 = cvtepu8_epi32(int_b1);
|
| 811 |
+
__m256i int32_b2 = cvtepu8_epi32(int_b2);
|
| 812 |
+
__m256i int32_b3 = cvtepu8_epi32(int_b3);
|
| 813 |
+
|
| 814 |
+
__m256i res_0 = _mm256_sub_epi32(int32_val0, int32_b0);
|
| 815 |
+
__m256i res_1 = _mm256_sub_epi32(int32_val1, int32_b1);
|
| 816 |
+
__m256i res_2 = _mm256_sub_epi32(int32_val2, int32_b2);
|
| 817 |
+
__m256i res_3 = _mm256_sub_epi32(int32_val3, int32_b3);
|
| 818 |
+
return {Vectorized<c10::qint32>(res_0),
|
| 819 |
+
Vectorized<c10::qint32>(res_1),
|
| 820 |
+
Vectorized<c10::qint32>(res_2),
|
| 821 |
+
Vectorized<c10::qint32>(res_3)};
|
| 822 |
+
}
|
| 823 |
+
|
| 824 |
+
static Vectorized<c10::quint8> requantize_from_int(
|
| 825 |
+
const int_vec_return_type& inp,
|
| 826 |
+
float multiplier,
|
| 827 |
+
int32_t zero_point) {
|
| 828 |
+
__m256 multiplier_v = _mm256_set1_ps(multiplier);
|
| 829 |
+
__m256i zero_point_v = _mm256_set1_epi32(zero_point);
|
| 830 |
+
return RequantizeAvx2<value_type>(inp, multiplier_v, zero_point_v);
|
| 831 |
+
}
|
| 832 |
+
|
| 833 |
+
private:
|
| 834 |
+
|
| 835 |
+
// Load from memory constructor
|
| 836 |
+
Vectorized(const void* ptr) {
|
| 837 |
+
vals = _mm256_loadu_si256((const __m256i*)ptr);
|
| 838 |
+
}
|
| 839 |
+
};
|
| 840 |
+
|
| 841 |
+
template <>
|
| 842 |
+
Vectorized<c10::quint8> inline maximum(const Vectorized<c10::quint8>& a, const Vectorized<c10::quint8>& b) {
|
| 843 |
+
return a.maximum(b);
|
| 844 |
+
}
|
| 845 |
+
|
| 846 |
+
#else
|
| 847 |
+
|
| 848 |
+
// NOTE: These are low-performance implementations that we fall back on
|
| 849 |
+
// if we are not building with AVX2. This may not be an issue, because
|
| 850 |
+
// currently for quantization we assume the user has at least AVX512
|
| 851 |
+
// installed, so these can simply act as a reference implementation.
|
| 852 |
+
//
|
| 853 |
+
// If in the future we relax this requirement (AVX2+), we should probably
|
| 854 |
+
// revisit these implementations
|
| 855 |
+
|
| 856 |
+
template <
|
| 857 |
+
typename T,
|
| 858 |
+
typename float_vec_return_type_,
|
| 859 |
+
typename int_vec_return_type_,
|
| 860 |
+
int size_>
|
| 861 |
+
struct VectorizedQuantizedConverter {
|
| 862 |
+
static constexpr int size() {
|
| 863 |
+
return size_;
|
| 864 |
+
}
|
| 865 |
+
|
| 866 |
+
static constexpr int float_num_vecs() {
|
| 867 |
+
return size() / 8;
|
| 868 |
+
}
|
| 869 |
+
|
| 870 |
+
static constexpr int int_num_vecs() {
|
| 871 |
+
return size() / 8;
|
| 872 |
+
}
|
| 873 |
+
|
| 874 |
+
using float_vec_return_type = float_vec_return_type_;
|
| 875 |
+
using int_vec_return_type = int_vec_return_type_;
|
| 876 |
+
|
| 877 |
+
using value_type = typename T::underlying;
|
| 878 |
+
std::array<value_type, size_> vals;
|
| 879 |
+
|
| 880 |
+
VectorizedQuantizedConverter(T val) {
|
| 881 |
+
for (const auto i : c10::irange(size())) {
|
| 882 |
+
vals[i] = val.val_;
|
| 883 |
+
}
|
| 884 |
+
}
|
| 885 |
+
|
| 886 |
+
VectorizedQuantizedConverter(const void* ptr) {
|
| 887 |
+
memcpy(vals.data(), ptr, sizeof(value_type) * size());
|
| 888 |
+
}
|
| 889 |
+
|
| 890 |
+
void store(void* ptr, int count = size()) const {
|
| 891 |
+
memcpy(ptr, vals.data(), count * sizeof(value_type));
|
| 892 |
+
}
|
| 893 |
+
|
| 894 |
+
float_vec_return_type dequantize(
|
| 895 |
+
Vectorized<float> scale,
|
| 896 |
+
Vectorized<float> zero_point,
|
| 897 |
+
Vectorized<float> /*scale_zp_premul*/) const {
|
| 898 |
+
float_vec_return_type rv;
|
| 899 |
+
for (const auto i : c10::irange(float_num_vecs())) {
|
| 900 |
+
float tmp_vals[8];
|
| 901 |
+
for (const auto j : c10::irange(8)) {
|
| 902 |
+
tmp_vals[j] = at::native::dequantize_val<T>(
|
| 903 |
+
scale[j], zero_point[j], T(vals[8 * i + j]));
|
| 904 |
+
}
|
| 905 |
+
rv[i] = Vectorized<float>(tmp_vals[0],
|
| 906 |
+
tmp_vals[1],
|
| 907 |
+
tmp_vals[2],
|
| 908 |
+
tmp_vals[3],
|
| 909 |
+
tmp_vals[4],
|
| 910 |
+
tmp_vals[5],
|
| 911 |
+
tmp_vals[6],
|
| 912 |
+
tmp_vals[7]);
|
| 913 |
+
}
|
| 914 |
+
return rv;
|
| 915 |
+
}
|
| 916 |
+
|
| 917 |
+
float_vec_return_type dequantize(
|
| 918 |
+
Vectorized<float> scale,
|
| 919 |
+
Vectorized<float> zero_point) const {
|
| 920 |
+
Vectorized<float> scale_zp_premul;
|
| 921 |
+
return dequantize(scale, zero_point, scale_zp_premul);
|
| 922 |
+
}
|
| 923 |
+
|
| 924 |
+
protected:
|
| 925 |
+
VectorizedQuantizedConverter() {}
|
| 926 |
+
};
|
| 927 |
+
|
| 928 |
+
template <>
|
| 929 |
+
struct Vectorized<c10::qint32> : public VectorizedQuantizedConverter<
|
| 930 |
+
c10::qint32,
|
| 931 |
+
std::array<Vectorized<float>, 1>,
|
| 932 |
+
std::array<Vectorized<c10::qint32>, 1>,
|
| 933 |
+
8> {
|
| 934 |
+
Vectorized()
|
| 935 |
+
: VectorizedQuantizedConverter<
|
| 936 |
+
c10::qint32,
|
| 937 |
+
std::array<Vectorized<float>, 1>,
|
| 938 |
+
std::array<Vectorized<c10::qint32>, 1>,
|
| 939 |
+
8>() {}
|
| 940 |
+
Vectorized(c10::qint32 val)
|
| 941 |
+
: VectorizedQuantizedConverter<
|
| 942 |
+
c10::qint32,
|
| 943 |
+
std::array<Vectorized<float>, 1>,
|
| 944 |
+
std::array<Vectorized<c10::qint32>, 1>,
|
| 945 |
+
8>(val) {}
|
| 946 |
+
Vectorized(const void* ptr)
|
| 947 |
+
: VectorizedQuantizedConverter<
|
| 948 |
+
c10::qint32,
|
| 949 |
+
std::array<Vectorized<float>, 1>,
|
| 950 |
+
std::array<Vectorized<c10::qint32>, 1>,
|
| 951 |
+
8>(ptr) {}
|
| 952 |
+
|
| 953 |
+
static Vectorized<c10::qint32> loadu(const void* ptr) {
|
| 954 |
+
return Vectorized<c10::qint32>(ptr);
|
| 955 |
+
}
|
| 956 |
+
|
| 957 |
+
static Vectorized<c10::qint32> loadu(const void* ptr, int64_t count) {
|
| 958 |
+
__at_align__ value_type tmp_values[size()];
|
| 959 |
+
// Ensure uninitialized memory does not change the output value See https://github.com/pytorch/pytorch/issues/32502
|
| 960 |
+
// for more details. We do not initialize arrays to zero using "={0}" because gcc would compile it to two
|
| 961 |
+
// instructions while a loop would be compiled to one instruction.
|
| 962 |
+
for (const auto i : c10::irange(size())) {
|
| 963 |
+
tmp_values[i] = 0;
|
| 964 |
+
}
|
| 965 |
+
std::memcpy(
|
| 966 |
+
tmp_values, reinterpret_cast<const value_type*>(ptr), count * sizeof(value_type));
|
| 967 |
+
return Vectorized<c10::qint32>(tmp_values);
|
| 968 |
+
}
|
| 969 |
+
|
| 970 |
+
static Vectorized<c10::qint32> quantize(
|
| 971 |
+
const float_vec_return_type& rhs,
|
| 972 |
+
float scale,
|
| 973 |
+
int32_t zero_point,
|
| 974 |
+
float /*inverse_scale*/) {
|
| 975 |
+
std::array<value_type, size()> qvals;
|
| 976 |
+
std::array<float, float_num_vecs() * 8> float_vals;
|
| 977 |
+
|
| 978 |
+
for (const auto i : c10::irange(float_num_vecs())) {
|
| 979 |
+
rhs[i].store(&float_vals[i * 8], 8);
|
| 980 |
+
}
|
| 981 |
+
|
| 982 |
+
at::native::quantize_vec<c10::qint32, /*precision=*/32>(
|
| 983 |
+
scale,
|
| 984 |
+
zero_point,
|
| 985 |
+
float_vals.data(),
|
| 986 |
+
(c10::qint32*)qvals.data(),
|
| 987 |
+
8 * float_num_vecs());
|
| 988 |
+
|
| 989 |
+
return Vectorized<c10::qint32>::loadu(qvals.data());
|
| 990 |
+
}
|
| 991 |
+
|
| 992 |
+
Vectorized<c10::qint32> maximum(Vectorized<c10::qint32> b) const {
|
| 993 |
+
Vectorized<c10::qint32> retval;
|
| 994 |
+
for (const auto i : c10::irange(size())) {
|
| 995 |
+
retval.vals[i] = std::max<value_type>(vals[i], b.vals[i]);
|
| 996 |
+
}
|
| 997 |
+
return retval;
|
| 998 |
+
}
|
| 999 |
+
|
| 1000 |
+
Vectorized<c10::qint32> minimum(Vectorized<c10::qint32> b) const {
|
| 1001 |
+
Vectorized<c10::qint32> retval;
|
| 1002 |
+
for (const auto i : c10::irange(size())) {
|
| 1003 |
+
retval.vals[i] = std::min<value_type>(vals[i], b.vals[i]);
|
| 1004 |
+
}
|
| 1005 |
+
return retval;
|
| 1006 |
+
}
|
| 1007 |
+
|
| 1008 |
+
Vectorized<c10::qint32> relu(Vectorized<c10::qint32> zero_point) const {
|
| 1009 |
+
return maximum(zero_point);
|
| 1010 |
+
}
|
| 1011 |
+
|
| 1012 |
+
|
| 1013 |
+
Vectorized<c10::qint32> relu6(
|
| 1014 |
+
Vectorized<c10::qint32> zero_point,
|
| 1015 |
+
Vectorized<c10::qint32> q_six) {
|
| 1016 |
+
Vectorized<c10::qint32> retval;
|
| 1017 |
+
for (const auto i : c10::irange(size())) {
|
| 1018 |
+
retval.vals[i] = std::min<value_type>(
|
| 1019 |
+
std::max<value_type>(vals[i], zero_point.vals[i]), q_six.vals[i]);
|
| 1020 |
+
}
|
| 1021 |
+
return retval;
|
| 1022 |
+
}
|
| 1023 |
+
|
| 1024 |
+
int_vec_return_type widening_subtract(Vectorized<c10::qint32> b) const {
|
| 1025 |
+
int_vec_return_type retval;
|
| 1026 |
+
for (const auto i : c10::irange(size())) {
|
| 1027 |
+
retval[0].vals[i] = vals[i] - b.vals[i];
|
| 1028 |
+
}
|
| 1029 |
+
return retval;
|
| 1030 |
+
}
|
| 1031 |
+
|
| 1032 |
+
static Vectorized<c10::qint32> requantize_from_int(
|
| 1033 |
+
const int_vec_return_type& inp,
|
| 1034 |
+
float multiplier,
|
| 1035 |
+
int32_t zero_point) {
|
| 1036 |
+
Vectorized<c10::qint32> retval;
|
| 1037 |
+
for (const auto i : c10::irange(size())) {
|
| 1038 |
+
retval.vals[i] =
|
| 1039 |
+
std::nearbyint(static_cast<float>(inp[0].vals[i]) * multiplier) +
|
| 1040 |
+
zero_point;
|
| 1041 |
+
}
|
| 1042 |
+
return retval;
|
| 1043 |
+
}
|
| 1044 |
+
};
|
| 1045 |
+
|
| 1046 |
+
template <>
|
| 1047 |
+
Vectorized<c10::qint32> inline maximum(const Vectorized<c10::qint32>& a, const Vectorized<c10::qint32>& b) {
|
| 1048 |
+
return a.maximum(b);
|
| 1049 |
+
}
|
| 1050 |
+
|
| 1051 |
+
template <>
|
| 1052 |
+
Vectorized<c10::qint32> inline operator*(
|
| 1053 |
+
const Vectorized<c10::qint32>& a,
|
| 1054 |
+
const Vectorized<c10::qint32>& b) {
|
| 1055 |
+
Vectorized<c10::qint32> retval;
|
| 1056 |
+
for (const auto i : c10::irange(std::decay_t<decltype(a)>::size())) {
|
| 1057 |
+
retval.vals[i] = a.vals[i] * b.vals[i];
|
| 1058 |
+
}
|
| 1059 |
+
return retval;
|
| 1060 |
+
}
|
| 1061 |
+
|
| 1062 |
+
template <>
|
| 1063 |
+
Vectorized<c10::qint32> inline operator+(
|
| 1064 |
+
const Vectorized<c10::qint32>& a,
|
| 1065 |
+
const Vectorized<c10::qint32>& b) {
|
| 1066 |
+
Vectorized<c10::qint32> retval;
|
| 1067 |
+
for (const auto i : c10::irange(std::decay_t<decltype(a)>::size())) {
|
| 1068 |
+
retval.vals[i] = a.vals[i] + b.vals[i];
|
| 1069 |
+
}
|
| 1070 |
+
return retval;
|
| 1071 |
+
}
|
| 1072 |
+
|
| 1073 |
+
template <>
|
| 1074 |
+
struct Vectorized<c10::qint8> : public VectorizedQuantizedConverter<
|
| 1075 |
+
c10::qint8,
|
| 1076 |
+
std::array<Vectorized<float>, 4>,
|
| 1077 |
+
std::array<Vectorized<c10::qint32>, 4>,
|
| 1078 |
+
32> {
|
| 1079 |
+
Vectorized()
|
| 1080 |
+
: VectorizedQuantizedConverter<
|
| 1081 |
+
c10::qint8,
|
| 1082 |
+
std::array<Vectorized<float>, 4>,
|
| 1083 |
+
std::array<Vectorized<c10::qint32>, 4>,
|
| 1084 |
+
32>() {}
|
| 1085 |
+
Vectorized(c10::qint8 val)
|
| 1086 |
+
: VectorizedQuantizedConverter<
|
| 1087 |
+
c10::qint8,
|
| 1088 |
+
std::array<Vectorized<float>, 4>,
|
| 1089 |
+
std::array<Vectorized<c10::qint32>, 4>,
|
| 1090 |
+
32>(val) {}
|
| 1091 |
+
Vectorized(const void* ptr)
|
| 1092 |
+
: VectorizedQuantizedConverter<
|
| 1093 |
+
c10::qint8,
|
| 1094 |
+
std::array<Vectorized<float>, 4>,
|
| 1095 |
+
std::array<Vectorized<c10::qint32>, 4>,
|
| 1096 |
+
32>(ptr) {}
|
| 1097 |
+
|
| 1098 |
+
static Vectorized<c10::qint8> loadu(const void* ptr) {
|
| 1099 |
+
return Vectorized<c10::qint8>(ptr);
|
| 1100 |
+
}
|
| 1101 |
+
|
| 1102 |
+
static Vectorized<c10::qint8> loadu(const void* ptr, int64_t count) {
|
| 1103 |
+
__at_align__ value_type tmp_values[size()];
|
| 1104 |
+
// Ensure uninitialized memory does not change the output value See https://github.com/pytorch/pytorch/issues/32502
|
| 1105 |
+
// for more details. We do not initialize arrays to zero using "={0}" because gcc would compile it to two
|
| 1106 |
+
// instructions while a loop would be compiled to one instruction.
|
| 1107 |
+
for (const auto i : c10::irange(size())) {
|
| 1108 |
+
tmp_values[i] = 0;
|
| 1109 |
+
}
|
| 1110 |
+
std::memcpy(
|
| 1111 |
+
tmp_values, reinterpret_cast<const value_type*>(ptr), count * sizeof(value_type));
|
| 1112 |
+
return Vectorized<c10::qint8>(tmp_values);
|
| 1113 |
+
}
|
| 1114 |
+
|
| 1115 |
+
static Vectorized<c10::qint8> quantize(
|
| 1116 |
+
const float_vec_return_type& rhs,
|
| 1117 |
+
float scale,
|
| 1118 |
+
int32_t zero_point,
|
| 1119 |
+
float /*inverse_scale*/) {
|
| 1120 |
+
std::array<value_type, size()> qvals;
|
| 1121 |
+
std::array<float, float_num_vecs() * 8> float_vals;
|
| 1122 |
+
|
| 1123 |
+
for (const auto i : c10::irange(float_num_vecs())) {
|
| 1124 |
+
rhs[i].store(&float_vals[i * 8], 8);
|
| 1125 |
+
}
|
| 1126 |
+
|
| 1127 |
+
at::native::quantize_vec<c10::qint8>(
|
| 1128 |
+
scale,
|
| 1129 |
+
zero_point,
|
| 1130 |
+
float_vals.data(),
|
| 1131 |
+
(c10::qint8*)qvals.data(),
|
| 1132 |
+
8 * float_num_vecs());
|
| 1133 |
+
|
| 1134 |
+
return Vectorized<c10::qint8>::loadu(qvals.data());
|
| 1135 |
+
}
|
| 1136 |
+
|
| 1137 |
+
Vectorized<c10::qint8> maximum(Vectorized<c10::qint8> b) const {
|
| 1138 |
+
Vectorized<c10::qint8> retval;
|
| 1139 |
+
for (const auto i : c10::irange(size())) {
|
| 1140 |
+
retval.vals[i] = std::max<value_type>(vals[i], b.vals[i]);
|
| 1141 |
+
}
|
| 1142 |
+
return retval;
|
| 1143 |
+
}
|
| 1144 |
+
|
| 1145 |
+
Vectorized<c10::qint8> minimum(Vectorized<c10::qint8> b) const {
|
| 1146 |
+
Vectorized<c10::qint8> retval;
|
| 1147 |
+
for (const auto i : c10::irange(size())) {
|
| 1148 |
+
retval.vals[i] = std::min<value_type>(vals[i], b.vals[i]);
|
| 1149 |
+
}
|
| 1150 |
+
return retval;
|
| 1151 |
+
}
|
| 1152 |
+
|
| 1153 |
+
Vectorized<c10::qint8> relu(Vectorized<c10::qint8> zero_point) const {
|
| 1154 |
+
return maximum(zero_point);
|
| 1155 |
+
}
|
| 1156 |
+
|
| 1157 |
+
Vectorized<c10::qint8> relu6(
|
| 1158 |
+
Vectorized<c10::qint8> zero_point,
|
| 1159 |
+
Vectorized<c10::qint8> q_six) {
|
| 1160 |
+
Vectorized<c10::qint8> retval;
|
| 1161 |
+
for (const auto i : c10::irange(size())) {
|
| 1162 |
+
retval.vals[i] = std::min<value_type>(
|
| 1163 |
+
std::max<value_type>(vals[i], zero_point.vals[i]), q_six.vals[i]);
|
| 1164 |
+
}
|
| 1165 |
+
return retval;
|
| 1166 |
+
}
|
| 1167 |
+
|
| 1168 |
+
int_vec_return_type widening_subtract(Vectorized<c10::qint8> b) const {
|
| 1169 |
+
int_vec_return_type retval;
|
| 1170 |
+
constexpr int elem_per_int_vec = size() / int_num_vecs();
|
| 1171 |
+
for (const auto i : c10::irange(int_num_vecs())) {
|
| 1172 |
+
for (const auto j : c10::irange(elem_per_int_vec)) {
|
| 1173 |
+
retval[i].vals[j] =
|
| 1174 |
+
static_cast<int32_t>(vals[i * elem_per_int_vec + j]) -
|
| 1175 |
+
static_cast<int32_t>(b.vals[i * elem_per_int_vec + j]);
|
| 1176 |
+
}
|
| 1177 |
+
}
|
| 1178 |
+
return retval;
|
| 1179 |
+
}
|
| 1180 |
+
static Vectorized<c10::qint8> requantize_from_int(
|
| 1181 |
+
const int_vec_return_type& inp,
|
| 1182 |
+
float multiplier,
|
| 1183 |
+
int32_t zero_point) {
|
| 1184 |
+
constexpr int elem_per_int_vec = size() / int_num_vecs();
|
| 1185 |
+
constexpr auto min_val = std::numeric_limits<value_type>::min();
|
| 1186 |
+
constexpr auto max_val = std::numeric_limits<value_type>::max();
|
| 1187 |
+
Vectorized<c10::qint8> retval;
|
| 1188 |
+
for (const auto i : c10::irange(int_num_vecs())) {
|
| 1189 |
+
for (const auto j : c10::irange(elem_per_int_vec)) {
|
| 1190 |
+
int32_t rounded =
|
| 1191 |
+
std::nearbyint(static_cast<float>(inp[i].vals[j]) * multiplier) +
|
| 1192 |
+
zero_point;
|
| 1193 |
+
retval.vals[i * elem_per_int_vec + j] =
|
| 1194 |
+
std::min<int32_t>(std::max<int32_t>(rounded, min_val), max_val);
|
| 1195 |
+
}
|
| 1196 |
+
}
|
| 1197 |
+
return retval;
|
| 1198 |
+
}
|
| 1199 |
+
};
|
| 1200 |
+
|
| 1201 |
+
template <>
|
| 1202 |
+
Vectorized<c10::qint8> inline maximum(const Vectorized<c10::qint8>& a, const Vectorized<c10::qint8>& b) {
|
| 1203 |
+
return a.maximum(b);
|
| 1204 |
+
}
|
| 1205 |
+
|
| 1206 |
+
template <>
|
| 1207 |
+
struct Vectorized<c10::quint8> : public VectorizedQuantizedConverter<
|
| 1208 |
+
c10::quint8,
|
| 1209 |
+
std::array<Vectorized<float>, 4>,
|
| 1210 |
+
std::array<Vectorized<c10::qint32>, 4>,
|
| 1211 |
+
32> {
|
| 1212 |
+
Vectorized()
|
| 1213 |
+
: VectorizedQuantizedConverter<
|
| 1214 |
+
c10::quint8,
|
| 1215 |
+
std::array<Vectorized<float>, 4>,
|
| 1216 |
+
std::array<Vectorized<c10::qint32>, 4>,
|
| 1217 |
+
32>() {}
|
| 1218 |
+
Vectorized(c10::quint8 val)
|
| 1219 |
+
: VectorizedQuantizedConverter<
|
| 1220 |
+
c10::quint8,
|
| 1221 |
+
std::array<Vectorized<float>, 4>,
|
| 1222 |
+
std::array<Vectorized<c10::qint32>, 4>,
|
| 1223 |
+
32>(val) {}
|
| 1224 |
+
Vectorized(const void* ptr)
|
| 1225 |
+
: VectorizedQuantizedConverter<
|
| 1226 |
+
c10::quint8,
|
| 1227 |
+
std::array<Vectorized<float>, 4>,
|
| 1228 |
+
std::array<Vectorized<c10::qint32>, 4>,
|
| 1229 |
+
32>(ptr) {}
|
| 1230 |
+
|
| 1231 |
+
static Vectorized<c10::quint8> loadu(const void* ptr) {
|
| 1232 |
+
return Vectorized<c10::quint8>(ptr);
|
| 1233 |
+
}
|
| 1234 |
+
|
| 1235 |
+
static Vectorized<c10::quint8> loadu(const void* ptr, int64_t count) {
|
| 1236 |
+
__at_align__ value_type tmp_values[size()];
|
| 1237 |
+
// Ensure uninitialized memory does not change the output value See https://github.com/pytorch/pytorch/issues/32502
|
| 1238 |
+
// for more details. We do not initialize arrays to zero using "={0}" because gcc would compile it to two
|
| 1239 |
+
// instructions while a loop would be compiled to one instruction.
|
| 1240 |
+
for (const auto i : c10::irange(size())) {
|
| 1241 |
+
tmp_values[i] = 0;
|
| 1242 |
+
}
|
| 1243 |
+
std::memcpy(
|
| 1244 |
+
tmp_values, reinterpret_cast<const value_type*>(ptr), count * sizeof(value_type));
|
| 1245 |
+
return Vectorized<c10::quint8>(tmp_values);
|
| 1246 |
+
}
|
| 1247 |
+
|
| 1248 |
+
static Vectorized<c10::quint8> quantize(
|
| 1249 |
+
const float_vec_return_type& rhs,
|
| 1250 |
+
float scale,
|
| 1251 |
+
int32_t zero_point,
|
| 1252 |
+
float /*inverse_scale*/) {
|
| 1253 |
+
std::array<value_type, size()> qvals;
|
| 1254 |
+
std::array<float, float_num_vecs() * 8> float_vals;
|
| 1255 |
+
|
| 1256 |
+
for (const auto i : c10::irange(float_num_vecs())) {
|
| 1257 |
+
rhs[i].store(&float_vals[i * 8], 8);
|
| 1258 |
+
}
|
| 1259 |
+
|
| 1260 |
+
at::native::quantize_vec<c10::quint8>(
|
| 1261 |
+
scale,
|
| 1262 |
+
zero_point,
|
| 1263 |
+
float_vals.data(),
|
| 1264 |
+
(c10::quint8*)qvals.data(),
|
| 1265 |
+
8 * float_num_vecs());
|
| 1266 |
+
|
| 1267 |
+
return Vectorized<c10::quint8>::loadu(qvals.data());
|
| 1268 |
+
}
|
| 1269 |
+
|
| 1270 |
+
Vectorized<c10::quint8> maximum(Vectorized<c10::quint8> b) const {
|
| 1271 |
+
Vectorized<c10::quint8> retval;
|
| 1272 |
+
for (const auto i : c10::irange(size())) {
|
| 1273 |
+
retval.vals[i] = std::max<value_type>(vals[i], b.vals[i]);
|
| 1274 |
+
}
|
| 1275 |
+
return retval;
|
| 1276 |
+
}
|
| 1277 |
+
|
| 1278 |
+
Vectorized<c10::quint8> minimum(Vectorized<c10::quint8> b) const {
|
| 1279 |
+
Vectorized<c10::quint8> retval;
|
| 1280 |
+
for (const auto i : c10::irange(size())) {
|
| 1281 |
+
retval.vals[i] = std::min<value_type>(vals[i], b.vals[i]);
|
| 1282 |
+
}
|
| 1283 |
+
return retval;
|
| 1284 |
+
}
|
| 1285 |
+
|
| 1286 |
+
Vectorized<c10::quint8> relu(Vectorized<c10::quint8> zero_point) const {
|
| 1287 |
+
return maximum(zero_point);
|
| 1288 |
+
}
|
| 1289 |
+
|
| 1290 |
+
|
| 1291 |
+
Vectorized<c10::quint8> relu6(
|
| 1292 |
+
Vectorized<c10::quint8> zero_point,
|
| 1293 |
+
Vectorized<c10::quint8> q_six) {
|
| 1294 |
+
Vectorized<c10::quint8> retval;
|
| 1295 |
+
for (const auto i : c10::irange(size())) {
|
| 1296 |
+
retval.vals[i] = std::min<value_type>(
|
| 1297 |
+
std::max<value_type>(vals[i], zero_point.vals[i]), q_six.vals[i]);
|
| 1298 |
+
}
|
| 1299 |
+
return retval;
|
| 1300 |
+
}
|
| 1301 |
+
|
| 1302 |
+
int_vec_return_type widening_subtract(Vectorized<c10::quint8> b) const {
|
| 1303 |
+
int_vec_return_type retval;
|
| 1304 |
+
constexpr int elem_per_int_vec = size() / int_num_vecs();
|
| 1305 |
+
for (const auto i : c10::irange(int_num_vecs())) {
|
| 1306 |
+
for (const auto j : c10::irange(elem_per_int_vec)) {
|
| 1307 |
+
retval[i].vals[j] =
|
| 1308 |
+
static_cast<int32_t>(vals[i * elem_per_int_vec + j]) -
|
| 1309 |
+
static_cast<int32_t>(b.vals[i * elem_per_int_vec + j]);
|
| 1310 |
+
}
|
| 1311 |
+
}
|
| 1312 |
+
return retval;
|
| 1313 |
+
}
|
| 1314 |
+
static Vectorized<c10::quint8> requantize_from_int(
|
| 1315 |
+
const int_vec_return_type& inp,
|
| 1316 |
+
float multiplier,
|
| 1317 |
+
int32_t zero_point) {
|
| 1318 |
+
constexpr int elem_per_int_vec = size() / int_num_vecs();
|
| 1319 |
+
constexpr auto min_val = std::numeric_limits<value_type>::min();
|
| 1320 |
+
constexpr auto max_val = std::numeric_limits<value_type>::max();
|
| 1321 |
+
Vectorized<c10::quint8> retval;
|
| 1322 |
+
for (const auto i : c10::irange(int_num_vecs())) {
|
| 1323 |
+
for (const auto j : c10::irange(elem_per_int_vec)) {
|
| 1324 |
+
int32_t rounded =
|
| 1325 |
+
std::nearbyint(static_cast<float>(inp[i].vals[j]) * multiplier) +
|
| 1326 |
+
zero_point;
|
| 1327 |
+
retval.vals[i * elem_per_int_vec + j] =
|
| 1328 |
+
std::min<int32_t>(std::max<int32_t>(rounded, min_val), max_val);
|
| 1329 |
+
}
|
| 1330 |
+
}
|
| 1331 |
+
return retval;
|
| 1332 |
+
}
|
| 1333 |
+
};
|
| 1334 |
+
|
| 1335 |
+
template <>
|
| 1336 |
+
Vectorized<c10::quint8> inline maximum(const Vectorized<c10::quint8>& a, const Vectorized<c10::quint8>& b) {
|
| 1337 |
+
return a.maximum(b);
|
| 1338 |
+
}
|
| 1339 |
+
|
| 1340 |
+
#endif // if defined(CPU_CAPABILITY_AVX2)
|
| 1341 |
+
|
| 1342 |
+
#if defined(CPU_CAPABILITY_NEON)
|
| 1343 |
+
template <typename T>
|
| 1344 |
+
typename std::enable_if_t<std::is_same_v<T, int8_t>, at::vec::Vectorized<float>>
|
| 1345 |
+
inline convert_int8_to_float(at::vec::Vectorized<T> src) {
|
| 1346 |
+
// Note: this function only convert inputs number of elements equal to at::vec::Vectorized<float>.size()
|
| 1347 |
+
auto s8x8 = vld1_s8(src.operator const int8_t*());
|
| 1348 |
+
auto s16x8 = vmovl_s8(s8x8);
|
| 1349 |
+
|
| 1350 |
+
auto s32x4_hi = vmovl_s16(vget_high_s16(s16x8));
|
| 1351 |
+
auto s32x4_lo = vmovl_s16(vget_low_s16(s16x8));
|
| 1352 |
+
|
| 1353 |
+
return Vectorized<float>(vcvtq_f32_s32(s32x4_lo), vcvtq_f32_s32(s32x4_hi));
|
| 1354 |
+
}
|
| 1355 |
+
|
| 1356 |
+
template <typename T>
|
| 1357 |
+
typename std::enable_if_t<std::is_same_v<T, uint8_t>, at::vec::Vectorized<float>>
|
| 1358 |
+
inline convert_int8_to_float(at::vec::Vectorized<T> src) {
|
| 1359 |
+
// Note: this function only convert inputs number of elements equal to at::vec::Vectorized<float>.size()
|
| 1360 |
+
auto u8x8 = vld1_u8(src.operator const uint8_t*());
|
| 1361 |
+
auto u16x8 = vmovl_u8(u8x8);
|
| 1362 |
+
auto u32x4_hi = vmovl_u16(vget_high_u16(u16x8));
|
| 1363 |
+
auto u32x4_lo = vmovl_u16(vget_low_u16(u16x8));
|
| 1364 |
+
|
| 1365 |
+
return Vectorized<float>(vcvtq_f32_u32(u32x4_lo), vcvtq_f32_u32(u32x4_hi));
|
| 1366 |
+
}
|
| 1367 |
+
|
| 1368 |
+
#endif
|
| 1369 |
+
}} // namespace at::vec::CPU_CAPABILITY
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_bfloat16_vsx.h
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 4 |
+
#include <ATen/cpu/vec/vec256/vsx/vsx_helpers.h>
|
| 5 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 6 |
+
#include <c10/util/irange.h>
|
| 7 |
+
|
| 8 |
+
namespace at {
|
| 9 |
+
namespace vec {
|
| 10 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 11 |
+
inline namespace CPU_CAPABILITY {
|
| 12 |
+
|
| 13 |
+
inline std::tuple<Vectorized<float>, Vectorized<float>> convert_bfloat16_float(
|
| 14 |
+
const Vectorized<BFloat16>& a) {
|
| 15 |
+
constexpr int64_t K = Vectorized<BFloat16>::size();
|
| 16 |
+
__at_align__ float arr[K];
|
| 17 |
+
__at_align__ BFloat16 arr2[K];
|
| 18 |
+
a.store(arr2);
|
| 19 |
+
convert(arr2, arr, K);
|
| 20 |
+
return std::make_tuple(
|
| 21 |
+
Vectorized<float>::loadu(arr),
|
| 22 |
+
Vectorized<float>::loadu(arr + Vectorized<float>::size()));
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
inline Vectorized<BFloat16> convert_float_bfloat16(
|
| 26 |
+
const Vectorized<float>& a,
|
| 27 |
+
const Vectorized<float>& b) {
|
| 28 |
+
constexpr int64_t K = Vectorized<BFloat16>::size();
|
| 29 |
+
__at_align__ float arr[K];
|
| 30 |
+
__at_align__ BFloat16 arr2[K];
|
| 31 |
+
a.store(arr);
|
| 32 |
+
b.store(arr + Vectorized<float>::size());
|
| 33 |
+
convert(arr, arr2, K);
|
| 34 |
+
return Vectorized<BFloat16>::loadu(arr2);
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
inline void load_fp32_from_bf16(const c10::BFloat16* data, Vectorized<float>& out) {
|
| 38 |
+
__at_align__ float values[Vectorized<float>::size()];
|
| 39 |
+
for (const auto k : c10::irange(Vectorized<float>::size())) {
|
| 40 |
+
values[k] = data[k];
|
| 41 |
+
}
|
| 42 |
+
out = Vectorized<float>::loadu(values);
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
inline void load_fp32_from_bf16(
|
| 46 |
+
const c10::BFloat16* data,
|
| 47 |
+
Vectorized<float>& out1,
|
| 48 |
+
Vectorized<float>& out2) {
|
| 49 |
+
load_fp32_from_bf16(data, out1);
|
| 50 |
+
data += Vectorized<float>::size();
|
| 51 |
+
load_fp32_from_bf16(data, out2);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
inline void load_fp32_from_fp16(const c10::Half* data, Vectorized<float>& out) {
|
| 55 |
+
__at_align__ float values[Vectorized<float>::size()];
|
| 56 |
+
for (const auto k : c10::irange(Vectorized<float>::size())) {
|
| 57 |
+
values[k] = data[k];
|
| 58 |
+
}
|
| 59 |
+
out = Vectorized<float>::loadu(values);
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
inline void load_fp32_from_fp16(
|
| 63 |
+
const c10::Half* data,
|
| 64 |
+
Vectorized<float>& out1,
|
| 65 |
+
Vectorized<float>& out2) {
|
| 66 |
+
load_fp32_from_fp16(data, out1);
|
| 67 |
+
data += Vectorized<float>::size();
|
| 68 |
+
load_fp32_from_fp16(data, out2);
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
} // namespace
|
| 72 |
+
} // namespace vec
|
| 73 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_common_vsx.h
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 4 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 5 |
+
#include <ATen/cpu/vec/vec256/vsx/vsx_helpers.h>
|
| 6 |
+
|
| 7 |
+
// Note: header order is important here
|
| 8 |
+
#include <ATen/cpu/vec/vec256/vsx/vec256_double_vsx.h>
|
| 9 |
+
#include <ATen/cpu/vec/vec256/vsx/vec256_float_vsx.h>
|
| 10 |
+
#include <ATen/cpu/vec/vec256/vsx/vec256_int16_vsx.h>
|
| 11 |
+
#include <ATen/cpu/vec/vec256/vsx/vec256_int32_vsx.h>
|
| 12 |
+
#include <ATen/cpu/vec/vec256/vsx/vec256_int64_vsx.h>
|
| 13 |
+
#include <ATen/cpu/vec/vec256/vsx/vec256_qint32_vsx.h>
|
| 14 |
+
#include <ATen/cpu/vec/vec256/vsx/vec256_qint8_vsx.h>
|
| 15 |
+
#include <ATen/cpu/vec/vec256/vsx/vec256_quint8_vsx.h>
|
| 16 |
+
|
| 17 |
+
#include <ATen/cpu/vec/vec256/vsx/vec256_complex_float_vsx.h>
|
| 18 |
+
#include <ATen/cpu/vec/vec256/vsx/vec256_complex_double_vsx.h>
|
| 19 |
+
|
| 20 |
+
#include <ATen/cpu/vec/vec256/vsx/vec256_bfloat16_vsx.h>
|
| 21 |
+
|
| 22 |
+
namespace at {
|
| 23 |
+
namespace vec {
|
| 24 |
+
|
| 25 |
+
inline namespace CPU_CAPABILITY {
|
| 26 |
+
|
| 27 |
+
DEFINE_CLAMP_FUNCS(c10::quint8)
|
| 28 |
+
DEFINE_CLAMP_FUNCS(c10::qint8)
|
| 29 |
+
DEFINE_CLAMP_FUNCS(c10::qint32)
|
| 30 |
+
DEFINE_CLAMP_FUNCS(int16_t)
|
| 31 |
+
DEFINE_CLAMP_FUNCS(int32_t)
|
| 32 |
+
DEFINE_CLAMP_FUNCS(int64_t)
|
| 33 |
+
DEFINE_CLAMP_FUNCS(float)
|
| 34 |
+
DEFINE_CLAMP_FUNCS(double)
|
| 35 |
+
|
| 36 |
+
template <>
|
| 37 |
+
Vectorized<double> C10_ALWAYS_INLINE fmadd(
|
| 38 |
+
const Vectorized<double>& a,
|
| 39 |
+
const Vectorized<double>& b,
|
| 40 |
+
const Vectorized<double>& c) {
|
| 41 |
+
return Vectorized<double>{
|
| 42 |
+
vec_madd(a.vec0(), b.vec0(), c.vec0()),
|
| 43 |
+
vec_madd(a.vec1(), b.vec1(), c.vec1())};
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
template <>
|
| 47 |
+
Vectorized<int64_t> C10_ALWAYS_INLINE fmadd(
|
| 48 |
+
const Vectorized<int64_t>& a,
|
| 49 |
+
const Vectorized<int64_t>& b,
|
| 50 |
+
const Vectorized<int64_t>& c) {
|
| 51 |
+
return Vectorized<int64_t>{
|
| 52 |
+
a.vec0() * b.vec0() + c.vec0(), a.vec1() * b.vec1() + c.vec1()};
|
| 53 |
+
}
|
| 54 |
+
template <>
|
| 55 |
+
Vectorized<int32_t> C10_ALWAYS_INLINE fmadd(
|
| 56 |
+
const Vectorized<int32_t>& a,
|
| 57 |
+
const Vectorized<int32_t>& b,
|
| 58 |
+
const Vectorized<int32_t>& c) {
|
| 59 |
+
return Vectorized<int32_t>{
|
| 60 |
+
a.vec0() * b.vec0() + c.vec0(), a.vec1() * b.vec1() + c.vec1()};
|
| 61 |
+
}
|
| 62 |
+
template <>
|
| 63 |
+
Vectorized<int16_t> C10_ALWAYS_INLINE fmadd(
|
| 64 |
+
const Vectorized<int16_t>& a,
|
| 65 |
+
const Vectorized<int16_t>& b,
|
| 66 |
+
const Vectorized<int16_t>& c) {
|
| 67 |
+
return Vectorized<int16_t>{
|
| 68 |
+
a.vec0() * b.vec0() + c.vec0(), a.vec1() * b.vec1() + c.vec1()};
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
DEFINE_REINTERPRET_CAST_TO_ALL_FUNCS(float)
|
| 72 |
+
DEFINE_REINTERPRET_CAST_TO_ALL_FUNCS(double)
|
| 73 |
+
DEFINE_REINTERPRET_CAST_TO_ALL_FUNCS(int64_t)
|
| 74 |
+
DEFINE_REINTERPRET_CAST_TO_ALL_FUNCS(int32_t)
|
| 75 |
+
DEFINE_REINTERPRET_CAST_TO_ALL_FUNCS(int16_t)
|
| 76 |
+
|
| 77 |
+
template <>
|
| 78 |
+
Vectorized<int64_t> C10_ALWAYS_INLINE
|
| 79 |
+
convert_to_int_of_same_size<double>(const Vectorized<double>& src) {
|
| 80 |
+
return Vectorized<int64_t>{vec_signed(src.vec0()), vec_signed(src.vec1())};
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
template <>
|
| 84 |
+
Vectorized<int32_t> C10_ALWAYS_INLINE
|
| 85 |
+
convert_to_int_of_same_size<float>(
|
| 86 |
+
const Vectorized<float>& src) {
|
| 87 |
+
return Vectorized<int32_t>{vec_signed(src.vec0()), vec_signed(src.vec1())};
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
template <>
|
| 91 |
+
inline void convert(const int32_t* src, float* dst, int64_t n) {
|
| 92 |
+
// int32_t and float have same size
|
| 93 |
+
int64_t i;
|
| 94 |
+
for (i = 0; i <= (n - Vectorized<float>::size()); i += Vectorized<float>::size()) {
|
| 95 |
+
const int32_t* src_a = src + i;
|
| 96 |
+
float* dst_a = dst + i;
|
| 97 |
+
vint32 input_vec0 = vec_vsx_ld(offset0, reinterpret_cast<const vint32*>(src_a));
|
| 98 |
+
vint32 input_vec1 =
|
| 99 |
+
vec_vsx_ld(offset16, reinterpret_cast<const vint32*>(src_a));
|
| 100 |
+
vfloat32 c0 = vec_float(input_vec0);
|
| 101 |
+
vfloat32 c1 = vec_float(input_vec1);
|
| 102 |
+
vec_vsx_st(c0, offset0, dst_a);
|
| 103 |
+
vec_vsx_st(c1, offset16, dst_a);
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
for (; i < n; i++) {
|
| 107 |
+
dst[i] = static_cast<float>(src[i]);
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
template <>
|
| 112 |
+
inline void convert(const int64_t* src, double* dst, int64_t n) {
|
| 113 |
+
int64_t i;
|
| 114 |
+
for (i = 0; i <= (n - Vectorized<double>::size()); i += Vectorized<double>::size()) {
|
| 115 |
+
const int64_t* src_a = src + i;
|
| 116 |
+
double* dst_a = dst + i;
|
| 117 |
+
vint64 input_vec0 =
|
| 118 |
+
vec_vsx_ld(offset0, reinterpret_cast<const vint64*>(src_a));
|
| 119 |
+
vint64 input_vec1 =
|
| 120 |
+
vec_vsx_ld(offset16, reinterpret_cast<const vint64*>(src_a));
|
| 121 |
+
vfloat64 c0 = vec_double(input_vec0);
|
| 122 |
+
vfloat64 c1 = vec_double(input_vec1);
|
| 123 |
+
vec_vsx_st(c0, offset0, reinterpret_cast<double*>(dst_a));
|
| 124 |
+
vec_vsx_st(c1, offset16, reinterpret_cast<double*>(dst_a));
|
| 125 |
+
}
|
| 126 |
+
for (; i < n; i++) {
|
| 127 |
+
dst[i] = static_cast<double>(src[i]);
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
//Generic implementation to fix compiler error
|
| 131 |
+
//TO-DO : Add optimized version for ppc64
|
| 132 |
+
inline std::tuple<Vectorized<float>, Vectorized<float>> convert_half_float(
|
| 133 |
+
const Vectorized<Half>& a) {
|
| 134 |
+
constexpr int64_t K = Vectorized<Half>::size();
|
| 135 |
+
__at_align__ float arr[K];
|
| 136 |
+
__at_align__ Half arr2[K];
|
| 137 |
+
a.store(arr2);
|
| 138 |
+
convert(arr2, arr, K);
|
| 139 |
+
return std::make_tuple(
|
| 140 |
+
Vectorized<float>::loadu(arr),
|
| 141 |
+
Vectorized<float>::loadu(arr + Vectorized<float>::size()));
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
inline Vectorized<Half> convert_float_half(
|
| 145 |
+
const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 146 |
+
constexpr int64_t K = Vectorized<Half>::size();
|
| 147 |
+
__at_align__ float arr[K];
|
| 148 |
+
__at_align__ Half arr2[K];
|
| 149 |
+
a.store(arr);
|
| 150 |
+
b.store(arr + Vectorized<float>::size());
|
| 151 |
+
convert(arr, arr2, K);
|
| 152 |
+
return Vectorized<Half>::loadu(arr2);
|
| 153 |
+
};
|
| 154 |
+
|
| 155 |
+
template <>
|
| 156 |
+
std::pair<Vectorized<double>, Vectorized<double>> inline interleave2<double>(
|
| 157 |
+
const Vectorized<double>& a,
|
| 158 |
+
const Vectorized<double>& b) {
|
| 159 |
+
// inputs:
|
| 160 |
+
// a = {a0, a1, a2, a3}
|
| 161 |
+
// b = {b0, b1, b2, b3}
|
| 162 |
+
|
| 163 |
+
vfloat64 ab00 = vec_xxpermdi(a.vec0(), b.vec0(), 0);
|
| 164 |
+
vfloat64 ab11 = vec_xxpermdi(a.vec0(), b.vec0(), 3);
|
| 165 |
+
vfloat64 ab2_00 = vec_xxpermdi(a.vec1(), b.vec1(), 0);
|
| 166 |
+
vfloat64 ab2_11 = vec_xxpermdi(a.vec1(), b.vec1(), 3);
|
| 167 |
+
// return {a0, b0, a1, b1}
|
| 168 |
+
// {a2, b2, a3, b3}
|
| 169 |
+
return std::make_pair(
|
| 170 |
+
Vectorized<double>{ab00, ab11}, Vectorized<double>{ab2_00, ab2_11});
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
template <>
|
| 174 |
+
std::pair<Vectorized<double>, Vectorized<double>> inline deinterleave2<double>(
|
| 175 |
+
const Vectorized<double>& a,
|
| 176 |
+
const Vectorized<double>& b) {
|
| 177 |
+
// inputs:
|
| 178 |
+
// a = {a0, b0, a1, b1}
|
| 179 |
+
// b = {a2, b2, a3, b3}
|
| 180 |
+
vfloat64 aa01 = vec_xxpermdi(a.vec0(), a.vec1(), 0);
|
| 181 |
+
vfloat64 aa23 = vec_xxpermdi(b.vec0(), b.vec1(), 0);
|
| 182 |
+
|
| 183 |
+
vfloat64 bb_01 = vec_xxpermdi(a.vec0(), a.vec1(), 3);
|
| 184 |
+
vfloat64 bb_23 = vec_xxpermdi(b.vec0(), b.vec1(), 3);
|
| 185 |
+
|
| 186 |
+
// swap lanes:
|
| 187 |
+
// return {a0, a1, a2, a3}
|
| 188 |
+
// {b0, b1, b2, b3}
|
| 189 |
+
return std::make_pair(
|
| 190 |
+
Vectorized<double>{aa01, aa23}, Vectorized<double>{bb_01, bb_23});
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
template <>
|
| 194 |
+
std::pair<Vectorized<float>, Vectorized<float>> inline interleave2<float>(
|
| 195 |
+
const Vectorized<float>& a,
|
| 196 |
+
const Vectorized<float>& b) {
|
| 197 |
+
// inputs:
|
| 198 |
+
// a = {a0, a1, a2, a3,, a4, a5, a6, a7}
|
| 199 |
+
// b = {b0, b1, b2, b3,, b4, b5, b6, b7}
|
| 200 |
+
|
| 201 |
+
vfloat32 ab0011 = vec_mergeh(a.vec0(), b.vec0());
|
| 202 |
+
vfloat32 ab2233 = vec_mergel(a.vec0(), b.vec0());
|
| 203 |
+
|
| 204 |
+
vfloat32 ab2_0011 = vec_mergeh(a.vec1(), b.vec1());
|
| 205 |
+
vfloat32 ab2_2233 = vec_mergel(a.vec1(), b.vec1());
|
| 206 |
+
// group cols crossing lanes:
|
| 207 |
+
// return {a0, b0, a1, b1,, a2, b2, a3, b3}
|
| 208 |
+
// {a4, b4, a5, b5,, a6, b6, a7, b7}
|
| 209 |
+
|
| 210 |
+
return std::make_pair(
|
| 211 |
+
Vectorized<float>{ab0011, ab2233}, Vectorized<float>{ab2_0011, ab2_2233});
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
template <>
|
| 215 |
+
std::pair<Vectorized<float>, Vectorized<float>> inline deinterleave2<float>(
|
| 216 |
+
const Vectorized<float>& a,
|
| 217 |
+
const Vectorized<float>& b) {
|
| 218 |
+
// inputs:
|
| 219 |
+
// a = {a0, b0, a1, b1,, a2, b2, a3, b3}
|
| 220 |
+
// b = {a4, b4, a5, b5,, a6, b6, a7, b7}
|
| 221 |
+
|
| 222 |
+
// {a0,a2,b0,b2} {a1,a3,b1,b3}
|
| 223 |
+
vfloat32 a0a2b0b2 = vec_mergeh(a.vec0(), a.vec1());
|
| 224 |
+
vfloat32 a1a3b1b3 = vec_mergel(a.vec0(), a.vec1());
|
| 225 |
+
|
| 226 |
+
vfloat32 aa0123 = vec_mergeh(a0a2b0b2, a1a3b1b3);
|
| 227 |
+
vfloat32 bb0123 = vec_mergel(a0a2b0b2, a1a3b1b3);
|
| 228 |
+
|
| 229 |
+
vfloat32 a0a2b0b2_2 = vec_mergeh(b.vec0(), b.vec1());
|
| 230 |
+
vfloat32 a1a3b1b3_2 = vec_mergel(b.vec0(), b.vec1());
|
| 231 |
+
|
| 232 |
+
vfloat32 aa0123_2 = vec_mergeh(a0a2b0b2_2, a1a3b1b3_2);
|
| 233 |
+
vfloat32 bb0123_2 = vec_mergel(a0a2b0b2_2, a1a3b1b3_2);
|
| 234 |
+
|
| 235 |
+
// it could be done with vec_perm ,too
|
| 236 |
+
// swap lanes:
|
| 237 |
+
// return {a0, a1, a2, a3,, a4, a5, a6, a7}
|
| 238 |
+
// {b0, b1, b2, b3,, b4, b5, b6, b7}
|
| 239 |
+
|
| 240 |
+
return std::make_pair(
|
| 241 |
+
Vectorized<float>{aa0123, aa0123_2}, Vectorized<float>{bb0123, bb0123_2});
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
} // namespace
|
| 245 |
+
} // namespace vec
|
| 246 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_complex_double_vsx.h
ADDED
|
@@ -0,0 +1,584 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 3 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 4 |
+
#include <ATen/cpu/vec/vec256/vsx/vsx_helpers.h>
|
| 5 |
+
#include <c10/util/complex.h>
|
| 6 |
+
#include <c10/util/irange.h>
|
| 7 |
+
|
| 8 |
+
namespace at {
|
| 9 |
+
namespace vec {
|
| 10 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 11 |
+
inline namespace CPU_CAPABILITY {
|
| 12 |
+
using ComplexDbl = c10::complex<double>;
|
| 13 |
+
|
| 14 |
+
template <>
|
| 15 |
+
class Vectorized<ComplexDbl> {
|
| 16 |
+
union {
|
| 17 |
+
struct {
|
| 18 |
+
vfloat64 _vec0;
|
| 19 |
+
vfloat64 _vec1;
|
| 20 |
+
};
|
| 21 |
+
struct {
|
| 22 |
+
vbool64 _vecb0;
|
| 23 |
+
vbool64 _vecb1;
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
+
} __attribute__((__may_alias__));
|
| 27 |
+
|
| 28 |
+
public:
|
| 29 |
+
using value_type = ComplexDbl;
|
| 30 |
+
using vec_internal_type = vfloat64;
|
| 31 |
+
using vec_internal_mask_type = vbool64;
|
| 32 |
+
using size_type = int;
|
| 33 |
+
static constexpr size_type size() {
|
| 34 |
+
return 2;
|
| 35 |
+
}
|
| 36 |
+
Vectorized() {}
|
| 37 |
+
C10_ALWAYS_INLINE Vectorized(vfloat64 v) : _vec0{v}, _vec1{v} {}
|
| 38 |
+
C10_ALWAYS_INLINE Vectorized(vbool64 vmask) : _vecb0{vmask}, _vecb1{vmask} {}
|
| 39 |
+
C10_ALWAYS_INLINE Vectorized(vfloat64 v1, vfloat64 v2) : _vec0{v1}, _vec1{v2} {}
|
| 40 |
+
C10_ALWAYS_INLINE Vectorized(vbool64 v1, vbool64 v2) : _vecb0{v1}, _vecb1{v2} {}
|
| 41 |
+
|
| 42 |
+
Vectorized(ComplexDbl val) {
|
| 43 |
+
double real_value = val.real();
|
| 44 |
+
double imag_value = val.imag();
|
| 45 |
+
_vec0 = vfloat64{real_value, imag_value};
|
| 46 |
+
_vec1 = vfloat64{real_value, imag_value};
|
| 47 |
+
}
|
| 48 |
+
Vectorized(ComplexDbl val1, ComplexDbl val2) {
|
| 49 |
+
_vec0 = vfloat64{val1.real(), val1.imag()};
|
| 50 |
+
_vec1 = vfloat64{val2.real(), val2.imag()};
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec0() const {
|
| 54 |
+
return _vec0;
|
| 55 |
+
}
|
| 56 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec1() const {
|
| 57 |
+
return _vec1;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
template <int64_t mask>
|
| 61 |
+
static std::enable_if_t<blendChoiceComplexDbl(mask) == 0, Vectorized<ComplexDbl>>
|
| 62 |
+
C10_ALWAYS_INLINE
|
| 63 |
+
blend(const Vectorized<ComplexDbl>& a, const Vectorized<ComplexDbl>& b) {
|
| 64 |
+
return a;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
template <int64_t mask>
|
| 68 |
+
static std::enable_if_t<blendChoiceComplexDbl(mask) == 1, Vectorized<ComplexDbl>>
|
| 69 |
+
C10_ALWAYS_INLINE
|
| 70 |
+
blend(const Vectorized<ComplexDbl>& a, const Vectorized<ComplexDbl>& b) {
|
| 71 |
+
return b;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
template <int64_t mask>
|
| 75 |
+
static std::enable_if_t<blendChoiceComplexDbl(mask) == 2, Vectorized<ComplexDbl>>
|
| 76 |
+
C10_ALWAYS_INLINE
|
| 77 |
+
blend(const Vectorized<ComplexDbl>& a, const Vectorized<ComplexDbl>& b) {
|
| 78 |
+
return {b._vec0, a._vec1};
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
template <int64_t mask>
|
| 82 |
+
static std::enable_if_t<blendChoiceComplexDbl(mask) == 3, Vectorized<ComplexDbl>>
|
| 83 |
+
C10_ALWAYS_INLINE
|
| 84 |
+
blend(const Vectorized<ComplexDbl>& a, const Vectorized<ComplexDbl>& b) {
|
| 85 |
+
return {a._vec0, b._vec1};
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
template <int64_t mask>
|
| 89 |
+
static Vectorized<ComplexDbl> C10_ALWAYS_INLINE
|
| 90 |
+
el_blend(const Vectorized<ComplexDbl>& a, const Vectorized<ComplexDbl>& b) {
|
| 91 |
+
const vbool64 mask_1st = VsxDblMask1(mask);
|
| 92 |
+
const vbool64 mask_2nd = VsxDblMask2(mask);
|
| 93 |
+
return {
|
| 94 |
+
(vfloat64)vec_sel(a._vec0, b._vec0, mask_1st),
|
| 95 |
+
(vfloat64)vec_sel(a._vec1, b._vec1, mask_2nd)};
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
static Vectorized<ComplexDbl> blendv(
|
| 99 |
+
const Vectorized<ComplexDbl>& a,
|
| 100 |
+
const Vectorized<ComplexDbl>& b,
|
| 101 |
+
const Vectorized<ComplexDbl>& mask) {
|
| 102 |
+
// convert std::complex<V> index mask to V index mask: xy -> xxyy
|
| 103 |
+
auto mask_complex =
|
| 104 |
+
Vectorized<ComplexDbl>(vec_splat(mask._vec0, 0), vec_splat(mask._vec1, 0));
|
| 105 |
+
return {
|
| 106 |
+
vec_sel(a._vec0, b._vec0, mask_complex._vecb0),
|
| 107 |
+
vec_sel(a._vec1, b._vec1, mask_complex._vecb1)};
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
static Vectorized<ComplexDbl> C10_ALWAYS_INLINE elwise_blendv(
|
| 111 |
+
const Vectorized<ComplexDbl>& a,
|
| 112 |
+
const Vectorized<ComplexDbl>& b,
|
| 113 |
+
const Vectorized<ComplexDbl>& mask) {
|
| 114 |
+
return {
|
| 115 |
+
vec_sel(a._vec0, b._vec0, mask._vecb0),
|
| 116 |
+
vec_sel(a._vec1, b._vec1, mask._vecb1)};
|
| 117 |
+
}
|
| 118 |
+
template <typename step_t>
|
| 119 |
+
static Vectorized<ComplexDbl> arange(
|
| 120 |
+
ComplexDbl base = 0.,
|
| 121 |
+
step_t step = static_cast<step_t>(1)) {
|
| 122 |
+
return Vectorized<ComplexDbl>(base, base + step);
|
| 123 |
+
}
|
| 124 |
+
static Vectorized<ComplexDbl> set(
|
| 125 |
+
const Vectorized<ComplexDbl>& a,
|
| 126 |
+
const Vectorized<ComplexDbl>& b,
|
| 127 |
+
int64_t count = size()) {
|
| 128 |
+
switch (count) {
|
| 129 |
+
case 0:
|
| 130 |
+
return a;
|
| 131 |
+
case 1:
|
| 132 |
+
return blend<1>(a, b);
|
| 133 |
+
}
|
| 134 |
+
return b;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
static Vectorized<value_type> C10_ALWAYS_INLINE
|
| 138 |
+
loadu(const void* ptr, int count = size()) {
|
| 139 |
+
if (count == size()) {
|
| 140 |
+
return {
|
| 141 |
+
vec_vsx_ld(offset0, reinterpret_cast<const double*>(ptr)),
|
| 142 |
+
vec_vsx_ld(offset16, reinterpret_cast<const double*>(ptr))};
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
__at_align__ value_type tmp_values[size()] = {};
|
| 146 |
+
std::memcpy(tmp_values, ptr, std::min(count, size()) * sizeof(value_type));
|
| 147 |
+
|
| 148 |
+
return {
|
| 149 |
+
vec_vsx_ld(offset0, reinterpret_cast<const double*>(tmp_values)),
|
| 150 |
+
vec_vsx_ld(offset16, reinterpret_cast<const double*>(tmp_values))};
|
| 151 |
+
}
|
| 152 |
+
void C10_ALWAYS_INLINE store(void* ptr, int count = size()) const {
|
| 153 |
+
if (count == size()) {
|
| 154 |
+
vec_vsx_st(_vec0, offset0, reinterpret_cast<double*>(ptr));
|
| 155 |
+
vec_vsx_st(_vec1, offset16, reinterpret_cast<double*>(ptr));
|
| 156 |
+
} else if (count > 0) {
|
| 157 |
+
__at_align__ value_type tmp_values[size()];
|
| 158 |
+
vec_vsx_st(_vec0, offset0, reinterpret_cast<double*>(tmp_values));
|
| 159 |
+
vec_vsx_st(_vec1, offset16, reinterpret_cast<double*>(tmp_values));
|
| 160 |
+
std::memcpy(
|
| 161 |
+
ptr, tmp_values, std::min(count, size()) * sizeof(value_type));
|
| 162 |
+
}
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
const ComplexDbl& operator[](int idx) const = delete;
|
| 166 |
+
ComplexDbl& operator[](int idx) = delete;
|
| 167 |
+
|
| 168 |
+
Vectorized<ComplexDbl> map(ComplexDbl (*const f)(ComplexDbl)) const {
|
| 169 |
+
__at_align__ ComplexDbl tmp[size()];
|
| 170 |
+
store(tmp);
|
| 171 |
+
for (const auto i : c10::irange(size())) {
|
| 172 |
+
tmp[i] = f(tmp[i]);
|
| 173 |
+
}
|
| 174 |
+
return loadu(tmp);
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
Vectorized<ComplexDbl> map(ComplexDbl (*const f)(const ComplexDbl&)) const {
|
| 178 |
+
__at_align__ ComplexDbl tmp[size()];
|
| 179 |
+
store(tmp);
|
| 180 |
+
for (const auto i : c10::irange(size())) {
|
| 181 |
+
tmp[i] = f(tmp[i]);
|
| 182 |
+
}
|
| 183 |
+
return loadu(tmp);
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
Vectorized<ComplexDbl> el_swapped() const {
|
| 187 |
+
vfloat64 v0 = vec_xxpermdi(_vec0, _vec0, 2);
|
| 188 |
+
vfloat64 v1 = vec_xxpermdi(_vec1, _vec1, 2);
|
| 189 |
+
return {v0, v1};
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
Vectorized<ComplexDbl> el_madd(
|
| 193 |
+
const Vectorized<ComplexDbl>& multiplier,
|
| 194 |
+
const Vectorized<ComplexDbl>& val) const {
|
| 195 |
+
return {
|
| 196 |
+
vec_madd(_vec0, multiplier._vec0, val._vec0),
|
| 197 |
+
vec_madd(_vec1, multiplier._vec1, val._vec1)};
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
Vectorized<ComplexDbl> el_mergeo() const {
|
| 201 |
+
vfloat64 v0 = vec_splat(_vec0, 1);
|
| 202 |
+
vfloat64 v1 = vec_splat(_vec1, 1);
|
| 203 |
+
return {v0, v1};
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
Vectorized<ComplexDbl> el_mergee() const {
|
| 207 |
+
vfloat64 v0 = vec_splat(_vec0, 0);
|
| 208 |
+
vfloat64 v1 = vec_splat(_vec1, 0);
|
| 209 |
+
return {v0, v1};
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
static Vectorized<ComplexDbl> el_mergee(
|
| 213 |
+
Vectorized<ComplexDbl>& first,
|
| 214 |
+
Vectorized<ComplexDbl>& second) {
|
| 215 |
+
return {
|
| 216 |
+
vec_mergeh(first._vec0, second._vec0),
|
| 217 |
+
vec_mergeh(first._vec1, second._vec1)};
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
static Vectorized<ComplexDbl> el_mergeo(
|
| 221 |
+
Vectorized<ComplexDbl>& first,
|
| 222 |
+
Vectorized<ComplexDbl>& second) {
|
| 223 |
+
return {
|
| 224 |
+
vec_mergel(first._vec0, second._vec0),
|
| 225 |
+
vec_mergel(first._vec1, second._vec1)};
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
Vectorized<ComplexDbl> abs_2_() const {
|
| 229 |
+
auto a = (*this).elwise_mult(*this);
|
| 230 |
+
auto permuted = a.el_swapped();
|
| 231 |
+
a = a + permuted;
|
| 232 |
+
return a;
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
Vectorized<ComplexDbl> abs_() const {
|
| 236 |
+
auto vi = el_mergeo();
|
| 237 |
+
auto vr = el_mergee();
|
| 238 |
+
return {Sleef_hypotd2_u05vsx(vr._vec0, vi._vec0), Sleef_hypotd2_u05vsx(vr._vec1, vi._vec1)};
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
Vectorized<ComplexDbl> abs() const {
|
| 242 |
+
return abs_() & vd_real_mask;
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
Vectorized<ComplexDbl> angle_() const {
|
| 246 |
+
// angle = atan2(b/a)
|
| 247 |
+
// auto b_a = _mm256_permute_pd(values, 0x05); // b a
|
| 248 |
+
// return Sleef_atan2d4_u10(values, b_a); // 90-angle angle
|
| 249 |
+
Vectorized<ComplexDbl> ret;
|
| 250 |
+
ret._vec0[0] = std::atan2(_vec0[1], _vec0[0]);
|
| 251 |
+
ret._vec1[0] = std::atan2(_vec1[1], _vec1[0]);
|
| 252 |
+
return ret;
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
Vectorized<ComplexDbl> angle() const {
|
| 256 |
+
return angle_() & vd_real_mask;
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
Vectorized<ComplexDbl> real_() const {
|
| 260 |
+
return *this & vd_real_mask;
|
| 261 |
+
}
|
| 262 |
+
Vectorized<ComplexDbl> real() const {
|
| 263 |
+
return *this & vd_real_mask;
|
| 264 |
+
}
|
| 265 |
+
Vectorized<ComplexDbl> imag_() const {
|
| 266 |
+
return *this & vd_imag_mask;
|
| 267 |
+
}
|
| 268 |
+
Vectorized<ComplexDbl> imag() const {
|
| 269 |
+
return imag_().el_swapped();
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
Vectorized<ComplexDbl> conj_() const {
|
| 273 |
+
return *this ^ vd_isign_mask;
|
| 274 |
+
}
|
| 275 |
+
Vectorized<ComplexDbl> conj() const {
|
| 276 |
+
return *this ^ vd_isign_mask;
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
Vectorized<ComplexDbl> log() const {
|
| 280 |
+
// Most trigonomic ops use the log() op to improve complex number
|
| 281 |
+
// performance.
|
| 282 |
+
return map(std::log);
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
Vectorized<ComplexDbl> log2() const {
|
| 286 |
+
// log2eB_inv
|
| 287 |
+
auto ret = log();
|
| 288 |
+
return ret.elwise_mult(vd_log2e_inv);
|
| 289 |
+
}
|
| 290 |
+
Vectorized<ComplexDbl> log10() const {
|
| 291 |
+
auto ret = log();
|
| 292 |
+
return ret.elwise_mult(vd_log10e_inv);
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
Vectorized<ComplexDbl> log1p() const {
|
| 296 |
+
return map(std::log1p);
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
Vectorized<ComplexDbl> asin() const {
|
| 300 |
+
// asin(x)
|
| 301 |
+
// = -i*ln(iz + sqrt(1 -z^2))
|
| 302 |
+
// = -i*ln((ai - b) + sqrt(1 - (a + bi)*(a + bi)))
|
| 303 |
+
// = -i*ln((-b + ai) + sqrt(1 - (a**2 - b**2) - 2*abi))
|
| 304 |
+
auto conj = conj_();
|
| 305 |
+
auto b_a = conj.el_swapped();
|
| 306 |
+
auto ab = conj.elwise_mult(b_a);
|
| 307 |
+
auto im = ab + ab;
|
| 308 |
+
auto val_2 = (*this).elwise_mult(*this);
|
| 309 |
+
auto val_2_swapped = val_2.el_swapped();
|
| 310 |
+
auto re = horizontal_sub(val_2, val_2_swapped);
|
| 311 |
+
re = Vectorized<ComplexDbl>(vd_one) - re;
|
| 312 |
+
auto root = el_blend<0x0A>(re, im).sqrt();
|
| 313 |
+
auto ln = (b_a + root).log();
|
| 314 |
+
return ln.el_swapped().conj();
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
Vectorized<ComplexDbl> acos() const {
|
| 318 |
+
// acos(x) = pi/2 - asin(x)
|
| 319 |
+
return Vectorized(vd_pi_2) - asin();
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
Vectorized<ComplexDbl> atan() const {
|
| 323 |
+
// atan(x) = i/2 * ln((i + z)/(i - z))
|
| 324 |
+
auto ione = Vectorized(vd_imag_one);
|
| 325 |
+
auto sum = ione + *this;
|
| 326 |
+
auto sub = ione - *this;
|
| 327 |
+
auto ln = (sum / sub).log(); // ln((i + z)/(i - z))
|
| 328 |
+
return ln * vd_imag_half; // i/2*ln()
|
| 329 |
+
}
|
| 330 |
+
Vectorized<ComplexDbl> atanh() const {
|
| 331 |
+
return map(std::atanh);
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
Vectorized<ComplexDbl> sin() const {
|
| 335 |
+
return map(std::sin);
|
| 336 |
+
}
|
| 337 |
+
Vectorized<ComplexDbl> sinh() const {
|
| 338 |
+
return map(std::sinh);
|
| 339 |
+
}
|
| 340 |
+
Vectorized<ComplexDbl> cos() const {
|
| 341 |
+
return map(std::cos);
|
| 342 |
+
}
|
| 343 |
+
Vectorized<ComplexDbl> cosh() const {
|
| 344 |
+
return map(std::cosh);
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
Vectorized<ComplexDbl> tan() const {
|
| 348 |
+
return map(std::tan);
|
| 349 |
+
}
|
| 350 |
+
Vectorized<ComplexDbl> tanh() const {
|
| 351 |
+
return map(std::tanh);
|
| 352 |
+
}
|
| 353 |
+
Vectorized<ComplexDbl> ceil() const {
|
| 354 |
+
return {vec_ceil(_vec0), vec_ceil(_vec1)};
|
| 355 |
+
}
|
| 356 |
+
Vectorized<ComplexDbl> floor() const {
|
| 357 |
+
return {vec_floor(_vec0), vec_floor(_vec1)};
|
| 358 |
+
}
|
| 359 |
+
Vectorized<ComplexDbl> neg() const {
|
| 360 |
+
auto z = Vectorized<ComplexDbl>(vd_zero);
|
| 361 |
+
return z - *this;
|
| 362 |
+
}
|
| 363 |
+
Vectorized<ComplexDbl> round() const {
|
| 364 |
+
return {vec_rint(_vec0), vec_rint(_vec1)};
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
Vectorized<ComplexDbl> trunc() const {
|
| 368 |
+
return {vec_trunc(_vec0), vec_trunc(_vec1)};
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
Vectorized<ComplexDbl> elwise_sqrt() const {
|
| 372 |
+
return {vec_sqrt(_vec0), vec_sqrt(_vec1)};
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
Vectorized<ComplexDbl> sqrt() const {
|
| 376 |
+
return map(std::sqrt);
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
Vectorized<ComplexDbl> reciprocal() const {
|
| 380 |
+
// re + im*i = (a + bi) / (c + di)
|
| 381 |
+
// re = (ac + bd)/abs_2() = c/abs_2()
|
| 382 |
+
// im = (bc - ad)/abs_2() = d/abs_2()
|
| 383 |
+
auto c_d = *this ^ vd_isign_mask; // c -d
|
| 384 |
+
auto abs = abs_2_();
|
| 385 |
+
return c_d.elwise_div(abs);
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
Vectorized<ComplexDbl> rsqrt() const {
|
| 389 |
+
return sqrt().reciprocal();
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
static Vectorized<ComplexDbl> horizontal_add(
|
| 393 |
+
Vectorized<ComplexDbl>& first,
|
| 394 |
+
Vectorized<ComplexDbl>& second) {
|
| 395 |
+
// Operates on individual floats, see _mm_hadd_ps
|
| 396 |
+
// {f0+f1, s0+s1, f2+f3, s2+s3, ...}
|
| 397 |
+
// i.e. it sums the re and im of each value and interleaves first and second:
|
| 398 |
+
// {f_re0 + f_im0, s_re0 + s_im0, f_re1 + f_im1, s_re1 + s_im1, ...}
|
| 399 |
+
return el_mergee(first, second) + el_mergeo(first, second);
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
static Vectorized<ComplexDbl> horizontal_sub(
|
| 403 |
+
Vectorized<ComplexDbl>& first,
|
| 404 |
+
Vectorized<ComplexDbl>& second) {
|
| 405 |
+
// we will simulate it differently with 6 instructions total
|
| 406 |
+
// lets permute second so that we can add it getting horizontal sums
|
| 407 |
+
auto first_perm = first.el_swapped(); // 2perm
|
| 408 |
+
auto second_perm = second.el_swapped(); // 2perm
|
| 409 |
+
// summ
|
| 410 |
+
auto first_ret = first - first_perm; // 2sub
|
| 411 |
+
auto second_ret = second - second_perm; // 2 sub
|
| 412 |
+
// now lets choose evens
|
| 413 |
+
return el_mergee(first_ret, second_ret); // 2 mergee's
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
Vectorized<ComplexDbl> inline operator*(const Vectorized<ComplexDbl>& b) const {
|
| 417 |
+
//(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
|
| 418 |
+
#if 1
|
| 419 |
+
// this is more vsx friendly than simulating horizontal from x86
|
| 420 |
+
auto vi = b.el_mergeo();
|
| 421 |
+
auto vr = b.el_mergee();
|
| 422 |
+
vi = vi ^ vd_rsign_mask;
|
| 423 |
+
auto ret = elwise_mult(vr);
|
| 424 |
+
auto vx_swapped = el_swapped();
|
| 425 |
+
ret = vx_swapped.el_madd(vi, ret);
|
| 426 |
+
#else
|
| 427 |
+
auto ac_bd = elwise_mult(b);
|
| 428 |
+
auto d_c = b.el_swapped();
|
| 429 |
+
d_c = d_c ^ vd_isign_mask;
|
| 430 |
+
auto ad_bc = elwise_mult(d_c);
|
| 431 |
+
auto ret = horizontal_sub(ac_bd, ad_bc);
|
| 432 |
+
#endif
|
| 433 |
+
return ret;
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
Vectorized<ComplexDbl> inline operator/(const Vectorized<ComplexDbl>& b) const {
|
| 437 |
+
// re + im*i = (a + bi) / (c + di)
|
| 438 |
+
// re = (ac + bd)/abs_2()
|
| 439 |
+
// im = (bc - ad)/abs_2()
|
| 440 |
+
auto fabs_cd = Vectorized{
|
| 441 |
+
vec_andc(b._vec0, vd_sign_mask),
|
| 442 |
+
vec_andc(b._vec1, vd_sign_mask)}; // |c| |d|
|
| 443 |
+
auto fabs_dc = fabs_cd.el_swapped(); // |d| |c|
|
| 444 |
+
auto scale = fabs_cd.elwise_max(fabs_dc); // sc = max(|c|, |d|)
|
| 445 |
+
auto a2 = elwise_div(scale); // a/sc b/sc
|
| 446 |
+
auto b2 = b.elwise_div(scale); // c/sc d/sc
|
| 447 |
+
auto acbd2 = a2.elwise_mult(b2); // ac/sc^2 bd/sc^2
|
| 448 |
+
auto dc2 = b2.el_swapped(); // d/sc c/sc
|
| 449 |
+
dc2 = dc2 ^ vd_rsign_mask; // -d/sc c/sc
|
| 450 |
+
auto adbc2 = a2.elwise_mult(dc2); // -ad/sc^2 bc/sc^2
|
| 451 |
+
auto ret = horizontal_add(acbd2, adbc2); // (ac+bd)/sc^2 (bc-ad)/sc^2
|
| 452 |
+
auto denom2 = b2.abs_2_(); // (c^2+d^2)/sc^2 (c^2+d^2)/sc^2
|
| 453 |
+
ret = ret.elwise_div(denom2);
|
| 454 |
+
return ret;
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
Vectorized<ComplexDbl> exp() const {
|
| 458 |
+
return map(std::exp);
|
| 459 |
+
}
|
| 460 |
+
Vectorized<ComplexDbl> exp2() const {
|
| 461 |
+
return map(exp2_impl);
|
| 462 |
+
}
|
| 463 |
+
Vectorized<ComplexDbl> expm1() const {
|
| 464 |
+
return map(std::expm1);
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
Vectorized<ComplexDbl> pow(const Vectorized<ComplexDbl>& exp) const {
|
| 468 |
+
__at_align__ ComplexDbl x_tmp[size()];
|
| 469 |
+
__at_align__ ComplexDbl y_tmp[size()];
|
| 470 |
+
store(x_tmp);
|
| 471 |
+
exp.store(y_tmp);
|
| 472 |
+
for (const auto i : c10::irange(size())) {
|
| 473 |
+
x_tmp[i] = std::pow(x_tmp[i], y_tmp[i]);
|
| 474 |
+
}
|
| 475 |
+
return loadu(x_tmp);
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
Vectorized<ComplexDbl> sgn() const {
|
| 479 |
+
return map(at::native::sgn_impl);
|
| 480 |
+
}
|
| 481 |
+
|
| 482 |
+
Vectorized<ComplexDbl> operator<(const Vectorized<ComplexDbl>& other) const {
|
| 483 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 484 |
+
}
|
| 485 |
+
Vectorized<ComplexDbl> operator<=(const Vectorized<ComplexDbl>& other) const {
|
| 486 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 487 |
+
}
|
| 488 |
+
Vectorized<ComplexDbl> operator>(const Vectorized<ComplexDbl>& other) const {
|
| 489 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 490 |
+
}
|
| 491 |
+
Vectorized<ComplexDbl> operator>=(const Vectorized<ComplexDbl>& other) const {
|
| 492 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 493 |
+
}
|
| 494 |
+
|
| 495 |
+
Vectorized<ComplexDbl> eq(const Vectorized<ComplexDbl>& other) const {
|
| 496 |
+
auto eq = (*this == other); // compares real and imag individually
|
| 497 |
+
// If both real numbers and imag numbers are equal, then the complex numbers are equal
|
| 498 |
+
return (eq.real() & eq.imag()) & vd_one;
|
| 499 |
+
}
|
| 500 |
+
Vectorized<ComplexDbl> ne(const Vectorized<ComplexDbl>& other) const {
|
| 501 |
+
auto ne = (*this != other); // compares real and imag individually
|
| 502 |
+
// If either real numbers or imag numbers are not equal, then the complex numbers are not equal
|
| 503 |
+
return (ne.real() | ne.imag()) & vd_one;
|
| 504 |
+
}
|
| 505 |
+
|
| 506 |
+
DEFINE_MEMBER_OP(operator==, ComplexDbl, vec_cmpeq)
|
| 507 |
+
DEFINE_MEMBER_OP(operator!=, ComplexDbl, vec_cmpne)
|
| 508 |
+
|
| 509 |
+
DEFINE_MEMBER_OP(operator+, ComplexDbl, vec_add)
|
| 510 |
+
DEFINE_MEMBER_OP(operator-, ComplexDbl, vec_sub)
|
| 511 |
+
DEFINE_MEMBER_OP(operator&, ComplexDbl, vec_and)
|
| 512 |
+
DEFINE_MEMBER_OP(operator|, ComplexDbl, vec_or)
|
| 513 |
+
DEFINE_MEMBER_OP(operator^, ComplexDbl, vec_xor)
|
| 514 |
+
// elementwise helpers
|
| 515 |
+
DEFINE_MEMBER_OP(elwise_mult, ComplexDbl, vec_mul)
|
| 516 |
+
DEFINE_MEMBER_OP(elwise_div, ComplexDbl, vec_div)
|
| 517 |
+
DEFINE_MEMBER_OP(elwise_gt, ComplexDbl, vec_cmpgt)
|
| 518 |
+
DEFINE_MEMBER_OP(elwise_ge, ComplexDbl, vec_cmpge)
|
| 519 |
+
DEFINE_MEMBER_OP(elwise_lt, ComplexDbl, vec_cmplt)
|
| 520 |
+
DEFINE_MEMBER_OP(elwise_le, ComplexDbl, vec_cmple)
|
| 521 |
+
DEFINE_MEMBER_OP(elwise_max, ComplexDbl, vec_max)
|
| 522 |
+
};
|
| 523 |
+
|
| 524 |
+
template <>
|
| 525 |
+
Vectorized<ComplexDbl> inline maximum(
|
| 526 |
+
const Vectorized<ComplexDbl>& a,
|
| 527 |
+
const Vectorized<ComplexDbl>& b) {
|
| 528 |
+
auto abs_a = a.abs_2_();
|
| 529 |
+
auto abs_b = b.abs_2_();
|
| 530 |
+
// auto mask = _mm256_cmp_ps(abs_a, abs_b, _CMP_LT_OQ);
|
| 531 |
+
// auto max = _mm256_blendv_ps(a, b, mask);
|
| 532 |
+
auto mask = abs_a.elwise_lt(abs_b);
|
| 533 |
+
auto max = Vectorized<ComplexDbl>::elwise_blendv(a, b, mask);
|
| 534 |
+
|
| 535 |
+
return max;
|
| 536 |
+
// Exploit the fact that all-ones is a NaN.
|
| 537 |
+
// auto isnan = _mm256_cmp_ps(abs_a, abs_b, _CMP_UNORD_Q);
|
| 538 |
+
// return _mm256_or_ps(max, isnan);
|
| 539 |
+
}
|
| 540 |
+
|
| 541 |
+
template <>
|
| 542 |
+
Vectorized<ComplexDbl> inline minimum(
|
| 543 |
+
const Vectorized<ComplexDbl>& a,
|
| 544 |
+
const Vectorized<ComplexDbl>& b) {
|
| 545 |
+
auto abs_a = a.abs_2_();
|
| 546 |
+
auto abs_b = b.abs_2_();
|
| 547 |
+
// auto mask = _mm256_cmp_ps(abs_a, abs_b, _CMP_GT_OQ);
|
| 548 |
+
// auto min = _mm256_blendv_ps(a, b, mask);
|
| 549 |
+
auto mask = abs_a.elwise_gt(abs_b);
|
| 550 |
+
auto min = Vectorized<ComplexDbl>::elwise_blendv(a, b, mask);
|
| 551 |
+
return min;
|
| 552 |
+
// Exploit the fact that all-ones is a NaN.
|
| 553 |
+
// auto isnan = _mm256_cmp_ps(abs_a, abs_b, _CMP_UNORD_Q);
|
| 554 |
+
// return _mm256_or_ps(min, isnan);
|
| 555 |
+
}
|
| 556 |
+
|
| 557 |
+
template <>
|
| 558 |
+
Vectorized<ComplexDbl> C10_ALWAYS_INLINE operator+(const Vectorized<ComplexDbl>& a, const Vectorized<ComplexDbl>& b) {
|
| 559 |
+
return Vectorized<ComplexDbl>{vec_add(a.vec0(), b.vec0()), vec_add(a.vec1(), b.vec1())};
|
| 560 |
+
}
|
| 561 |
+
|
| 562 |
+
template <>
|
| 563 |
+
Vectorized<ComplexDbl> C10_ALWAYS_INLINE operator-(const Vectorized<ComplexDbl>& a, const Vectorized<ComplexDbl>& b) {
|
| 564 |
+
return Vectorized<ComplexDbl>{vec_sub(a.vec0(), b.vec0()), vec_sub(a.vec1(), b.vec1())};
|
| 565 |
+
}
|
| 566 |
+
|
| 567 |
+
template <>
|
| 568 |
+
Vectorized<ComplexDbl> C10_ALWAYS_INLINE operator&(const Vectorized<ComplexDbl>& a, const Vectorized<ComplexDbl>& b) {
|
| 569 |
+
return Vectorized<ComplexDbl>{vec_and(a.vec0(), b.vec0()), vec_and(a.vec1(), b.vec1())};
|
| 570 |
+
}
|
| 571 |
+
|
| 572 |
+
template <>
|
| 573 |
+
Vectorized<ComplexDbl> C10_ALWAYS_INLINE operator|(const Vectorized<ComplexDbl>& a, const Vectorized<ComplexDbl>& b) {
|
| 574 |
+
return Vectorized<ComplexDbl>{vec_or(a.vec0(), b.vec0()), vec_or(a.vec1(), b.vec1())};
|
| 575 |
+
}
|
| 576 |
+
|
| 577 |
+
template <>
|
| 578 |
+
Vectorized<ComplexDbl> C10_ALWAYS_INLINE operator^(const Vectorized<ComplexDbl>& a, const Vectorized<ComplexDbl>& b) {
|
| 579 |
+
return Vectorized<ComplexDbl>{vec_xor(a.vec0(), b.vec0()), vec_xor(a.vec1(), b.vec1())};
|
| 580 |
+
}
|
| 581 |
+
|
| 582 |
+
} // namespace
|
| 583 |
+
} // namespace vec
|
| 584 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_complex_float_vsx.h
ADDED
|
@@ -0,0 +1,660 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 4 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 5 |
+
#include <ATen/cpu/vec/vec256/vsx/vsx_helpers.h>
|
| 6 |
+
#include <c10/util/complex.h>
|
| 7 |
+
#include <c10/util/irange.h>
|
| 8 |
+
|
| 9 |
+
namespace at {
|
| 10 |
+
namespace vec {
|
| 11 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 12 |
+
inline namespace CPU_CAPABILITY {
|
| 13 |
+
using ComplexFlt = c10::complex<float>;
|
| 14 |
+
|
| 15 |
+
template <>
|
| 16 |
+
class Vectorized<ComplexFlt> {
|
| 17 |
+
private:
|
| 18 |
+
union {
|
| 19 |
+
struct {
|
| 20 |
+
vfloat32 _vec0;
|
| 21 |
+
vfloat32 _vec1;
|
| 22 |
+
};
|
| 23 |
+
struct {
|
| 24 |
+
vbool32 _vecb0;
|
| 25 |
+
vbool32 _vecb1;
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
} __attribute__((__may_alias__));
|
| 29 |
+
|
| 30 |
+
public:
|
| 31 |
+
using value_type = ComplexFlt;
|
| 32 |
+
using vec_internal_type = vfloat32;
|
| 33 |
+
using vec_internal_mask_type = vbool32;
|
| 34 |
+
using size_type = int;
|
| 35 |
+
|
| 36 |
+
static constexpr size_type size() {
|
| 37 |
+
return 4;
|
| 38 |
+
}
|
| 39 |
+
Vectorized() {}
|
| 40 |
+
|
| 41 |
+
C10_ALWAYS_INLINE Vectorized(vfloat32 v) : _vec0{v}, _vec1{v} {}
|
| 42 |
+
C10_ALWAYS_INLINE Vectorized(vbool32 vmask) : _vecb0{vmask}, _vecb1{vmask} {}
|
| 43 |
+
C10_ALWAYS_INLINE Vectorized(vfloat32 v1, vfloat32 v2) : _vec0{v1}, _vec1{v2} {}
|
| 44 |
+
C10_ALWAYS_INLINE Vectorized(vbool32 v1, vbool32 v2) : _vecb0{v1}, _vecb1{v2} {}
|
| 45 |
+
|
| 46 |
+
Vectorized(ComplexFlt val) {
|
| 47 |
+
float real_value = val.real();
|
| 48 |
+
float imag_value = val.imag();
|
| 49 |
+
_vec0 = vfloat32{real_value, imag_value, real_value, imag_value};
|
| 50 |
+
_vec1 = vfloat32{real_value, imag_value, real_value, imag_value};
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
Vectorized(ComplexFlt val1, ComplexFlt val2, ComplexFlt val3, ComplexFlt val4) {
|
| 54 |
+
_vec0 = vfloat32{val1.real(), val1.imag(), val2.real(), val2.imag()};
|
| 55 |
+
_vec1 = vfloat32{val3.real(), val3.imag(), val4.real(), val4.imag()};
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec0() const {
|
| 59 |
+
return _vec0;
|
| 60 |
+
}
|
| 61 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec1() const {
|
| 62 |
+
return _vec1;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
template <uint64_t mask>
|
| 66 |
+
static std::enable_if_t<blendChoiceComplex(mask) == 0, Vectorized<ComplexFlt>>
|
| 67 |
+
C10_ALWAYS_INLINE
|
| 68 |
+
blend(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 69 |
+
return a;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
template <uint64_t mask>
|
| 73 |
+
static std::enable_if_t<blendChoiceComplex(mask) == 1, Vectorized<ComplexFlt>>
|
| 74 |
+
C10_ALWAYS_INLINE
|
| 75 |
+
blend(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 76 |
+
return b;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
template <uint64_t mask>
|
| 80 |
+
static std::enable_if_t<blendChoiceComplex(mask) == 2, Vectorized<ComplexFlt>>
|
| 81 |
+
C10_ALWAYS_INLINE
|
| 82 |
+
blend(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 83 |
+
return {b._vec0, a._vec1};
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
template <uint64_t mask>
|
| 87 |
+
static std::enable_if_t<blendChoiceComplex(mask) == 3, Vectorized<ComplexFlt>>
|
| 88 |
+
C10_ALWAYS_INLINE
|
| 89 |
+
blend(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 90 |
+
return {a._vec0, b._vec1};
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
template <uint64_t mask>
|
| 94 |
+
static std::enable_if_t<blendChoiceComplex(mask) == 4, Vectorized<ComplexFlt>>
|
| 95 |
+
C10_ALWAYS_INLINE
|
| 96 |
+
blend(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 97 |
+
const vbool32 mask_1st = VsxComplexMask1(mask);
|
| 98 |
+
return {(vfloat32)vec_sel(a._vec0, b._vec0, mask_1st), a._vec1};
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
template <uint64_t mask>
|
| 102 |
+
static std::enable_if_t<blendChoiceComplex(mask) == 5, Vectorized<ComplexFlt>>
|
| 103 |
+
C10_ALWAYS_INLINE
|
| 104 |
+
blend(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 105 |
+
const vbool32 mask_1st = VsxComplexMask1(mask);
|
| 106 |
+
return {(vfloat32)vec_sel(a._vec0, b._vec0, mask_1st), b._vec1};
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
template <uint64_t mask>
|
| 110 |
+
static std::enable_if_t<blendChoiceComplex(mask) == 6, Vectorized<ComplexFlt>>
|
| 111 |
+
C10_ALWAYS_INLINE
|
| 112 |
+
blend(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 113 |
+
const vbool32 mask_2nd = VsxComplexMask2(mask);
|
| 114 |
+
// generated masks
|
| 115 |
+
return {a._vec0, (vfloat32)vec_sel(a._vec1, b._vec1, mask_2nd)};
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
template <uint64_t mask>
|
| 119 |
+
static std::enable_if_t<blendChoiceComplex(mask) == 7, Vectorized<ComplexFlt>>
|
| 120 |
+
C10_ALWAYS_INLINE
|
| 121 |
+
blend(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 122 |
+
const vbool32 mask_2nd = VsxComplexMask2(mask);
|
| 123 |
+
// generated masks
|
| 124 |
+
return {b._vec0, (vfloat32)vec_sel(a._vec1, b._vec1, mask_2nd)};
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
template <uint64_t mask>
|
| 128 |
+
static std::enable_if_t<blendChoiceComplex(mask) == 8, Vectorized<ComplexFlt>>
|
| 129 |
+
C10_ALWAYS_INLINE
|
| 130 |
+
blend(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 131 |
+
const vbool32 mask_1st = VsxComplexMask1(mask);
|
| 132 |
+
const vbool32 mask_2nd = VsxComplexMask2(mask);
|
| 133 |
+
return {
|
| 134 |
+
(vfloat32)vec_sel(a._vec0, b._vec0, mask_1st),
|
| 135 |
+
(vfloat32)vec_sel(a._vec1, b._vec1, mask_2nd)};
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
template <int64_t mask>
|
| 139 |
+
static Vectorized<ComplexFlt> C10_ALWAYS_INLINE
|
| 140 |
+
el_blend(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 141 |
+
const vbool32 mask_1st = VsxMask1(mask);
|
| 142 |
+
const vbool32 mask_2nd = VsxMask2(mask);
|
| 143 |
+
return {
|
| 144 |
+
(vfloat32)vec_sel(a._vec0, b._vec0, mask_1st),
|
| 145 |
+
(vfloat32)vec_sel(a._vec1, b._vec1, mask_2nd)};
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
static Vectorized<ComplexFlt> blendv(
|
| 149 |
+
const Vectorized<ComplexFlt>& a,
|
| 150 |
+
const Vectorized<ComplexFlt>& b,
|
| 151 |
+
const Vectorized<ComplexFlt>& mask) {
|
| 152 |
+
// convert std::complex<V> index mask to V index mask: xy -> xxyy
|
| 153 |
+
auto mask_complex = Vectorized<ComplexFlt>(
|
| 154 |
+
vec_mergeh(mask._vec0, mask._vec0), vec_mergeh(mask._vec1, mask._vec1));
|
| 155 |
+
return {
|
| 156 |
+
vec_sel(a._vec0, b._vec0, reinterpret_cast<vbool32>(mask_complex._vec0)),
|
| 157 |
+
vec_sel(a._vec1, b._vec1, reinterpret_cast<vbool32>(mask_complex._vec1)),
|
| 158 |
+
};
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
static Vectorized<ComplexFlt> elwise_blendv(
|
| 162 |
+
const Vectorized<ComplexFlt>& a,
|
| 163 |
+
const Vectorized<ComplexFlt>& b,
|
| 164 |
+
const Vectorized<ComplexFlt>& mask) {
|
| 165 |
+
return {
|
| 166 |
+
vec_sel(a._vec0, b._vec0, reinterpret_cast<vbool32>(mask._vec0)),
|
| 167 |
+
vec_sel(a._vec1, b._vec1, reinterpret_cast<vbool32>(mask._vec1)),
|
| 168 |
+
};
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
template <typename step_t>
|
| 172 |
+
static Vectorized<ComplexFlt> arange(
|
| 173 |
+
ComplexFlt base = 0.,
|
| 174 |
+
step_t step = static_cast<step_t>(1)) {
|
| 175 |
+
return Vectorized<ComplexFlt>(
|
| 176 |
+
base,
|
| 177 |
+
base + step,
|
| 178 |
+
base + ComplexFlt(2) * step,
|
| 179 |
+
base + ComplexFlt(3) * step);
|
| 180 |
+
}
|
| 181 |
+
static Vectorized<ComplexFlt> set(
|
| 182 |
+
const Vectorized<ComplexFlt>& a,
|
| 183 |
+
const Vectorized<ComplexFlt>& b,
|
| 184 |
+
int64_t count = size()) {
|
| 185 |
+
switch (count) {
|
| 186 |
+
case 0:
|
| 187 |
+
return a;
|
| 188 |
+
case 1:
|
| 189 |
+
return blend<1>(a, b);
|
| 190 |
+
case 2:
|
| 191 |
+
return blend<3>(a, b);
|
| 192 |
+
case 3:
|
| 193 |
+
return blend<7>(a, b);
|
| 194 |
+
}
|
| 195 |
+
return b;
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
static Vectorized<value_type> C10_ALWAYS_INLINE
|
| 199 |
+
loadu(const void* ptr, int count = size()) {
|
| 200 |
+
if (count == size()) {
|
| 201 |
+
return {
|
| 202 |
+
vec_vsx_ld(offset0, reinterpret_cast<const float*>(ptr)),
|
| 203 |
+
vec_vsx_ld(offset16, reinterpret_cast<const float*>(ptr))};
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
__at_align__ value_type tmp_values[size()] = {};
|
| 207 |
+
std::memcpy(tmp_values, ptr, std::min(count, size()) * sizeof(value_type));
|
| 208 |
+
|
| 209 |
+
return {
|
| 210 |
+
vec_vsx_ld(offset0, reinterpret_cast<const float*>(tmp_values)),
|
| 211 |
+
vec_vsx_ld(offset16, reinterpret_cast<const float*>(tmp_values))};
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
void C10_ALWAYS_INLINE store(void* ptr, int count = size()) const {
|
| 215 |
+
if (count == size()) {
|
| 216 |
+
vec_vsx_st(_vec0, offset0, reinterpret_cast<float*>(ptr));
|
| 217 |
+
vec_vsx_st(_vec1, offset16, reinterpret_cast<float*>(ptr));
|
| 218 |
+
} else if (count > 0) {
|
| 219 |
+
__at_align__ value_type tmp_values[size()];
|
| 220 |
+
vec_vsx_st(_vec0, offset0, reinterpret_cast<float*>(tmp_values));
|
| 221 |
+
vec_vsx_st(_vec1, offset16, reinterpret_cast<float*>(tmp_values));
|
| 222 |
+
std::memcpy(
|
| 223 |
+
ptr, tmp_values, std::min(count, size()) * sizeof(value_type));
|
| 224 |
+
}
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
const ComplexFlt& operator[](int idx) const = delete;
|
| 228 |
+
ComplexFlt& operator[](int idx) = delete;
|
| 229 |
+
|
| 230 |
+
Vectorized<ComplexFlt> map(ComplexFlt (*const f)(ComplexFlt)) const {
|
| 231 |
+
__at_align__ ComplexFlt tmp[size()];
|
| 232 |
+
store(tmp);
|
| 233 |
+
for (const auto i : c10::irange(size())) {
|
| 234 |
+
tmp[i] = f(tmp[i]);
|
| 235 |
+
}
|
| 236 |
+
return loadu(tmp);
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
Vectorized<ComplexFlt> map(ComplexFlt (*const f)(const ComplexFlt&)) const {
|
| 240 |
+
__at_align__ ComplexFlt tmp[size()];
|
| 241 |
+
store(tmp);
|
| 242 |
+
for (const auto i : c10::irange(size())) {
|
| 243 |
+
tmp[i] = f(tmp[i]);
|
| 244 |
+
}
|
| 245 |
+
return loadu(tmp);
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
static Vectorized<ComplexFlt> horizontal_add(
|
| 249 |
+
Vectorized<ComplexFlt>& first,
|
| 250 |
+
Vectorized<ComplexFlt>& second) {
|
| 251 |
+
// Operates on individual floats, see _mm_hadd_ps
|
| 252 |
+
// {f0+f1, s0+s1, f2+f3, s2+s3, ...}
|
| 253 |
+
// i.e. it sums the re and im of each value and interleaves first and second:
|
| 254 |
+
// {f_re0 + f_im0, s_re0 + s_im0, f_re1 + f_im1, s_re1 + s_im1, ...}
|
| 255 |
+
return el_mergee(first, second) + el_mergeo(first, second);
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
static Vectorized<ComplexFlt> horizontal_sub_permD8(
|
| 259 |
+
Vectorized<ComplexFlt>& first,
|
| 260 |
+
Vectorized<ComplexFlt>& second) {
|
| 261 |
+
// we will simulate it differently with 6 instructions total
|
| 262 |
+
// lets permute second so that we can add it getting horizontal sums
|
| 263 |
+
auto first_perm = first.el_swapped(); // 2perm
|
| 264 |
+
auto second_perm = second.el_swapped(); // 2perm
|
| 265 |
+
// sum
|
| 266 |
+
auto first_ret = first - first_perm; // 2sub
|
| 267 |
+
auto second_ret = second - second_perm; // 2 sub
|
| 268 |
+
// now lets choose evens
|
| 269 |
+
return el_mergee(first_ret, second_ret); // 2 mergee's
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
Vectorized<ComplexFlt> abs_2_() const {
|
| 273 |
+
auto a = (*this).elwise_mult(*this);
|
| 274 |
+
auto permuted = a.el_swapped();
|
| 275 |
+
a = a + permuted;
|
| 276 |
+
return a.el_mergee();
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
Vectorized<ComplexFlt> abs_() const {
|
| 280 |
+
auto vi = el_mergeo();
|
| 281 |
+
auto vr = el_mergee();
|
| 282 |
+
return {Sleef_hypotf4_u05vsx(vr._vec0, vi._vec0), Sleef_hypotf4_u05vsx(vr._vec1, vi._vec1)};
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
Vectorized<ComplexFlt> abs() const {
|
| 286 |
+
return abs_() & real_mask;
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
Vectorized<ComplexFlt> real_() const {
|
| 290 |
+
return *this & real_mask;
|
| 291 |
+
}
|
| 292 |
+
Vectorized<ComplexFlt> real() const {
|
| 293 |
+
return *this & real_mask;
|
| 294 |
+
}
|
| 295 |
+
Vectorized<ComplexFlt> imag_() const {
|
| 296 |
+
return *this & imag_mask;
|
| 297 |
+
}
|
| 298 |
+
Vectorized<ComplexFlt> imag() const {
|
| 299 |
+
// we can use swap_mask or sldwi
|
| 300 |
+
auto ret = imag_();
|
| 301 |
+
return {
|
| 302 |
+
vec_sldw(ret._vec0, ret._vec0, 3), vec_sldw(ret._vec1, ret._vec1, 3)};
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
Vectorized<ComplexFlt> conj_() const {
|
| 306 |
+
return *this ^ isign_mask;
|
| 307 |
+
}
|
| 308 |
+
Vectorized<ComplexFlt> conj() const {
|
| 309 |
+
return *this ^ isign_mask;
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
Vectorized<ComplexFlt> log() const {
|
| 313 |
+
// Most trigonomic ops use the log() op to improve complex number
|
| 314 |
+
// performance.
|
| 315 |
+
return map(std::log);
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
Vectorized<ComplexFlt> log2() const {
|
| 319 |
+
// log2eB_inv
|
| 320 |
+
auto ret = log();
|
| 321 |
+
return ret.elwise_mult(log2e_inv);
|
| 322 |
+
}
|
| 323 |
+
Vectorized<ComplexFlt> log10() const {
|
| 324 |
+
auto ret = log();
|
| 325 |
+
return ret.elwise_mult(log10e_inv);
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
Vectorized<ComplexFlt> log1p() const {
|
| 329 |
+
return map(std::log1p);
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
Vectorized<ComplexFlt> el_swapped() const {
|
| 333 |
+
vfloat32 v0 = vec_perm(_vec0, _vec0, swap_mask);
|
| 334 |
+
vfloat32 v1 = vec_perm(_vec1, _vec1, swap_mask);
|
| 335 |
+
return {v0, v1};
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
Vectorized<ComplexFlt> el_mergee() const {
|
| 339 |
+
// as mergee phased in , we can use vec_perm with mask
|
| 340 |
+
return {vec_mergee(_vecb0, _vecb0), vec_mergee(_vecb1, _vecb1)};
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
Vectorized<ComplexFlt> el_mergeo() const {
|
| 344 |
+
// as mergeo phased in , we can use vec_perm with mask
|
| 345 |
+
return {vec_mergeo(_vecb0, _vecb0), vec_mergeo(_vecb1, _vecb1)};
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
Vectorized<ComplexFlt> el_madd(
|
| 349 |
+
const Vectorized<ComplexFlt>& multiplier,
|
| 350 |
+
const Vectorized<ComplexFlt>& val) const {
|
| 351 |
+
return {
|
| 352 |
+
vec_madd(_vec0, multiplier._vec0, val._vec0),
|
| 353 |
+
vec_madd(_vec1, multiplier._vec1, val._vec1)};
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
static Vectorized<ComplexFlt> el_mergee(
|
| 357 |
+
Vectorized<ComplexFlt>& first,
|
| 358 |
+
Vectorized<ComplexFlt>& second) {
|
| 359 |
+
return {
|
| 360 |
+
vec_mergee(first._vecb0, second._vecb0),
|
| 361 |
+
vec_mergee(first._vecb1, second._vecb1)};
|
| 362 |
+
}
|
| 363 |
+
|
| 364 |
+
static Vectorized<ComplexFlt> el_mergeo(
|
| 365 |
+
Vectorized<ComplexFlt>& first,
|
| 366 |
+
Vectorized<ComplexFlt>& second) {
|
| 367 |
+
return {
|
| 368 |
+
vec_mergeo(first._vecb0, second._vecb0),
|
| 369 |
+
vec_mergeo(first._vecb1, second._vecb1)};
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
Vectorized<ComplexFlt> angle_() const {
|
| 373 |
+
// angle = atan2(b/a)
|
| 374 |
+
// auto b_a = _mm256_permute_ps(values, 0xB1); // b a
|
| 375 |
+
// return Sleef_atan2f8_u10(values, b_a); // 90-angle angle
|
| 376 |
+
Vectorized<ComplexFlt> ret;
|
| 377 |
+
for (int i = 0; i < 4; i += 2) {
|
| 378 |
+
ret._vec0[i] = std::atan2(_vec0[i + 1], _vec0[i]);
|
| 379 |
+
ret._vec1[i] = std::atan2(_vec1[i + 1], _vec1[i]);
|
| 380 |
+
}
|
| 381 |
+
return ret;
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
Vectorized<ComplexFlt> angle() const {
|
| 385 |
+
return angle_() & real_mask;
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
Vectorized<ComplexFlt> sin() const {
|
| 389 |
+
return map(std::sin);
|
| 390 |
+
}
|
| 391 |
+
Vectorized<ComplexFlt> sinh() const {
|
| 392 |
+
return map(std::sinh);
|
| 393 |
+
}
|
| 394 |
+
Vectorized<ComplexFlt> cos() const {
|
| 395 |
+
return map(std::cos);
|
| 396 |
+
}
|
| 397 |
+
Vectorized<ComplexFlt> cosh() const {
|
| 398 |
+
return map(std::cosh);
|
| 399 |
+
}
|
| 400 |
+
Vectorized<ComplexFlt> ceil() const {
|
| 401 |
+
return {vec_ceil(_vec0), vec_ceil(_vec1)};
|
| 402 |
+
}
|
| 403 |
+
Vectorized<ComplexFlt> floor() const {
|
| 404 |
+
return {vec_floor(_vec0), vec_floor(_vec1)};
|
| 405 |
+
}
|
| 406 |
+
Vectorized<ComplexFlt> neg() const {
|
| 407 |
+
auto z = Vectorized<ComplexFlt>(zero);
|
| 408 |
+
return z - *this;
|
| 409 |
+
}
|
| 410 |
+
Vectorized<ComplexFlt> round() const {
|
| 411 |
+
return {vec_round(_vec0), vec_round(_vec1)};
|
| 412 |
+
}
|
| 413 |
+
Vectorized<ComplexFlt> tan() const {
|
| 414 |
+
return map(std::tan);
|
| 415 |
+
}
|
| 416 |
+
Vectorized<ComplexFlt> tanh() const {
|
| 417 |
+
return map(std::tanh);
|
| 418 |
+
}
|
| 419 |
+
Vectorized<ComplexFlt> trunc() const {
|
| 420 |
+
return {vec_trunc(_vec0), vec_trunc(_vec1)};
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
Vectorized<ComplexFlt> elwise_sqrt() const {
|
| 424 |
+
return {vec_sqrt(_vec0), vec_sqrt(_vec1)};
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
Vectorized<ComplexFlt> sqrt() const {
|
| 428 |
+
return map(std::sqrt);
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
Vectorized<ComplexFlt> reciprocal() const {
|
| 432 |
+
// re + im*i = (a + bi) / (c + di)
|
| 433 |
+
// re = (ac + bd)/abs_2() = c/abs_2()
|
| 434 |
+
// im = (bc - ad)/abs_2() = d/abs_2()
|
| 435 |
+
auto c_d = *this ^ isign_mask; // c -d
|
| 436 |
+
auto abs = abs_2_();
|
| 437 |
+
return c_d.elwise_div(abs);
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
Vectorized<ComplexFlt> rsqrt() const {
|
| 441 |
+
return sqrt().reciprocal();
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
Vectorized<ComplexFlt> pow(const Vectorized<ComplexFlt>& exp) const {
|
| 445 |
+
__at_align__ ComplexFlt x_tmp[size()];
|
| 446 |
+
__at_align__ ComplexFlt y_tmp[size()];
|
| 447 |
+
store(x_tmp);
|
| 448 |
+
exp.store(y_tmp);
|
| 449 |
+
for (const auto i : c10::irange(size())) {
|
| 450 |
+
x_tmp[i] = std::pow(x_tmp[i], y_tmp[i]);
|
| 451 |
+
}
|
| 452 |
+
return loadu(x_tmp);
|
| 453 |
+
}
|
| 454 |
+
|
| 455 |
+
Vectorized<ComplexFlt> atan() const {
|
| 456 |
+
// atan(x) = i/2 * ln((i + z)/(i - z))
|
| 457 |
+
auto ione = Vectorized(imag_one);
|
| 458 |
+
auto sum = ione + *this;
|
| 459 |
+
auto sub = ione - *this;
|
| 460 |
+
auto ln = (sum / sub).log(); // ln((i + z)/(i - z))
|
| 461 |
+
return ln * imag_half; // i/2*ln()
|
| 462 |
+
}
|
| 463 |
+
Vectorized<ComplexFlt> atanh() const {
|
| 464 |
+
return map(std::atanh);
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
Vectorized<ComplexFlt> acos() const {
|
| 468 |
+
// acos(x) = pi/2 - asin(x)
|
| 469 |
+
return Vectorized(pi_2) - asin();
|
| 470 |
+
}
|
| 471 |
+
|
| 472 |
+
Vectorized<ComplexFlt> inline operator*(const Vectorized<ComplexFlt>& b) const {
|
| 473 |
+
//(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
|
| 474 |
+
|
| 475 |
+
#if 1
|
| 476 |
+
// this is more vsx friendly than simulating horizontal from x86
|
| 477 |
+
|
| 478 |
+
auto vi = b.el_mergeo();
|
| 479 |
+
auto vr = b.el_mergee();
|
| 480 |
+
vi = vi ^ rsign_mask;
|
| 481 |
+
auto ret = elwise_mult(vr);
|
| 482 |
+
auto vx_swapped = el_swapped();
|
| 483 |
+
ret = vx_swapped.el_madd(vi, ret);
|
| 484 |
+
return ret;
|
| 485 |
+
|
| 486 |
+
#else
|
| 487 |
+
|
| 488 |
+
auto ac_bd = elwise_mult(b);
|
| 489 |
+
auto d_c = b.el_swapped();
|
| 490 |
+
d_c = d_c ^ isign_mask;
|
| 491 |
+
auto ad_bc = elwise_mult(d_c);
|
| 492 |
+
auto ret = horizontal_sub_permD8(ac_bd, ad_bc);
|
| 493 |
+
return ret;
|
| 494 |
+
#endif
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
Vectorized<ComplexFlt> inline operator/(const Vectorized<ComplexFlt>& b) const {
|
| 498 |
+
// re + im*i = (a + bi) / (c + di)
|
| 499 |
+
// re = (ac + bd)/abs_2()
|
| 500 |
+
// im = (bc - ad)/abs_2()
|
| 501 |
+
auto fabs_cd = Vectorized{
|
| 502 |
+
vec_andc(b._vec0, sign_mask),
|
| 503 |
+
vec_andc(b._vec1, sign_mask)}; // |c| |d|
|
| 504 |
+
auto fabs_dc = fabs_cd.el_swapped(); // |d| |c|
|
| 505 |
+
auto scale = fabs_cd.elwise_max(fabs_dc); // sc = max(|c|, |d|)
|
| 506 |
+
auto a2 = elwise_div(scale); // a/sc b/sc
|
| 507 |
+
auto b2 = b.elwise_div(scale); // c/sc d/sc
|
| 508 |
+
auto acbd2 = a2.elwise_mult(b2); // ac/sc^2 bd/sc^2
|
| 509 |
+
auto dc2 = b2.el_swapped(); // d/sc c/sc
|
| 510 |
+
dc2 = dc2 ^ rsign_mask; // -d/sc c/sc
|
| 511 |
+
auto adbc2 = a2.elwise_mult(dc2); // -ad/sc^2 bc/sc^2
|
| 512 |
+
auto ret = horizontal_add(acbd2, adbc2); // (ac+bd)/sc^2 (bc-ad)/sc^2
|
| 513 |
+
auto denom2 = b2.abs_2_(); // (c^2+d^2)/sc^2 (c^2+d^2)/sc^2
|
| 514 |
+
ret = ret.elwise_div(denom2);
|
| 515 |
+
return ret;
|
| 516 |
+
}
|
| 517 |
+
|
| 518 |
+
Vectorized<ComplexFlt> asin() const {
|
| 519 |
+
// asin(x)
|
| 520 |
+
// = -i*ln(iz + sqrt(1 -z^2))
|
| 521 |
+
// = -i*ln((ai - b) + sqrt(1 - (a + bi)*(a + bi)))
|
| 522 |
+
// = -i*ln((-b + ai) + sqrt(1 - (a**2 - b**2) - 2*abi))
|
| 523 |
+
|
| 524 |
+
#if 1
|
| 525 |
+
auto conj = conj_();
|
| 526 |
+
auto b_a = conj.el_swapped();
|
| 527 |
+
auto ab = conj.elwise_mult(b_a);
|
| 528 |
+
auto im = ab + ab;
|
| 529 |
+
auto val_2 = (*this).elwise_mult(*this);
|
| 530 |
+
auto val_2_swapped = val_2.el_swapped();
|
| 531 |
+
auto re = horizontal_sub_permD8(val_2, val_2_swapped);
|
| 532 |
+
re = Vectorized<ComplexFlt>(one) - re;
|
| 533 |
+
auto root = el_blend<0xAA>(re, im).sqrt();
|
| 534 |
+
auto ln = (b_a + root).log();
|
| 535 |
+
return ln.el_swapped().conj();
|
| 536 |
+
#else
|
| 537 |
+
return map(std::asin);
|
| 538 |
+
#endif
|
| 539 |
+
}
|
| 540 |
+
|
| 541 |
+
Vectorized<ComplexFlt> exp() const {
|
| 542 |
+
return map(std::exp);
|
| 543 |
+
}
|
| 544 |
+
Vectorized<ComplexFlt> exp2() const {
|
| 545 |
+
return map(exp2_impl);
|
| 546 |
+
}
|
| 547 |
+
Vectorized<ComplexFlt> expm1() const {
|
| 548 |
+
return map(std::expm1);
|
| 549 |
+
}
|
| 550 |
+
|
| 551 |
+
Vectorized<ComplexFlt> eq(const Vectorized<ComplexFlt>& other) const {
|
| 552 |
+
auto eq = (*this == other); // compares real and imag individually
|
| 553 |
+
// If both real numbers and imag numbers are equal, then the complex numbers are equal
|
| 554 |
+
return (eq.real() & eq.imag()) & one;
|
| 555 |
+
}
|
| 556 |
+
Vectorized<ComplexFlt> ne(const Vectorized<ComplexFlt>& other) const {
|
| 557 |
+
auto ne = (*this != other); // compares real and imag individually
|
| 558 |
+
// If either real numbers or imag numbers are not equal, then the complex numbers are not equal
|
| 559 |
+
return (ne.real() | ne.imag()) & one;
|
| 560 |
+
}
|
| 561 |
+
|
| 562 |
+
Vectorized<ComplexFlt> sgn() const {
|
| 563 |
+
return map(at::native::sgn_impl);
|
| 564 |
+
}
|
| 565 |
+
|
| 566 |
+
Vectorized<ComplexFlt> operator<(const Vectorized<ComplexFlt>& other) const {
|
| 567 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
Vectorized<ComplexFlt> operator<=(const Vectorized<ComplexFlt>& other) const {
|
| 571 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 572 |
+
}
|
| 573 |
+
|
| 574 |
+
Vectorized<ComplexFlt> operator>(const Vectorized<ComplexFlt>& other) const {
|
| 575 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 576 |
+
}
|
| 577 |
+
|
| 578 |
+
Vectorized<ComplexFlt> operator>=(const Vectorized<ComplexFlt>& other) const {
|
| 579 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 580 |
+
}
|
| 581 |
+
|
| 582 |
+
DEFINE_MEMBER_OP(operator==, ComplexFlt, vec_cmpeq)
|
| 583 |
+
DEFINE_MEMBER_OP(operator!=, ComplexFlt, vec_cmpne)
|
| 584 |
+
|
| 585 |
+
DEFINE_MEMBER_OP(operator+, ComplexFlt, vec_add)
|
| 586 |
+
DEFINE_MEMBER_OP(operator-, ComplexFlt, vec_sub)
|
| 587 |
+
DEFINE_MEMBER_OP(operator&, ComplexFlt, vec_and)
|
| 588 |
+
DEFINE_MEMBER_OP(operator|, ComplexFlt, vec_or)
|
| 589 |
+
DEFINE_MEMBER_OP(operator^, ComplexFlt, vec_xor)
|
| 590 |
+
// elementwise helpers
|
| 591 |
+
DEFINE_MEMBER_OP(elwise_mult, ComplexFlt, vec_mul)
|
| 592 |
+
DEFINE_MEMBER_OP(elwise_div, ComplexFlt, vec_div)
|
| 593 |
+
DEFINE_MEMBER_OP(elwise_gt, ComplexFlt, vec_cmpgt)
|
| 594 |
+
DEFINE_MEMBER_OP(elwise_ge, ComplexFlt, vec_cmpge)
|
| 595 |
+
DEFINE_MEMBER_OP(elwise_lt, ComplexFlt, vec_cmplt)
|
| 596 |
+
DEFINE_MEMBER_OP(elwise_le, ComplexFlt, vec_cmple)
|
| 597 |
+
DEFINE_MEMBER_OP(elwise_max, ComplexFlt, vec_max)
|
| 598 |
+
};
|
| 599 |
+
|
| 600 |
+
template <>
|
| 601 |
+
Vectorized<ComplexFlt> inline maximum(
|
| 602 |
+
const Vectorized<ComplexFlt>& a,
|
| 603 |
+
const Vectorized<ComplexFlt>& b) {
|
| 604 |
+
auto abs_a = a.abs_2_();
|
| 605 |
+
auto abs_b = b.abs_2_();
|
| 606 |
+
// auto mask = _mm256_cmp_ps(abs_a, abs_b, _CMP_LT_OQ);
|
| 607 |
+
// auto max = _mm256_blendv_ps(a, b, mask);
|
| 608 |
+
auto mask = abs_a.elwise_lt(abs_b);
|
| 609 |
+
auto max = Vectorized<ComplexFlt>::elwise_blendv(a, b, mask);
|
| 610 |
+
|
| 611 |
+
return max;
|
| 612 |
+
// Exploit the fact that all-ones is a NaN.
|
| 613 |
+
// auto isnan = _mm256_cmp_ps(abs_a, abs_b, _CMP_UNORD_Q);
|
| 614 |
+
// return _mm256_or_ps(max, isnan);
|
| 615 |
+
}
|
| 616 |
+
|
| 617 |
+
template <>
|
| 618 |
+
Vectorized<ComplexFlt> inline minimum(
|
| 619 |
+
const Vectorized<ComplexFlt>& a,
|
| 620 |
+
const Vectorized<ComplexFlt>& b) {
|
| 621 |
+
auto abs_a = a.abs_2_();
|
| 622 |
+
auto abs_b = b.abs_2_();
|
| 623 |
+
// auto mask = _mm256_cmp_ps(abs_a, abs_b, _CMP_GT_OQ);
|
| 624 |
+
// auto min = _mm256_blendv_ps(a, b, mask);
|
| 625 |
+
auto mask = abs_a.elwise_gt(abs_b);
|
| 626 |
+
auto min = Vectorized<ComplexFlt>::elwise_blendv(a, b, mask);
|
| 627 |
+
return min;
|
| 628 |
+
// Exploit the fact that all-ones is a NaN.
|
| 629 |
+
// auto isnan = _mm256_cmp_ps(abs_a, abs_b, _CMP_UNORD_Q);
|
| 630 |
+
// return _mm256_or_ps(min, isnan);
|
| 631 |
+
}
|
| 632 |
+
|
| 633 |
+
template <>
|
| 634 |
+
Vectorized<ComplexFlt> C10_ALWAYS_INLINE operator+(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 635 |
+
return Vectorized<ComplexFlt>{vec_add(a.vec0(), b.vec0()), vec_add(a.vec1(), b.vec1())};
|
| 636 |
+
}
|
| 637 |
+
|
| 638 |
+
template <>
|
| 639 |
+
Vectorized<ComplexFlt> C10_ALWAYS_INLINE operator-(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 640 |
+
return Vectorized<ComplexFlt>{vec_sub(a.vec0(), b.vec0()), vec_sub(a.vec1(), b.vec1())};
|
| 641 |
+
}
|
| 642 |
+
|
| 643 |
+
template <>
|
| 644 |
+
Vectorized<ComplexFlt> C10_ALWAYS_INLINE operator&(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 645 |
+
return Vectorized<ComplexFlt>{vec_and(a.vec0(), b.vec0()), vec_and(a.vec1(), b.vec1())};
|
| 646 |
+
}
|
| 647 |
+
|
| 648 |
+
template <>
|
| 649 |
+
Vectorized<ComplexFlt> C10_ALWAYS_INLINE operator|(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 650 |
+
return Vectorized<ComplexFlt>{vec_or(a.vec0(), b.vec0()), vec_or(a.vec1(), b.vec1())};
|
| 651 |
+
}
|
| 652 |
+
|
| 653 |
+
template <>
|
| 654 |
+
Vectorized<ComplexFlt> C10_ALWAYS_INLINE operator^(const Vectorized<ComplexFlt>& a, const Vectorized<ComplexFlt>& b) {
|
| 655 |
+
return Vectorized<ComplexFlt>{vec_xor(a.vec0(), b.vec0()), vec_xor(a.vec1(), b.vec1())};
|
| 656 |
+
}
|
| 657 |
+
|
| 658 |
+
} // namespace
|
| 659 |
+
} // namespace vec
|
| 660 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_double_vsx.h
ADDED
|
@@ -0,0 +1,477 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 4 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 5 |
+
#include <ATen/cpu/vec/vec256/vsx/vsx_helpers.h>
|
| 6 |
+
#include <c10/util/irange.h>
|
| 7 |
+
|
| 8 |
+
#include <sleef.h>
|
| 9 |
+
|
| 10 |
+
namespace at {
|
| 11 |
+
namespace vec {
|
| 12 |
+
|
| 13 |
+
inline namespace CPU_CAPABILITY {
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
template <>
|
| 17 |
+
class Vectorized<double> {
|
| 18 |
+
private:
|
| 19 |
+
union {
|
| 20 |
+
struct {
|
| 21 |
+
vfloat64 _vec0;
|
| 22 |
+
vfloat64 _vec1;
|
| 23 |
+
};
|
| 24 |
+
struct {
|
| 25 |
+
vbool64 _vecb0;
|
| 26 |
+
vbool64 _vecb1;
|
| 27 |
+
};
|
| 28 |
+
|
| 29 |
+
} __attribute__((__may_alias__));
|
| 30 |
+
|
| 31 |
+
public:
|
| 32 |
+
using value_type = double;
|
| 33 |
+
using vec_internal_type = vfloat64;
|
| 34 |
+
using vec_internal_mask_type = vbool64;
|
| 35 |
+
using size_type = int;
|
| 36 |
+
static constexpr size_type size() {
|
| 37 |
+
return 4;
|
| 38 |
+
}
|
| 39 |
+
Vectorized() {}
|
| 40 |
+
C10_ALWAYS_INLINE Vectorized(vfloat64 v) : _vec0{v}, _vec1{v} {}
|
| 41 |
+
C10_ALWAYS_INLINE Vectorized(vbool64 vmask) : _vecb0{vmask}, _vecb1{vmask} {}
|
| 42 |
+
C10_ALWAYS_INLINE Vectorized(vfloat64 v1, vfloat64 v2) : _vec0{v1}, _vec1{v2} {}
|
| 43 |
+
C10_ALWAYS_INLINE Vectorized(vbool64 v1, vbool64 v2) : _vecb0{v1}, _vecb1{v2} {}
|
| 44 |
+
C10_ALWAYS_INLINE Vectorized(double scalar)
|
| 45 |
+
: _vec0{vec_splats(scalar)}, _vec1{vec_splats(scalar)} {}
|
| 46 |
+
C10_ALWAYS_INLINE Vectorized(
|
| 47 |
+
double scalar1,
|
| 48 |
+
double scalar2,
|
| 49 |
+
double scalar3,
|
| 50 |
+
double scalar4)
|
| 51 |
+
: _vec0{vfloat64{scalar1, scalar2}}, _vec1{vfloat64{scalar3, scalar4}} {}
|
| 52 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec0() const {
|
| 53 |
+
return _vec0;
|
| 54 |
+
}
|
| 55 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec1() const {
|
| 56 |
+
return _vec1;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
int zero_mask() const {
|
| 60 |
+
auto cmp = (*this == vd_zero);
|
| 61 |
+
return (cmp._vecb0[0] & 1) | (cmp._vecb0[1] & 2) | (cmp._vecb1[0] & 4) |
|
| 62 |
+
(cmp._vecb1[1] & 8);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
template <int64_t mask>
|
| 66 |
+
static std::enable_if_t<blendChoiceDbl(mask) == 0, Vectorized<double>> C10_ALWAYS_INLINE
|
| 67 |
+
blend(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 68 |
+
return a;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
template <int64_t mask>
|
| 72 |
+
static std::enable_if_t<blendChoiceDbl(mask) == 1, Vectorized<double>> C10_ALWAYS_INLINE
|
| 73 |
+
blend(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 74 |
+
return b;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
template <int64_t mask>
|
| 78 |
+
static std::enable_if_t<blendChoiceDbl(mask) == 2, Vectorized<double>> C10_ALWAYS_INLINE
|
| 79 |
+
blend(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 80 |
+
return { b._vec0, a._vec1 };
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
template <int64_t mask>
|
| 84 |
+
static std::enable_if_t<blendChoiceDbl(mask) == 3, Vectorized<double>> C10_ALWAYS_INLINE
|
| 85 |
+
blend(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 86 |
+
return { a._vec0, b._vec1 };
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
template <int64_t mask>
|
| 91 |
+
static std::enable_if_t<blendChoiceDbl(mask) == 4, Vectorized<double>> C10_ALWAYS_INLINE
|
| 92 |
+
blend(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 93 |
+
const vbool64 mask_1st = VsxDblMask1(mask);
|
| 94 |
+
return { (vfloat64)vec_sel(a._vec0, b._vec0, mask_1st), a._vec1 };
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
template <int64_t mask>
|
| 98 |
+
static std::enable_if_t<blendChoiceDbl(mask) == 5, Vectorized<double>> C10_ALWAYS_INLINE
|
| 99 |
+
blend(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 100 |
+
const vbool64 mask_1st = VsxDblMask1(mask);
|
| 101 |
+
return { (vfloat64)vec_sel(a._vec0, b._vec0, mask_1st), b._vec1 };
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
template <int64_t mask>
|
| 106 |
+
static std::enable_if_t<blendChoiceDbl(mask) == 6,
|
| 107 |
+
Vectorized<double>>
|
| 108 |
+
C10_ALWAYS_INLINE blend(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 109 |
+
const vbool64 mask_2nd = VsxDblMask2(mask);
|
| 110 |
+
// generated masks
|
| 111 |
+
return { a._vec0,
|
| 112 |
+
(vfloat64)vec_sel(a._vec1, b._vec1, mask_2nd) };
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
template <int64_t mask>
|
| 116 |
+
static std::enable_if_t<blendChoiceDbl(mask) == 7,
|
| 117 |
+
Vectorized<double>>
|
| 118 |
+
C10_ALWAYS_INLINE blend(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 119 |
+
const vbool64 mask_2nd = VsxDblMask2(mask);
|
| 120 |
+
// generated masks
|
| 121 |
+
return { b._vec0,
|
| 122 |
+
(vfloat64)vec_sel(a._vec1, b._vec1, mask_2nd) };
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
template <int64_t mask>
|
| 126 |
+
static std::enable_if_t<blendChoiceDbl(mask) == 8, Vectorized<double>>
|
| 127 |
+
C10_ALWAYS_INLINE blend(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 128 |
+
const vbool64 mask_1st = VsxDblMask1(mask);
|
| 129 |
+
const vbool64 mask_2nd = VsxDblMask2(mask);
|
| 130 |
+
return {
|
| 131 |
+
(vfloat64)vec_sel(a._vec0, b._vec0, mask_1st),
|
| 132 |
+
(vfloat64)vec_sel(a._vec1, b._vec1, mask_2nd) };
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
static Vectorized<double> C10_ALWAYS_INLINE blendv(
|
| 137 |
+
const Vectorized<double>& a,
|
| 138 |
+
const Vectorized<double>& b,
|
| 139 |
+
const Vectorized<double>& mask) {
|
| 140 |
+
// the mask used here returned by comparision of vec256
|
| 141 |
+
|
| 142 |
+
return {
|
| 143 |
+
vec_sel(a._vec0, b._vec0, mask._vecb0),
|
| 144 |
+
vec_sel(a._vec1, b._vec1, mask._vecb1)};
|
| 145 |
+
}
|
| 146 |
+
template <typename step_t>
|
| 147 |
+
static Vectorized<double> arange(double base = 0., step_t step = static_cast<step_t>(1)) {
|
| 148 |
+
return Vectorized<double>(base, base + step, base + 2 * step, base + 3 * step);
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
static Vectorized<double> C10_ALWAYS_INLINE
|
| 152 |
+
set(const Vectorized<double>& a, const Vectorized<double>& b, size_t count = size()) {
|
| 153 |
+
switch (count) {
|
| 154 |
+
case 0:
|
| 155 |
+
return a;
|
| 156 |
+
case 1:
|
| 157 |
+
return blend<1>(a, b);
|
| 158 |
+
case 2:
|
| 159 |
+
return blend<3>(a, b);
|
| 160 |
+
case 3:
|
| 161 |
+
return blend<7>(a, b);
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
return b;
|
| 165 |
+
}
|
| 166 |
+
static Vectorized<value_type> C10_ALWAYS_INLINE
|
| 167 |
+
loadu(const void* ptr, int count = size()) {
|
| 168 |
+
if (count == size()) {
|
| 169 |
+
return {
|
| 170 |
+
vec_vsx_ld(offset0, reinterpret_cast<const value_type*>(ptr)),
|
| 171 |
+
vec_vsx_ld(offset16, reinterpret_cast<const value_type*>(ptr))};
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
__at_align__ value_type tmp_values[size()] = {};
|
| 175 |
+
std::memcpy(tmp_values, ptr, std::min(count, size()) * sizeof(value_type));
|
| 176 |
+
|
| 177 |
+
return {vec_vsx_ld(offset0, tmp_values), vec_vsx_ld(offset16, tmp_values)};
|
| 178 |
+
}
|
| 179 |
+
void C10_ALWAYS_INLINE store(void* ptr, int count = size()) const {
|
| 180 |
+
if (count == size()) {
|
| 181 |
+
vec_vsx_st(_vec0, offset0, reinterpret_cast<value_type*>(ptr));
|
| 182 |
+
vec_vsx_st(_vec1, offset16, reinterpret_cast<value_type*>(ptr));
|
| 183 |
+
} else if (count > 0) {
|
| 184 |
+
__at_align__ value_type tmp_values[size()];
|
| 185 |
+
vec_vsx_st(_vec0, offset0, tmp_values);
|
| 186 |
+
vec_vsx_st(_vec1, offset16, tmp_values);
|
| 187 |
+
std::memcpy(
|
| 188 |
+
ptr, tmp_values, std::min(count, size()) * sizeof(value_type));
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
const double& operator[](int idx) const = delete;
|
| 192 |
+
double& operator[](int idx) = delete;
|
| 193 |
+
Vectorized<double> map(double (*const f)(double)) const {
|
| 194 |
+
Vectorized<double> ret;
|
| 195 |
+
for (const auto i : c10::irange(size()/2)) {
|
| 196 |
+
ret._vec0[i] = f(_vec0[i]);
|
| 197 |
+
}
|
| 198 |
+
for (const auto i : c10::irange(size()/2)) {
|
| 199 |
+
ret._vec1[i] = f(_vec1[i]);
|
| 200 |
+
}
|
| 201 |
+
return ret;
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
Vectorized<double> mapbi(double (*const f)(double, double), const Vectorized<double>& other)
|
| 205 |
+
const {
|
| 206 |
+
Vectorized<double> ret;
|
| 207 |
+
for (const auto i : c10::irange(size()/2)) {
|
| 208 |
+
ret._vec0[i] = f(_vec0[i], other._vec0[i]);
|
| 209 |
+
}
|
| 210 |
+
for (const auto i : c10::irange(size()/2)) {
|
| 211 |
+
ret._vec1[i] = f(_vec1[i], other._vec1[i]);
|
| 212 |
+
}
|
| 213 |
+
return ret;
|
| 214 |
+
}
|
| 215 |
+
Vectorized<double> C10_ALWAYS_INLINE abs() const {
|
| 216 |
+
return {vec_abs(_vec0), vec_abs(_vec1)};
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
Vectorized<double> C10_ALWAYS_INLINE acos() const {
|
| 220 |
+
return {Sleef_acosd2_u10(_vec0), Sleef_acosd2_u10(_vec1)};
|
| 221 |
+
}
|
| 222 |
+
Vectorized<double> C10_ALWAYS_INLINE acosh() const {
|
| 223 |
+
return {Sleef_acoshd2_u10(_vec0), Sleef_acoshd2_u10(_vec1)};
|
| 224 |
+
}
|
| 225 |
+
Vectorized<double> C10_ALWAYS_INLINE asin() const {
|
| 226 |
+
return {Sleef_asind2_u10(_vec0), Sleef_asind2_u10(_vec1)};
|
| 227 |
+
}
|
| 228 |
+
Vectorized<double> atan() const {
|
| 229 |
+
return {Sleef_atand2_u10(_vec0), Sleef_atand2_u10(_vec1)};
|
| 230 |
+
}
|
| 231 |
+
Vectorized<double> atanh() const {
|
| 232 |
+
return {Sleef_atanhd2_u10(_vec0), Sleef_atanhd2_u10(_vec1)};
|
| 233 |
+
}
|
| 234 |
+
Vectorized<double> atan2(const Vectorized<double>& b) const {
|
| 235 |
+
return {Sleef_atan2d2_u10(_vec0, b._vec0), Sleef_atan2d2_u10(_vec1, b._vec1)};
|
| 236 |
+
}
|
| 237 |
+
Vectorized<double> copysign(const Vectorized<double> &sign) const {
|
| 238 |
+
return {Sleef_copysignd2(_vec0, sign._vec0), Sleef_copysignd2(_vec1, sign._vec1)};
|
| 239 |
+
}
|
| 240 |
+
Vectorized<double> erf() const {
|
| 241 |
+
return {Sleef_erfd2_u10(_vec0), Sleef_erfd2_u10(_vec1)};
|
| 242 |
+
}
|
| 243 |
+
Vectorized<double> erfc() const {
|
| 244 |
+
return {Sleef_erfcd2_u15(_vec0), Sleef_erfcd2_u15(_vec1)};
|
| 245 |
+
}
|
| 246 |
+
Vectorized<double> C10_ALWAYS_INLINE exp() const {
|
| 247 |
+
return {Sleef_expd2_u10(_vec0), Sleef_expd2_u10(_vec1)};
|
| 248 |
+
}
|
| 249 |
+
Vectorized<double> C10_ALWAYS_INLINE exp2() const {
|
| 250 |
+
return {Sleef_exp2d2_u10(_vec0), Sleef_exp2d2_u10(_vec1)};
|
| 251 |
+
}
|
| 252 |
+
Vectorized<double> expm1() const {
|
| 253 |
+
return {Sleef_expm1d2_u10(_vec0), Sleef_expm1d2_u10(_vec1)};
|
| 254 |
+
}
|
| 255 |
+
Vectorized<double> C10_ALWAYS_INLINE exp_u20() const {
|
| 256 |
+
return exp();
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
Vectorized<double> lgamma() const __ubsan_ignore_undefined__ {
|
| 260 |
+
return {Sleef_lgammad2_u10(_vec0), Sleef_lgammad2_u10(_vec1)};
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
Vectorized<double> erfinv() const {
|
| 264 |
+
return map(calc_erfinv);
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
Vectorized<double> angle() const {
|
| 268 |
+
auto tmp = blendv(
|
| 269 |
+
Vectorized<double>(0), Vectorized<double>(c10::pi<double>), *this < Vectorized<double>(0));
|
| 270 |
+
return blendv(tmp, *this, isnan());
|
| 271 |
+
}
|
| 272 |
+
Vectorized<double> real() const {
|
| 273 |
+
return *this;
|
| 274 |
+
}
|
| 275 |
+
Vectorized<double> imag() const {
|
| 276 |
+
return Vectorized<double>{0};
|
| 277 |
+
}
|
| 278 |
+
Vectorized<double> conj() const {
|
| 279 |
+
return *this;
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
Vectorized<double> C10_ALWAYS_INLINE log() const {
|
| 283 |
+
return {Sleef_logd2_u10(_vec0), Sleef_logd2_u10(_vec1)};
|
| 284 |
+
}
|
| 285 |
+
Vectorized<double> C10_ALWAYS_INLINE log10() const {
|
| 286 |
+
return {Sleef_log10d2_u10(_vec0), Sleef_log10d2_u10(_vec1)};
|
| 287 |
+
}
|
| 288 |
+
Vectorized<double> C10_ALWAYS_INLINE log1p() const {
|
| 289 |
+
return {Sleef_log1pd2_u10(_vec0), Sleef_log1pd2_u10(_vec1)};
|
| 290 |
+
}
|
| 291 |
+
Vectorized<double> C10_ALWAYS_INLINE log2() const {
|
| 292 |
+
return {Sleef_log2d2_u10(_vec0), Sleef_log2d2_u10(_vec1)};
|
| 293 |
+
}
|
| 294 |
+
Vectorized<double> C10_ALWAYS_INLINE ceil() const {
|
| 295 |
+
return {vec_ceil(_vec0), vec_ceil(_vec1)};
|
| 296 |
+
}
|
| 297 |
+
Vectorized<double> C10_ALWAYS_INLINE cos() const {
|
| 298 |
+
return {Sleef_cosd2_u10(_vec0), Sleef_cosd2_u10(_vec1)};
|
| 299 |
+
}
|
| 300 |
+
Vectorized<double> C10_ALWAYS_INLINE cosh() const {
|
| 301 |
+
return {Sleef_coshd2_u10(_vec0), Sleef_coshd2_u10(_vec1)};
|
| 302 |
+
}
|
| 303 |
+
Vectorized<double> C10_ALWAYS_INLINE floor() const {
|
| 304 |
+
return {vec_floor(_vec0), vec_floor(_vec1)};
|
| 305 |
+
}
|
| 306 |
+
Vectorized<double> C10_ALWAYS_INLINE neg() const {
|
| 307 |
+
return {vec_neg(_vec0), vec_neg(_vec1)};
|
| 308 |
+
}
|
| 309 |
+
Vectorized<double> C10_ALWAYS_INLINE round() const {
|
| 310 |
+
return {vec_rint(_vec0), vec_rint(_vec1)};
|
| 311 |
+
}
|
| 312 |
+
Vectorized<double> C10_ALWAYS_INLINE sin() const {
|
| 313 |
+
return {Sleef_sind2_u10(_vec0), Sleef_sind2_u10(_vec1)};
|
| 314 |
+
}
|
| 315 |
+
Vectorized<double> C10_ALWAYS_INLINE sinh() const {
|
| 316 |
+
return {Sleef_sinhd2_u10(_vec0), Sleef_sinhd2_u10(_vec1)};
|
| 317 |
+
}
|
| 318 |
+
Vectorized<double> C10_ALWAYS_INLINE tan() const {
|
| 319 |
+
return {Sleef_tand2_u10(_vec0), Sleef_tand2_u10(_vec1)};
|
| 320 |
+
}
|
| 321 |
+
Vectorized<double> C10_ALWAYS_INLINE tanh() const {
|
| 322 |
+
return {Sleef_tanhd2_u10(_vec0), Sleef_tanhd2_u10(_vec1)};
|
| 323 |
+
}
|
| 324 |
+
Vectorized<double> C10_ALWAYS_INLINE trunc() const {
|
| 325 |
+
return {vec_trunc(_vec0), vec_trunc(_vec1)};
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
Vectorized<double> C10_ALWAYS_INLINE frac() const {
|
| 329 |
+
return *this - trunc();
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
Vectorized<double> C10_ALWAYS_INLINE sqrt() const {
|
| 333 |
+
return {vec_sqrt(_vec0), vec_sqrt(_vec1)};
|
| 334 |
+
}
|
| 335 |
+
Vectorized<double> C10_ALWAYS_INLINE reciprocal() const {
|
| 336 |
+
return {
|
| 337 |
+
vec_div(vd_one, _vec0), // vec_re(_vec0) is estimated one.
|
| 338 |
+
vec_div(vd_one, _vec1)};
|
| 339 |
+
}
|
| 340 |
+
Vectorized<double> C10_ALWAYS_INLINE rsqrt() const {
|
| 341 |
+
return sqrt().reciprocal();
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
Vectorized<double> C10_ALWAYS_INLINE pow(const Vectorized<double>& b) const {
|
| 345 |
+
return {Sleef_powd2_u10(_vec0, b._vec0), Sleef_powd2_u10(_vec1, b._vec1)};
|
| 346 |
+
}
|
| 347 |
+
Vectorized<double> C10_ALWAYS_INLINE fmod(const Vectorized<double>& b) const {
|
| 348 |
+
return {Sleef_fmodd2(_vec0, b._vec0),Sleef_fmodd2(_vec1, b._vec1)};
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
Vectorized<double> hypot(const Vectorized<double>& b) const {
|
| 352 |
+
return {Sleef_hypotd2_u05(_vec0, b._vec0), Sleef_hypotd2_u05(_vec1, b._vec1)};
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
Vectorized<double> nextafter(const Vectorized<double>& b) const {
|
| 356 |
+
return {Sleef_nextafterd2(_vec0, b._vec0), Sleef_nextafterd2(_vec1, b._vec1)};
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
Vectorized<double> igamma(const Vectorized<double>& x) const {
|
| 360 |
+
return mapbi(calc_igamma, x);
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
Vectorized<double> igammac(const Vectorized<double>& x) const {
|
| 364 |
+
return mapbi(calc_igammac, x);
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
Vectorized<double> i0() const {
|
| 369 |
+
return map(calc_i0);
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
Vectorized<double> i0e() const {
|
| 373 |
+
return map(calc_i0e);
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
Vectorized<double> digamma() const {
|
| 377 |
+
return map(calc_digamma);
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
Vectorized<double> _nor() const {
|
| 381 |
+
return {vec_nor(_vec0, _vec0), vec_nor(_vec1, _vec1)};
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
Vectorized<double> isnan() const {
|
| 385 |
+
auto x = *this;
|
| 386 |
+
auto ret = (x == x);
|
| 387 |
+
return ret._nor();
|
| 388 |
+
}
|
| 389 |
+
bool has_inf_nan() const {
|
| 390 |
+
for (const auto i : c10::irange(size()/2)) {
|
| 391 |
+
if(_isnan(_vec0[i]) || _isinf(_vec0[i])) {
|
| 392 |
+
return true;
|
| 393 |
+
}
|
| 394 |
+
}
|
| 395 |
+
for (const auto i : c10::irange(size()/2)) {
|
| 396 |
+
if(_isnan(_vec1[i]) || _isinf(_vec1[i])) {
|
| 397 |
+
return true;
|
| 398 |
+
}
|
| 399 |
+
}
|
| 400 |
+
return false;
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
DEFINE_MEMBER_OP(operator==, double, vec_cmpeq)
|
| 404 |
+
DEFINE_MEMBER_OP(operator!=, double, vec_cmpne)
|
| 405 |
+
DEFINE_MEMBER_OP(operator<, double, vec_cmplt)
|
| 406 |
+
DEFINE_MEMBER_OP(operator<=, double, vec_cmple)
|
| 407 |
+
DEFINE_MEMBER_OP(operator>, double, vec_cmpgt)
|
| 408 |
+
DEFINE_MEMBER_OP(operator>=, double, vec_cmpge)
|
| 409 |
+
DEFINE_MEMBER_OP_AND_ONE(eq, double, vec_cmpeq)
|
| 410 |
+
DEFINE_MEMBER_OP_AND_ONE(ne, double, vec_cmpne)
|
| 411 |
+
DEFINE_MEMBER_OP_AND_ONE(lt, double, vec_cmplt)
|
| 412 |
+
DEFINE_MEMBER_OP_AND_ONE(le, double, vec_cmple)
|
| 413 |
+
DEFINE_MEMBER_OP_AND_ONE(gt, double, vec_cmpgt)
|
| 414 |
+
DEFINE_MEMBER_OP_AND_ONE(ge, double, vec_cmpge)
|
| 415 |
+
DEFINE_MEMBER_OP(operator+, double, vec_add)
|
| 416 |
+
DEFINE_MEMBER_OP(operator-, double, vec_sub)
|
| 417 |
+
DEFINE_MEMBER_OP(operator*, double, vec_mul)
|
| 418 |
+
DEFINE_MEMBER_OP(operator/, double, vec_div)
|
| 419 |
+
DEFINE_MEMBER_OP(maximum, double, vec_max_nan2)
|
| 420 |
+
DEFINE_MEMBER_OP(minimum, double, vec_min_nan2)
|
| 421 |
+
DEFINE_MEMBER_OP(operator&, double, vec_and)
|
| 422 |
+
DEFINE_MEMBER_OP(operator|, double, vec_or)
|
| 423 |
+
DEFINE_MEMBER_OP(operator^, double, vec_xor)
|
| 424 |
+
DEFINE_MEMBER_TERNARY_OP(madd, double, vec_madd)
|
| 425 |
+
};
|
| 426 |
+
template <>
|
| 427 |
+
Vectorized<double> inline maximum(
|
| 428 |
+
const Vectorized<double>& a,
|
| 429 |
+
const Vectorized<double>& b) {
|
| 430 |
+
return a.maximum(b);
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
template <>
|
| 434 |
+
Vectorized<double> inline minimum(
|
| 435 |
+
const Vectorized<double>& a,
|
| 436 |
+
const Vectorized<double>& b) {
|
| 437 |
+
return a.minimum(b);
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
template <>
|
| 441 |
+
Vectorized<double> C10_ALWAYS_INLINE operator+(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 442 |
+
return Vectorized<double>{vec_add(a.vec0(), b.vec0()), vec_add(a.vec1(), b.vec1())};
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
template <>
|
| 446 |
+
Vectorized<double> C10_ALWAYS_INLINE operator-(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 447 |
+
return Vectorized<double>{vec_sub(a.vec0(), b.vec0()), vec_sub(a.vec1(), b.vec1())};
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
template <>
|
| 451 |
+
Vectorized<double> C10_ALWAYS_INLINE operator*(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 452 |
+
return Vectorized<double>{vec_mul(a.vec0(), b.vec0()), vec_mul(a.vec1(), b.vec1())};
|
| 453 |
+
}
|
| 454 |
+
|
| 455 |
+
template <>
|
| 456 |
+
Vectorized<double> C10_ALWAYS_INLINE operator/(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 457 |
+
return Vectorized<double>{vec_div(a.vec0(), b.vec0()), vec_div(a.vec1(), b.vec1())};
|
| 458 |
+
}
|
| 459 |
+
|
| 460 |
+
template <>
|
| 461 |
+
Vectorized<double> C10_ALWAYS_INLINE operator&(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 462 |
+
return Vectorized<double>{vec_and(a.vec0(), b.vec0()), vec_and(a.vec1(), b.vec1())};
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
template <>
|
| 466 |
+
Vectorized<double> C10_ALWAYS_INLINE operator|(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 467 |
+
return Vectorized<double>{vec_or(a.vec0(), b.vec0()), vec_or(a.vec1(), b.vec1())};
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
template <>
|
| 471 |
+
Vectorized<double> C10_ALWAYS_INLINE operator^(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 472 |
+
return Vectorized<double>{vec_x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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 4 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 5 |
+
#include <ATen/cpu/vec/vec256/vsx/vsx_helpers.h>
|
| 6 |
+
#include <sleef.h>
|
| 7 |
+
namespace at {
|
| 8 |
+
namespace vec {
|
| 9 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 10 |
+
|
| 11 |
+
inline namespace CPU_CAPABILITY {
|
| 12 |
+
|
| 13 |
+
template <>
|
| 14 |
+
class Vectorized<float> {
|
| 15 |
+
private:
|
| 16 |
+
union {
|
| 17 |
+
struct {
|
| 18 |
+
vfloat32 _vec0;
|
| 19 |
+
vfloat32 _vec1;
|
| 20 |
+
};
|
| 21 |
+
struct {
|
| 22 |
+
vbool32 _vecb0;
|
| 23 |
+
vbool32 _vecb1;
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
+
} __attribute__((__may_alias__));
|
| 27 |
+
|
| 28 |
+
public:
|
| 29 |
+
using value_type = float;
|
| 30 |
+
using vec_internal_type = vfloat32;
|
| 31 |
+
using vec_internal_mask_type = vbool32;
|
| 32 |
+
using size_type = int;
|
| 33 |
+
|
| 34 |
+
static constexpr size_type size() {
|
| 35 |
+
return 8;
|
| 36 |
+
}
|
| 37 |
+
Vectorized() {}
|
| 38 |
+
|
| 39 |
+
C10_ALWAYS_INLINE Vectorized(vfloat32 v) : _vec0{v}, _vec1{v} {}
|
| 40 |
+
C10_ALWAYS_INLINE Vectorized(vbool32 vmask) : _vecb0{vmask}, _vecb1{vmask} {}
|
| 41 |
+
C10_ALWAYS_INLINE Vectorized(vfloat32 v1, vfloat32 v2) : _vec0{v1}, _vec1{v2} {}
|
| 42 |
+
C10_ALWAYS_INLINE Vectorized(vbool32 v1, vbool32 v2) : _vecb0{v1}, _vecb1{v2} {}
|
| 43 |
+
C10_ALWAYS_INLINE Vectorized(float scalar)
|
| 44 |
+
: _vec0{vec_splats(scalar)}, _vec1{vec_splats(scalar)} {}
|
| 45 |
+
C10_ALWAYS_INLINE Vectorized(
|
| 46 |
+
float scalar1,
|
| 47 |
+
float scalar2,
|
| 48 |
+
float scalar3,
|
| 49 |
+
float scalar4,
|
| 50 |
+
float scalar5,
|
| 51 |
+
float scalar6,
|
| 52 |
+
float scalar7,
|
| 53 |
+
float scalar8)
|
| 54 |
+
: _vec0{vfloat32{scalar1, scalar2, scalar3, scalar4}},
|
| 55 |
+
_vec1{vfloat32{scalar5, scalar6, scalar7, scalar8}} {}
|
| 56 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec0() const {
|
| 57 |
+
return _vec0;
|
| 58 |
+
}
|
| 59 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec1() const {
|
| 60 |
+
return _vec1;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
template <int64_t mask>
|
| 64 |
+
static std::enable_if_t<blendChoice(mask) == 0, Vectorized<float>> C10_ALWAYS_INLINE
|
| 65 |
+
blend(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 66 |
+
return a;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
template <int64_t mask>
|
| 70 |
+
static std::enable_if_t<blendChoice(mask) == 1, Vectorized<float>> C10_ALWAYS_INLINE
|
| 71 |
+
blend(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 72 |
+
return b;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
template <int64_t mask>
|
| 76 |
+
static std::enable_if_t<blendChoice(mask) == 2, Vectorized<float>> C10_ALWAYS_INLINE
|
| 77 |
+
blend(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 78 |
+
return {b._vec0, a._vec1};
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
template <int64_t mask>
|
| 82 |
+
static std::enable_if_t<blendChoice(mask) == 3, Vectorized<float>> C10_ALWAYS_INLINE
|
| 83 |
+
blend(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 84 |
+
return {a._vec0, b._vec1};
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
template <int64_t mask>
|
| 88 |
+
static std::enable_if_t<blendChoice(mask) == 4, Vectorized<float>> C10_ALWAYS_INLINE
|
| 89 |
+
blend(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 90 |
+
const vbool32 mask_1st = VsxMask1(mask);
|
| 91 |
+
return {(vfloat32)vec_sel(a._vec0, b._vec0, mask_1st), a._vec1};
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
template <int64_t mask>
|
| 95 |
+
static std::enable_if_t<blendChoice(mask) == 5, Vectorized<float>> C10_ALWAYS_INLINE
|
| 96 |
+
blend(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 97 |
+
const vbool32 mask_1st = VsxMask1(mask);
|
| 98 |
+
return {(vfloat32)vec_sel(a._vec0, b._vec0, mask_1st), b._vec1};
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
template <int64_t mask>
|
| 102 |
+
static std::enable_if_t<blendChoice(mask) == 6, Vectorized<float>> C10_ALWAYS_INLINE
|
| 103 |
+
blend(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 104 |
+
const vbool32 mask_2nd = VsxMask2(mask);
|
| 105 |
+
// generated masks
|
| 106 |
+
return {a._vec0, (vfloat32)vec_sel(a._vec1, b._vec1, mask_2nd)};
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
template <int64_t mask>
|
| 110 |
+
static std::enable_if_t<blendChoice(mask) == 7, Vectorized<float>> C10_ALWAYS_INLINE
|
| 111 |
+
blend(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 112 |
+
const vbool32 mask_2nd = VsxMask2(mask);
|
| 113 |
+
// generated masks
|
| 114 |
+
return {b._vec0, (vfloat32)vec_sel(a._vec1, b._vec1, mask_2nd)};
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
template <int64_t mask>
|
| 118 |
+
static std::enable_if_t<blendChoice(mask) == 8, Vectorized<float>> C10_ALWAYS_INLINE
|
| 119 |
+
blend(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 120 |
+
const vbool32 mask_1st = VsxMask1(mask);
|
| 121 |
+
const vbool32 mask_2nd = VsxMask2(mask);
|
| 122 |
+
return {
|
| 123 |
+
(vfloat32)vec_sel(a._vec0, b._vec0, mask_1st),
|
| 124 |
+
(vfloat32)vec_sel(a._vec1, b._vec1, mask_2nd)};
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
static Vectorized<float> C10_ALWAYS_INLINE blendv(
|
| 128 |
+
const Vectorized<float>& a,
|
| 129 |
+
const Vectorized<float>& b,
|
| 130 |
+
const Vectorized<float>& mask) {
|
| 131 |
+
// the mask used here returned by comparision of vec256
|
| 132 |
+
// assuming this we can use the same mask directly with vec_sel
|
| 133 |
+
return {
|
| 134 |
+
vec_sel(a._vec0, b._vec0, mask._vecb0),
|
| 135 |
+
vec_sel(a._vec1, b._vec1, mask._vecb1)};
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
template <typename step_t>
|
| 139 |
+
static Vectorized<float> arange(float base = 0.f, step_t step = static_cast<step_t>(1)) {
|
| 140 |
+
return Vectorized<float>(
|
| 141 |
+
base,
|
| 142 |
+
base + step,
|
| 143 |
+
base + 2 * step,
|
| 144 |
+
base + 3 * step,
|
| 145 |
+
base + 4 * step,
|
| 146 |
+
base + 5 * step,
|
| 147 |
+
base + 6 * step,
|
| 148 |
+
base + 7 * step);
|
| 149 |
+
}
|
| 150 |
+
static Vectorized<float> set(
|
| 151 |
+
const Vectorized<float>& a,
|
| 152 |
+
const Vectorized<float>& b,
|
| 153 |
+
size_t count = size()) {
|
| 154 |
+
switch (count) {
|
| 155 |
+
case 0:
|
| 156 |
+
return a;
|
| 157 |
+
case 1:
|
| 158 |
+
return blend<1>(a, b);
|
| 159 |
+
case 2:
|
| 160 |
+
return blend<3>(a, b);
|
| 161 |
+
case 3:
|
| 162 |
+
return blend<7>(a, b);
|
| 163 |
+
case 4:
|
| 164 |
+
return blend<15>(a, b);
|
| 165 |
+
case 5:
|
| 166 |
+
return blend<31>(a, b);
|
| 167 |
+
case 6:
|
| 168 |
+
return blend<63>(a, b);
|
| 169 |
+
case 7:
|
| 170 |
+
return blend<127>(a, b);
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
return b;
|
| 174 |
+
}
|
| 175 |
+
static Vectorized<value_type> C10_ALWAYS_INLINE
|
| 176 |
+
loadu(const void* ptr, int count = size()) {
|
| 177 |
+
if (count == size()) {
|
| 178 |
+
return {
|
| 179 |
+
vec_vsx_ld(offset0, reinterpret_cast<const value_type*>(ptr)),
|
| 180 |
+
vec_vsx_ld(offset16, reinterpret_cast<const value_type*>(ptr))};
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
__at_align__ value_type tmp_values[size()] = {};
|
| 184 |
+
std::memcpy(tmp_values, ptr, std::min(count, size()) * sizeof(value_type));
|
| 185 |
+
|
| 186 |
+
return {vec_vsx_ld(offset0, tmp_values), vec_vsx_ld(offset16, tmp_values)};
|
| 187 |
+
}
|
| 188 |
+
void C10_ALWAYS_INLINE store(void* ptr, int count = size()) const {
|
| 189 |
+
if (count == size()) {
|
| 190 |
+
vec_vsx_st(_vec0, offset0, reinterpret_cast<value_type*>(ptr));
|
| 191 |
+
vec_vsx_st(_vec1, offset16, reinterpret_cast<value_type*>(ptr));
|
| 192 |
+
} else if (count > 0) {
|
| 193 |
+
__at_align__ value_type tmp_values[size()];
|
| 194 |
+
vec_vsx_st(_vec0, offset0, tmp_values);
|
| 195 |
+
vec_vsx_st(_vec1, offset16, tmp_values);
|
| 196 |
+
std::memcpy(
|
| 197 |
+
ptr, tmp_values, std::min(count, size()) * sizeof(value_type));
|
| 198 |
+
}
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
const float& operator[](int idx) const = delete;
|
| 202 |
+
float& operator[](int idx) = delete;
|
| 203 |
+
|
| 204 |
+
Vectorized<float> map(float (*const f)(float)) const {
|
| 205 |
+
Vectorized<float> ret;
|
| 206 |
+
for (int i = 0; i < size() / 2; i++) {
|
| 207 |
+
ret._vec0[i] = f(_vec0[i]);
|
| 208 |
+
}
|
| 209 |
+
for (int i = 0; i < size() / 2; i++) {
|
| 210 |
+
ret._vec1[i] = f(_vec1[i]);
|
| 211 |
+
}
|
| 212 |
+
return ret;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
Vectorized<float> mapbi(float (*const f)(float, float), const Vectorized<float>& other)
|
| 216 |
+
const {
|
| 217 |
+
Vectorized<float> ret;
|
| 218 |
+
for (int i = 0; i < size() / 2; i++) {
|
| 219 |
+
ret._vec0[i] = f(_vec0[i], other._vec0[i]);
|
| 220 |
+
}
|
| 221 |
+
for (int i = 0; i < size() / 2; i++) {
|
| 222 |
+
ret._vec1[i] = f(_vec1[i], other._vec1[i]);
|
| 223 |
+
}
|
| 224 |
+
return ret;
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
Vectorized<float> _nor() const {
|
| 228 |
+
return {vec_nor(_vec0, _vec0), vec_nor(_vec1, _vec1)};
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
Vectorized<float> isnan() const {
|
| 232 |
+
auto x = *this;
|
| 233 |
+
auto ret = (x == x);
|
| 234 |
+
return ret._nor();
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
bool has_inf_nan() const {
|
| 238 |
+
for (const auto i : c10::irange(size()/2)) {
|
| 239 |
+
if(_isnan(_vec0[i]) || _isinf(_vec0[i])) {
|
| 240 |
+
return true;
|
| 241 |
+
}
|
| 242 |
+
}
|
| 243 |
+
for (const auto i : c10::irange(size()/2)) {
|
| 244 |
+
if(_isnan(_vec1[i]) || _isinf(_vec1[i])) {
|
| 245 |
+
return true;
|
| 246 |
+
}
|
| 247 |
+
}
|
| 248 |
+
return false;
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
int zero_mask() const {
|
| 252 |
+
// returns an integer mask where all zero elements are translated to 1-bit
|
| 253 |
+
// and others are translated to 0-bit
|
| 254 |
+
//__m256 cmp = _mm256_cmp_ps(values, _mm256_set1_ps(0.0f), _CMP_EQ_OQ);
|
| 255 |
+
auto cmp = (*this == zero);
|
| 256 |
+
// return _mm256_movemask_ps(cmp);
|
| 257 |
+
// possible simulation //mask= lvsl ( 0 ) vbpermq( vec, mask <<5)
|
| 258 |
+
vuint64 result0 = vec_vbpermq((vuint8)cmp._vecb0, mask_zero_bits);
|
| 259 |
+
vuint64 result1 = vec_vbpermq((vuint8)cmp._vecb1, mask_zero_bits);
|
| 260 |
+
return (result0[1] >> 12 | (result1[1] >> 8));
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
Vectorized<float> C10_ALWAYS_INLINE abs() const {
|
| 264 |
+
return {vec_abs(_vec0), vec_abs(_vec1)};
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
Vectorized<float> C10_ALWAYS_INLINE acos() const {
|
| 268 |
+
return {Sleef_acosf4_u10(_vec0), Sleef_acosf4_u10(_vec1)};
|
| 269 |
+
}
|
| 270 |
+
Vectorized<float> C10_ALWAYS_INLINE acosh() const {
|
| 271 |
+
return {Sleef_acoshf4_u10(_vec0), Sleef_acoshf4_u10(_vec1)};
|
| 272 |
+
}
|
| 273 |
+
Vectorized<float> C10_ALWAYS_INLINE asin() const {
|
| 274 |
+
return {Sleef_asinf4_u10(_vec0), Sleef_asinf4_u10(_vec1)};
|
| 275 |
+
}
|
| 276 |
+
Vectorized<float> atan() const {
|
| 277 |
+
return {Sleef_atanf4_u10(_vec0), Sleef_atanf4_u10(_vec1)};
|
| 278 |
+
}
|
| 279 |
+
Vectorized<float> atanh() const {
|
| 280 |
+
return {Sleef_atanhf4_u10(_vec0), Sleef_atanhf4_u10(_vec1)};
|
| 281 |
+
}
|
| 282 |
+
Vectorized<float> atan2(const Vectorized<float>& b) const {
|
| 283 |
+
return {Sleef_atan2f4_u10(_vec0, b._vec0), Sleef_atan2f4_u10(_vec1, b._vec1)};
|
| 284 |
+
}
|
| 285 |
+
Vectorized<float> copysign(const Vectorized<float> &sign) const {
|
| 286 |
+
return {Sleef_copysignf4(_vec0, sign._vec0), Sleef_copysignf4(_vec1, sign._vec1)};
|
| 287 |
+
}
|
| 288 |
+
Vectorized<float> lgamma() const {
|
| 289 |
+
return {Sleef_lgammaf4_u10(_vec0), Sleef_lgammaf4_u10(_vec1)};
|
| 290 |
+
}
|
| 291 |
+
Vectorized<float> erf() const {
|
| 292 |
+
return {Sleef_erff4_u10(_vec0), Sleef_erff4_u10(_vec1)};
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
Vectorized<float> erfc() const {
|
| 296 |
+
return {Sleef_erfcf4_u15(_vec0), Sleef_erfcf4_u15(_vec1)};
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
Vectorized<float> erfinv() const {
|
| 300 |
+
return map(calc_erfinv);
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
Vectorized<float> angle() const {
|
| 304 |
+
auto tmp = blendv(
|
| 305 |
+
Vectorized<float>(0), Vectorized<float>(c10::pi<float>), *this < Vectorized<float>(0));
|
| 306 |
+
return blendv(tmp, *this, isnan());
|
| 307 |
+
}
|
| 308 |
+
Vectorized<float> real() const {
|
| 309 |
+
return *this;
|
| 310 |
+
}
|
| 311 |
+
Vectorized<float> imag() const {
|
| 312 |
+
return Vectorized<float>{0};
|
| 313 |
+
}
|
| 314 |
+
Vectorized<float> conj() const {
|
| 315 |
+
return *this;
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
Vectorized<float> C10_ALWAYS_INLINE exp() const {
|
| 319 |
+
return {Sleef_expf4_u10(_vec0), Sleef_expf4_u10(_vec1)};
|
| 320 |
+
}
|
| 321 |
+
Vectorized<float> C10_ALWAYS_INLINE exp2() const {
|
| 322 |
+
return {Sleef_exp2f4_u10(_vec0), Sleef_exp2f4_u10(_vec1)};
|
| 323 |
+
}
|
| 324 |
+
Vectorized<float> expm1() const {
|
| 325 |
+
return {Sleef_expm1f4_u10(_vec0), Sleef_expm1f4_u10(_vec1)};
|
| 326 |
+
}
|
| 327 |
+
Vectorized<float> C10_ALWAYS_INLINE exp_u20() const {
|
| 328 |
+
return exp();
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
Vectorized<float> C10_ALWAYS_INLINE log() const {
|
| 332 |
+
return {Sleef_logf4_u10(_vec0), Sleef_logf4_u10(_vec1)};
|
| 333 |
+
}
|
| 334 |
+
Vectorized<float> C10_ALWAYS_INLINE log10() const {
|
| 335 |
+
return {Sleef_log10f4_u10(_vec0), Sleef_log10f4_u10(_vec1)};
|
| 336 |
+
}
|
| 337 |
+
Vectorized<float> C10_ALWAYS_INLINE log1p() const {
|
| 338 |
+
return {Sleef_log1pf4_u10(_vec0), Sleef_log1pf4_u10(_vec1)};
|
| 339 |
+
}
|
| 340 |
+
Vectorized<float> C10_ALWAYS_INLINE log2() const {
|
| 341 |
+
return {Sleef_log2f4_u10(_vec0), Sleef_log2f4_u10(_vec1)};
|
| 342 |
+
}
|
| 343 |
+
Vectorized<float> C10_ALWAYS_INLINE ceil() const {
|
| 344 |
+
return {vec_ceil(_vec0), vec_ceil(_vec1)};
|
| 345 |
+
}
|
| 346 |
+
Vectorized<float> C10_ALWAYS_INLINE cos() const {
|
| 347 |
+
return {Sleef_cosf4_u10(_vec0), Sleef_cosf4_u10(_vec1)};
|
| 348 |
+
}
|
| 349 |
+
Vectorized<float> C10_ALWAYS_INLINE cosh() const {
|
| 350 |
+
return {Sleef_coshf4_u10(_vec0), Sleef_coshf4_u10(_vec1)};
|
| 351 |
+
}
|
| 352 |
+
Vectorized<float> C10_ALWAYS_INLINE floor() const {
|
| 353 |
+
return {vec_floor(_vec0), vec_floor(_vec1)};
|
| 354 |
+
}
|
| 355 |
+
Vectorized<float> C10_ALWAYS_INLINE neg() const {
|
| 356 |
+
return {vec_neg(_vec0), vec_neg(_vec1)};
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
Vectorized<float> C10_ALWAYS_INLINE round() const {
|
| 360 |
+
return {vec_round(_vec0), vec_round(_vec1)};
|
| 361 |
+
}
|
| 362 |
+
Vectorized<float> C10_ALWAYS_INLINE sin() const {
|
| 363 |
+
return {Sleef_sinf4_u10(_vec0), Sleef_sinf4_u10(_vec1)};
|
| 364 |
+
}
|
| 365 |
+
Vectorized<float> C10_ALWAYS_INLINE sinh() const {
|
| 366 |
+
return {Sleef_sinhf4_u10(_vec0), Sleef_sinhf4_u10(_vec1)};
|
| 367 |
+
}
|
| 368 |
+
Vectorized<float> C10_ALWAYS_INLINE tan() const {
|
| 369 |
+
return {Sleef_tanf4_u10(_vec0), Sleef_tanf4_u10(_vec1)};
|
| 370 |
+
}
|
| 371 |
+
Vectorized<float> C10_ALWAYS_INLINE tanh() const {
|
| 372 |
+
return {Sleef_tanhf4_u10(_vec0), Sleef_tanhf4_u10(_vec1)};
|
| 373 |
+
}
|
| 374 |
+
Vectorized<float> C10_ALWAYS_INLINE trunc() const {
|
| 375 |
+
return {vec_trunc(_vec0), vec_trunc(_vec1)};
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
Vectorized<float> C10_ALWAYS_INLINE frac() const {
|
| 379 |
+
return *this - trunc();
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
Vectorized<float> C10_ALWAYS_INLINE sqrt() const {
|
| 383 |
+
return {vec_sqrt(_vec0), vec_sqrt(_vec1)};
|
| 384 |
+
}
|
| 385 |
+
Vectorized<float> C10_ALWAYS_INLINE reciprocal() const {
|
| 386 |
+
return Vectorized<float>(one) / (*this);
|
| 387 |
+
}
|
| 388 |
+
Vectorized<float> C10_ALWAYS_INLINE rsqrt() const {
|
| 389 |
+
return sqrt().reciprocal();
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
Vectorized<float> C10_ALWAYS_INLINE pow(const Vectorized<float>& exp) const {
|
| 393 |
+
return {Sleef_powf4_u10(_vec0, exp._vec0), Sleef_powf4_u10(_vec1, exp._vec1)};
|
| 394 |
+
}
|
| 395 |
+
|
| 396 |
+
Vectorized<float> fmod(const Vectorized<float>& b) const {
|
| 397 |
+
return {Sleef_fmodf4(_vec0, b._vec0),Sleef_fmodf4(_vec1, b._vec1)};
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
Vectorized<float> hypot(const Vectorized<float>& b) const {
|
| 401 |
+
return {Sleef_hypotf4_u05(_vec0, b._vec0), Sleef_hypotf4_u05(_vec1, b._vec1)};
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
Vectorized<float> nextafter(const Vectorized<float>& b) const {
|
| 405 |
+
return {Sleef_nextafterf4(_vec0, b._vec0), Sleef_nextafterf4(_vec1, b._vec1)};
|
| 406 |
+
}
|
| 407 |
+
|
| 408 |
+
Vectorized<float> igamma(const Vectorized<float>& x) const {
|
| 409 |
+
return mapbi(calc_igamma, x);
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
Vectorized<float> igammac(const Vectorized<float>& x) const {
|
| 413 |
+
return mapbi(calc_igammac, x);
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
Vectorized<float> i0() const {
|
| 417 |
+
return map(calc_i0);
|
| 418 |
+
}
|
| 419 |
+
|
| 420 |
+
Vectorized<float> i0e() const {
|
| 421 |
+
return map(calc_i0e);
|
| 422 |
+
}
|
| 423 |
+
|
| 424 |
+
Vectorized<float> digamma() const {
|
| 425 |
+
return map(calc_digamma);
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
+
DEFINE_MEMBER_OP(operator==, float, vec_cmpeq)
|
| 429 |
+
DEFINE_MEMBER_OP(operator!=, float, vec_cmpne)
|
| 430 |
+
DEFINE_MEMBER_OP(operator<, float, vec_cmplt)
|
| 431 |
+
DEFINE_MEMBER_OP(operator<=, float, vec_cmple)
|
| 432 |
+
DEFINE_MEMBER_OP(operator>, float, vec_cmpgt)
|
| 433 |
+
DEFINE_MEMBER_OP(operator>=, float, vec_cmpge)
|
| 434 |
+
DEFINE_MEMBER_OP_AND_ONE(eq, float, vec_cmpeq)
|
| 435 |
+
DEFINE_MEMBER_OP_AND_ONE(ne, float, vec_cmpne)
|
| 436 |
+
DEFINE_MEMBER_OP_AND_ONE(lt, float, vec_cmplt)
|
| 437 |
+
DEFINE_MEMBER_OP_AND_ONE(le, float, vec_cmple)
|
| 438 |
+
DEFINE_MEMBER_OP_AND_ONE(gt, float, vec_cmpgt)
|
| 439 |
+
DEFINE_MEMBER_OP_AND_ONE(ge, float, vec_cmpge)
|
| 440 |
+
DEFINE_MEMBER_OP(operator+, float, vec_add)
|
| 441 |
+
DEFINE_MEMBER_OP(operator-, float, vec_sub)
|
| 442 |
+
DEFINE_MEMBER_OP(operator*, float, vec_mul)
|
| 443 |
+
DEFINE_MEMBER_OP(operator/, float, vec_div)
|
| 444 |
+
DEFINE_MEMBER_OP(maximum, float, vec_max_nan2)
|
| 445 |
+
DEFINE_MEMBER_OP(minimum, float, vec_min_nan2)
|
| 446 |
+
DEFINE_MEMBER_OP(operator&, float, vec_and)
|
| 447 |
+
DEFINE_MEMBER_OP(operator|, float, vec_or)
|
| 448 |
+
DEFINE_MEMBER_OP(operator^, float, vec_xor)
|
| 449 |
+
DEFINE_MEMBER_TERNARY_OP(madd, float, vec_madd)
|
| 450 |
+
};
|
| 451 |
+
|
| 452 |
+
template <>
|
| 453 |
+
Vectorized<float> inline maximum(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 454 |
+
return a.maximum(b);
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
template <>
|
| 458 |
+
Vectorized<float> inline minimum(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 459 |
+
return a.minimum(b);
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
template <>
|
| 463 |
+
Vectorized<float> C10_ALWAYS_INLINE operator+(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 464 |
+
return Vectorized<float>{vec_add(a.vec0(), b.vec0()), vec_add(a.vec1(), b.vec1())};
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
template <>
|
| 468 |
+
Vectorized<float> C10_ALWAYS_INLINE operator-(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 469 |
+
return Vectorized<float>{vec_sub(a.vec0(), b.vec0()), vec_sub(a.vec1(), b.vec1())};
|
| 470 |
+
}
|
| 471 |
+
|
| 472 |
+
template <>
|
| 473 |
+
Vectorized<float> C10_ALWAYS_INLINE operator*(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 474 |
+
return Vectorized<float>{vec_mul(a.vec0(), b.vec0()), vec_mul(a.vec1(), b.vec1())};
|
| 475 |
+
}
|
| 476 |
+
|
| 477 |
+
template <>
|
| 478 |
+
Vectorized<float> C10_ALWAYS_INLINE operator/(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 479 |
+
return Vectorized<float>{vec_div(a.vec0(), b.vec0()), vec_div(a.vec1(), b.vec1())};
|
| 480 |
+
}
|
| 481 |
+
|
| 482 |
+
template <>
|
| 483 |
+
Vectorized<float> C10_ALWAYS_INLINE operator&(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 484 |
+
return Vectorized<float>{vec_and(a.vec0(), b.vec0()), vec_and(a.vec1(), b.vec1())};
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
template <>
|
| 488 |
+
Vectorized<float> C10_ALWAYS_INLINE operator|(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 489 |
+
return Vectorized<float>{vec_or(a.vec0(), b.vec0()), vec_or(a.vec1(), b.vec1())};
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
template <>
|
| 493 |
+
Vectorized<float> C10_ALWAYS_INLINE operator^(const Vectorized<float>& a, const Vectorized<float>& b) {
|
| 494 |
+
return Vectorized<float>{vec_xor(a.vec0(), b.vec0()), vec_xor(a.vec1(), b.vec1())};
|
| 495 |
+
}
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 4 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 5 |
+
#include <ATen/cpu/vec/vec256/vsx/vsx_helpers.h>
|
| 6 |
+
namespace at {
|
| 7 |
+
namespace vec {
|
| 8 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 9 |
+
inline namespace CPU_CAPABILITY {
|
| 10 |
+
|
| 11 |
+
template <>
|
| 12 |
+
class Vectorized<int16_t> {
|
| 13 |
+
private:
|
| 14 |
+
union {
|
| 15 |
+
struct {
|
| 16 |
+
vint16 _vec0;
|
| 17 |
+
vint16 _vec1;
|
| 18 |
+
};
|
| 19 |
+
struct {
|
| 20 |
+
vbool16 _vecb0;
|
| 21 |
+
vbool16 _vecb1;
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
} __attribute__((__may_alias__));
|
| 25 |
+
|
| 26 |
+
public:
|
| 27 |
+
using value_type = int16_t;
|
| 28 |
+
using vec_internal_type = vint16;
|
| 29 |
+
using vec_internal_mask_type = vbool16;
|
| 30 |
+
using size_type = int;
|
| 31 |
+
static constexpr size_type size() {
|
| 32 |
+
return 16;
|
| 33 |
+
}
|
| 34 |
+
Vectorized() {}
|
| 35 |
+
C10_ALWAYS_INLINE Vectorized(vint16 v) : _vec0{v}, _vec1{v} {}
|
| 36 |
+
C10_ALWAYS_INLINE Vectorized(vbool16 vmask) : _vecb0{vmask}, _vecb1{vmask} {}
|
| 37 |
+
C10_ALWAYS_INLINE Vectorized(vint16 v1, vint16 v2) : _vec0{v1}, _vec1{v2} {}
|
| 38 |
+
C10_ALWAYS_INLINE Vectorized(vbool16 v1, vbool16 v2) : _vecb0{v1}, _vecb1{v2} {}
|
| 39 |
+
C10_ALWAYS_INLINE Vectorized(int16_t scalar)
|
| 40 |
+
: _vec0{vec_splats(scalar)}, _vec1{vec_splats(scalar)} {}
|
| 41 |
+
|
| 42 |
+
C10_ALWAYS_INLINE Vectorized(
|
| 43 |
+
int16_t scalar1,
|
| 44 |
+
int16_t scalar2,
|
| 45 |
+
int16_t scalar3,
|
| 46 |
+
int16_t scalar4,
|
| 47 |
+
int16_t scalar5,
|
| 48 |
+
int16_t scalar6,
|
| 49 |
+
int16_t scalar7,
|
| 50 |
+
int16_t scalar8,
|
| 51 |
+
int16_t scalar9,
|
| 52 |
+
int16_t scalar10,
|
| 53 |
+
int16_t scalar11,
|
| 54 |
+
int16_t scalar12,
|
| 55 |
+
int16_t scalar13,
|
| 56 |
+
int16_t scalar14,
|
| 57 |
+
int16_t scalar15,
|
| 58 |
+
int16_t scalar16)
|
| 59 |
+
: _vec0{vint16{
|
| 60 |
+
scalar1,
|
| 61 |
+
scalar2,
|
| 62 |
+
scalar3,
|
| 63 |
+
scalar4,
|
| 64 |
+
scalar5,
|
| 65 |
+
scalar6,
|
| 66 |
+
scalar7,
|
| 67 |
+
scalar8}},
|
| 68 |
+
_vec1{vint16{
|
| 69 |
+
scalar9,
|
| 70 |
+
scalar10,
|
| 71 |
+
scalar11,
|
| 72 |
+
scalar12,
|
| 73 |
+
scalar13,
|
| 74 |
+
scalar14,
|
| 75 |
+
scalar15,
|
| 76 |
+
scalar16}} {}
|
| 77 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec0() const {
|
| 78 |
+
return _vec0;
|
| 79 |
+
}
|
| 80 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec1() const {
|
| 81 |
+
return _vec1;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
template <uint64_t mask>
|
| 85 |
+
static std::enable_if_t<mask == 0, Vectorized<int16_t>> C10_ALWAYS_INLINE
|
| 86 |
+
blend(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 87 |
+
return a;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
template <uint64_t mask>
|
| 91 |
+
static std::enable_if_t<(mask & 65535) == 65535, Vectorized<int16_t>>
|
| 92 |
+
C10_ALWAYS_INLINE blend(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 93 |
+
return b;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
template <uint64_t mask>
|
| 97 |
+
static std::enable_if_t<mask == 255, Vectorized<int16_t>> C10_ALWAYS_INLINE
|
| 98 |
+
blend(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 99 |
+
return {b._vec0, a._vec1};
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
template <uint64_t mask>
|
| 103 |
+
static std::enable_if_t<(mask > 0 && mask < 255), Vectorized<int16_t>>
|
| 104 |
+
C10_ALWAYS_INLINE blend(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 105 |
+
constexpr int16_t g0 = (mask & 1) * 0xffff;
|
| 106 |
+
constexpr int16_t g1 = ((mask & 2) >> 1) * 0xffff;
|
| 107 |
+
constexpr int16_t g2 = ((mask & 4) >> 2) * 0xffff;
|
| 108 |
+
constexpr int16_t g3 = ((mask & 8) >> 3) * 0xffff;
|
| 109 |
+
constexpr int16_t g4 = ((mask & 16) >> 4) * 0xffff;
|
| 110 |
+
constexpr int16_t g5 = ((mask & 32) >> 5) * 0xffff;
|
| 111 |
+
constexpr int16_t g6 = ((mask & 64) >> 6) * 0xffff;
|
| 112 |
+
constexpr int16_t g7 = ((mask & 128) >> 7) * 0xffff;
|
| 113 |
+
const vint16 mask_1st = vint16{g0, g1, g2, g3, g4, g5, g6, g7};
|
| 114 |
+
|
| 115 |
+
return {(vint16)vec_sel(a._vec0, b._vec0, (vbool16)mask_1st), a._vec1};
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
template <uint64_t mask>
|
| 119 |
+
static std::enable_if_t<
|
| 120 |
+
(mask > 255 && (mask & 65535) != 65535 && ((mask & 255) == 255)),
|
| 121 |
+
Vectorized<int16_t>>
|
| 122 |
+
C10_ALWAYS_INLINE blend(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 123 |
+
constexpr int16_t g0_2 = (mask & 1) * 0xffff;
|
| 124 |
+
constexpr int16_t g1_2 = ((mask & 2) >> 1) * 0xffff;
|
| 125 |
+
constexpr int16_t g2_2 = ((mask & 4) >> 2) * 0xffff;
|
| 126 |
+
constexpr int16_t g3_2 = ((mask & 8) >> 3) * 0xffff;
|
| 127 |
+
constexpr int16_t g4_2 = ((mask & 16) >> 4) * 0xffff;
|
| 128 |
+
constexpr int16_t g5_2 = ((mask & 32) >> 5) * 0xffff;
|
| 129 |
+
constexpr int16_t g6_2 = ((mask & 64) >> 6) * 0xffff;
|
| 130 |
+
constexpr int16_t g7_2 = ((mask & 128) >> 7) * 0xffff;
|
| 131 |
+
|
| 132 |
+
const vint16 mask_2nd =
|
| 133 |
+
vint16{g0_2, g1_2, g2_2, g3_2, g4_2, g5_2, g6_2, g7_2};
|
| 134 |
+
// generated masks
|
| 135 |
+
return {b._vec0, (vint16)vec_sel(a._vec1, b._vec1, (vbool16)mask_2nd)};
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
template <uint64_t mask>
|
| 139 |
+
static std::enable_if_t<
|
| 140 |
+
(mask > 255 && ((mask & 65535) != 65535) && ((mask & 255) == 0)),
|
| 141 |
+
Vectorized<int16_t>>
|
| 142 |
+
C10_ALWAYS_INLINE blend(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 143 |
+
constexpr int16_t mask2 = (mask & 65535) >> 16;
|
| 144 |
+
constexpr int16_t g0_2 = (mask & 1) * 0xffff;
|
| 145 |
+
constexpr int16_t g1_2 = ((mask & 2) >> 1) * 0xffff;
|
| 146 |
+
constexpr int16_t g2_2 = ((mask & 4) >> 2) * 0xffff;
|
| 147 |
+
constexpr int16_t g3_2 = ((mask & 8) >> 3) * 0xffff;
|
| 148 |
+
constexpr int16_t g4_2 = ((mask & 16) >> 4) * 0xffff;
|
| 149 |
+
constexpr int16_t g5_2 = ((mask & 32) >> 5) * 0xffff;
|
| 150 |
+
constexpr int16_t g6_2 = ((mask & 64) >> 6) * 0xffff;
|
| 151 |
+
constexpr int16_t g7_2 = ((mask & 128) >> 7) * 0xffff;
|
| 152 |
+
|
| 153 |
+
const vint16 mask_2nd =
|
| 154 |
+
vint16{g0_2, g1_2, g2_2, g3_2, g4_2, g5_2, g6_2, g7_2};
|
| 155 |
+
// generated masks
|
| 156 |
+
return {a, (vint16)vec_sel(a._vec1, b._vec1, (vbool16)mask_2nd)};
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
template <uint64_t mask>
|
| 160 |
+
static std::enable_if_t<
|
| 161 |
+
(mask > 255 && ((mask & 65535) != 65535) && ((mask & 255) != 0) &&
|
| 162 |
+
((mask & 255) != 255)),
|
| 163 |
+
Vectorized<int16_t>>
|
| 164 |
+
C10_ALWAYS_INLINE blend(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 165 |
+
constexpr int16_t g0 = (mask & 1) * 0xffff;
|
| 166 |
+
constexpr int16_t g1 = ((mask & 2) >> 1) * 0xffff;
|
| 167 |
+
constexpr int16_t g2 = ((mask & 4) >> 2) * 0xffff;
|
| 168 |
+
constexpr int16_t g3 = ((mask & 8) >> 3) * 0xffff;
|
| 169 |
+
constexpr int16_t g4 = ((mask & 16) >> 4) * 0xffff;
|
| 170 |
+
constexpr int16_t g5 = ((mask & 32) >> 5) * 0xffff;
|
| 171 |
+
constexpr int16_t g6 = ((mask & 64) >> 6) * 0xffff;
|
| 172 |
+
constexpr int16_t g7 = ((mask & 128) >> 7) * 0xffff;
|
| 173 |
+
constexpr int16_t mask2 = (mask & 65535) >> 16;
|
| 174 |
+
constexpr int16_t g0_2 = (mask & 1) * 0xffff;
|
| 175 |
+
constexpr int16_t g1_2 = ((mask & 2) >> 1) * 0xffff;
|
| 176 |
+
constexpr int16_t g2_2 = ((mask & 4) >> 2) * 0xffff;
|
| 177 |
+
constexpr int16_t g3_2 = ((mask & 8) >> 3) * 0xffff;
|
| 178 |
+
constexpr int16_t g4_2 = ((mask & 16) >> 4) * 0xffff;
|
| 179 |
+
constexpr int16_t g5_2 = ((mask & 32) >> 5) * 0xffff;
|
| 180 |
+
constexpr int16_t g6_2 = ((mask & 64) >> 6) * 0xffff;
|
| 181 |
+
constexpr int16_t g7_2 = ((mask & 128) >> 7) * 0xffff;
|
| 182 |
+
|
| 183 |
+
const vint16 mask_1st = vint16{g0, g1, g2, g3, g4, g5, g6, g7};
|
| 184 |
+
const vint16 mask_2nd =
|
| 185 |
+
vint16{g0_2, g1_2, g2_2, g3_2, g4_2, g5_2, g6_2, g7_2};
|
| 186 |
+
// generated masks
|
| 187 |
+
return {
|
| 188 |
+
(vint16)vec_sel(a._vec0, b._vec0, (vbool16)mask_1st),
|
| 189 |
+
(vint16)vec_sel(a._vec1, b._vec1, (vbool16)mask_2nd)};
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
static Vectorized<int16_t> C10_ALWAYS_INLINE blendv(
|
| 193 |
+
const Vectorized<int16_t>& a,
|
| 194 |
+
const Vectorized<int16_t>& b,
|
| 195 |
+
const Vectorized<int16_t>& mask) {
|
| 196 |
+
// the mask used here returned by comparision of vec256
|
| 197 |
+
// assuming this we can use the same mask directly with vec_sel
|
| 198 |
+
// warning intel style mask will not work properly
|
| 199 |
+
return {
|
| 200 |
+
vec_sel(a._vec0, b._vec0, mask._vecb0),
|
| 201 |
+
vec_sel(a._vec1, b._vec1, mask._vecb1)};
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
template <typename step_t>
|
| 205 |
+
static Vectorized<int16_t> arange(int16_t base = 0, step_t step = static_cast<step_t>(1)) {
|
| 206 |
+
return Vectorized<int16_t>(
|
| 207 |
+
base,
|
| 208 |
+
base + step,
|
| 209 |
+
base + 2 * step,
|
| 210 |
+
base + 3 * step,
|
| 211 |
+
base + 4 * step,
|
| 212 |
+
base + 5 * step,
|
| 213 |
+
base + 6 * step,
|
| 214 |
+
base + 7 * step,
|
| 215 |
+
base + 8 * step,
|
| 216 |
+
base + 9 * step,
|
| 217 |
+
base + 10 * step,
|
| 218 |
+
base + 11 * step,
|
| 219 |
+
base + 12 * step,
|
| 220 |
+
base + 13 * step,
|
| 221 |
+
base + 14 * step,
|
| 222 |
+
base + 15 * step);
|
| 223 |
+
}
|
| 224 |
+
static Vectorized<int16_t> set(
|
| 225 |
+
const Vectorized<int16_t>& a,
|
| 226 |
+
const Vectorized<int16_t>& b,
|
| 227 |
+
size_t count = size()) {
|
| 228 |
+
switch (count) {
|
| 229 |
+
case 0:
|
| 230 |
+
return a;
|
| 231 |
+
case 1:
|
| 232 |
+
return blend<1>(a, b);
|
| 233 |
+
case 2:
|
| 234 |
+
return blend<3>(a, b);
|
| 235 |
+
case 3:
|
| 236 |
+
return blend<7>(a, b);
|
| 237 |
+
case 4:
|
| 238 |
+
return blend<15>(a, b);
|
| 239 |
+
case 5:
|
| 240 |
+
return blend<31>(a, b);
|
| 241 |
+
case 6:
|
| 242 |
+
return blend<63>(a, b);
|
| 243 |
+
case 7:
|
| 244 |
+
return blend<127>(a, b);
|
| 245 |
+
case 8:
|
| 246 |
+
return blend<255>(a, b);
|
| 247 |
+
case 9:
|
| 248 |
+
return blend<511>(a, b);
|
| 249 |
+
case 10:
|
| 250 |
+
return blend<1023>(a, b);
|
| 251 |
+
case 11:
|
| 252 |
+
return blend<2047>(a, b);
|
| 253 |
+
case 12:
|
| 254 |
+
return blend<4095>(a, b);
|
| 255 |
+
case 13:
|
| 256 |
+
return blend<8191>(a, b);
|
| 257 |
+
case 14:
|
| 258 |
+
return blend<16383>(a, b);
|
| 259 |
+
case 15:
|
| 260 |
+
return blend<32767>(a, b);
|
| 261 |
+
}
|
| 262 |
+
return b;
|
| 263 |
+
}
|
| 264 |
+
static Vectorized<value_type> C10_ALWAYS_INLINE
|
| 265 |
+
loadu(const void* ptr, int count = size()) {
|
| 266 |
+
if (count == size()) {
|
| 267 |
+
return {
|
| 268 |
+
vec_vsx_ld(offset0, reinterpret_cast<const value_type*>(ptr)),
|
| 269 |
+
vec_vsx_ld(offset16, reinterpret_cast<const value_type*>(ptr))};
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
__at_align__ value_type tmp_values[size()] = {};
|
| 273 |
+
std::memcpy(tmp_values, ptr, std::min(count, size()) * sizeof(value_type));
|
| 274 |
+
|
| 275 |
+
return {vec_vsx_ld(offset0, tmp_values), vec_vsx_ld(offset16, tmp_values)};
|
| 276 |
+
}
|
| 277 |
+
void C10_ALWAYS_INLINE store(void* ptr, int count = size()) const {
|
| 278 |
+
if (count == size()) {
|
| 279 |
+
vec_vsx_st(_vec0, offset0, reinterpret_cast<value_type*>(ptr));
|
| 280 |
+
vec_vsx_st(_vec1, offset16, reinterpret_cast<value_type*>(ptr));
|
| 281 |
+
} else if (count > 0) {
|
| 282 |
+
__at_align__ value_type tmp_values[size()];
|
| 283 |
+
vec_vsx_st(_vec0, offset0, tmp_values);
|
| 284 |
+
vec_vsx_st(_vec1, offset16, tmp_values);
|
| 285 |
+
std::memcpy(ptr, tmp_values, std::min(count, size()) * sizeof(value_type));
|
| 286 |
+
}
|
| 287 |
+
}
|
| 288 |
+
const int16_t& operator[](int idx) const = delete;
|
| 289 |
+
int16_t& operator[](int idx) = delete;
|
| 290 |
+
|
| 291 |
+
Vectorized<int16_t> angle() const {
|
| 292 |
+
return blendv(
|
| 293 |
+
Vectorized<int16_t>(0), Vectorized<int16_t>(c10::pi<int16_t>), *this < Vectorized<int16_t>(0));
|
| 294 |
+
}
|
| 295 |
+
Vectorized<int16_t> real() const {
|
| 296 |
+
return *this;
|
| 297 |
+
}
|
| 298 |
+
Vectorized<int16_t> imag() const {
|
| 299 |
+
return Vectorized<int16_t>{0};
|
| 300 |
+
}
|
| 301 |
+
Vectorized<int16_t> conj() const {
|
| 302 |
+
return *this;
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
Vectorized<int16_t> C10_ALWAYS_INLINE abs() const {
|
| 306 |
+
return {vec_abs(_vec0), vec_abs(_vec1)};
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
Vectorized<int16_t> C10_ALWAYS_INLINE neg() const {
|
| 310 |
+
return {vec_neg(_vec0), vec_neg(_vec1)};
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
DEFINE_MEMBER_UNARY_OP(operator~, int16_t, vec_not)
|
| 314 |
+
DEFINE_MEMBER_OP(operator==, int16_t, vec_cmpeq)
|
| 315 |
+
DEFINE_MEMBER_OP(operator!=, int16_t, vec_cmpne)
|
| 316 |
+
DEFINE_MEMBER_OP(operator<, int16_t, vec_cmplt)
|
| 317 |
+
DEFINE_MEMBER_OP(operator<=, int16_t, vec_cmple)
|
| 318 |
+
DEFINE_MEMBER_OP(operator>, int16_t, vec_cmpgt)
|
| 319 |
+
DEFINE_MEMBER_OP(operator>=, int16_t, vec_cmpge)
|
| 320 |
+
DEFINE_MEMBER_OP_AND_ONE(eq, int16_t, vec_cmpeq)
|
| 321 |
+
DEFINE_MEMBER_OP_AND_ONE(ne, int16_t, vec_cmpne)
|
| 322 |
+
DEFINE_MEMBER_OP_AND_ONE(lt, int16_t, vec_cmplt)
|
| 323 |
+
DEFINE_MEMBER_OP_AND_ONE(le, int16_t, vec_cmple)
|
| 324 |
+
DEFINE_MEMBER_OP_AND_ONE(gt, int16_t, vec_cmpgt)
|
| 325 |
+
DEFINE_MEMBER_OP_AND_ONE(ge, int16_t, vec_cmpge)
|
| 326 |
+
DEFINE_MEMBER_OP(operator+, int16_t, vec_add)
|
| 327 |
+
DEFINE_MEMBER_OP(operator-, int16_t, vec_sub)
|
| 328 |
+
DEFINE_MEMBER_OP(operator*, int16_t, vec_mul)
|
| 329 |
+
DEFINE_MEMBER_EMULATE_BINARY_OP(operator/, int16_t, /)
|
| 330 |
+
DEFINE_MEMBER_OP(maximum, int16_t, vec_max)
|
| 331 |
+
DEFINE_MEMBER_OP(minimum, int16_t, vec_min)
|
| 332 |
+
DEFINE_MEMBER_OP(operator&, int16_t, vec_and)
|
| 333 |
+
DEFINE_MEMBER_OP(operator|, int16_t, vec_or)
|
| 334 |
+
DEFINE_MEMBER_OP(operator^, int16_t, vec_xor)
|
| 335 |
+
};
|
| 336 |
+
|
| 337 |
+
template <>
|
| 338 |
+
Vectorized<int16_t> inline operator<<(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 339 |
+
vuint16 shift_vec0 = reinterpret_cast<vuint16>(b.vec0());
|
| 340 |
+
vuint16 shift_vec1 = reinterpret_cast<vuint16>(b.vec1());
|
| 341 |
+
return Vectorized<int16_t>{vec_sl(a.vec0(), shift_vec0), vec_sl(a.vec1(), shift_vec1)};
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
template <>
|
| 345 |
+
Vectorized<int16_t> inline operator>>(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 346 |
+
vuint16 shift_vec0 = reinterpret_cast<vuint16>(b.vec0());
|
| 347 |
+
vuint16 shift_vec1 = reinterpret_cast<vuint16>(b.vec1()) ;
|
| 348 |
+
return Vectorized<int16_t>{vec_sr(a.vec0(), shift_vec0), vec_sr(a.vec1(), shift_vec1)};
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
template <>
|
| 352 |
+
Vectorized<int16_t> inline maximum(
|
| 353 |
+
const Vectorized<int16_t>& a,
|
| 354 |
+
const Vectorized<int16_t>& b) {
|
| 355 |
+
return a.maximum(b);
|
| 356 |
+
}
|
| 357 |
+
|
| 358 |
+
template <>
|
| 359 |
+
Vectorized<int16_t> inline minimum(
|
| 360 |
+
const Vectorized<int16_t>& a,
|
| 361 |
+
const Vectorized<int16_t>& b) {
|
| 362 |
+
return a.minimum(b);
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
template <>
|
| 366 |
+
Vectorized<int16_t> C10_ALWAYS_INLINE operator+(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 367 |
+
return Vectorized<int16_t>{vec_add(a.vec0(), b.vec0()), vec_add(a.vec1(), b.vec1())};
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
template <>
|
| 371 |
+
Vectorized<int16_t> C10_ALWAYS_INLINE operator-(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 372 |
+
return Vectorized<int16_t>{vec_sub(a.vec0(), b.vec0()), vec_sub(a.vec1(), b.vec1())};
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
template <>
|
| 376 |
+
Vectorized<int16_t> C10_ALWAYS_INLINE operator*(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 377 |
+
return Vectorized<int16_t>{vec_mul(a.vec0(), b.vec0()), vec_mul(a.vec1(), b.vec1())};
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
template <>
|
| 381 |
+
Vectorized<int16_t> C10_ALWAYS_INLINE operator/(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 382 |
+
return Vectorized<int16_t>{a.vec0()/b.vec0(), a.vec1()/b.vec1()};
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
template <>
|
| 386 |
+
Vectorized<int16_t> C10_ALWAYS_INLINE operator&(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 387 |
+
return Vectorized<int16_t>{vec_and(a.vec0(), b.vec0()), vec_and(a.vec1(), b.vec1())};
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
template <>
|
| 391 |
+
Vectorized<int16_t> C10_ALWAYS_INLINE operator|(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 392 |
+
return Vectorized<int16_t>{vec_or(a.vec0(), b.vec0()), vec_or(a.vec1(), b.vec1())};
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
template <>
|
| 396 |
+
Vectorized<int16_t> C10_ALWAYS_INLINE operator^(const Vectorized<int16_t>& a, const Vectorized<int16_t>& b) {
|
| 397 |
+
return Vectorized<int16_t>{vec_xor(a.vec0(), b.vec0()), vec_xor(a.vec1(), b.vec1())};
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
} // namespace
|
| 401 |
+
} // namespace vec
|
| 402 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_int32_vsx.h
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 4 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 5 |
+
#include <ATen/cpu/vec/vec256/vsx/vsx_helpers.h>
|
| 6 |
+
namespace at {
|
| 7 |
+
namespace vec {
|
| 8 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 9 |
+
inline namespace CPU_CAPABILITY {
|
| 10 |
+
|
| 11 |
+
template <>
|
| 12 |
+
class Vectorized<int32_t> {
|
| 13 |
+
private:
|
| 14 |
+
union {
|
| 15 |
+
struct {
|
| 16 |
+
vint32 _vec0;
|
| 17 |
+
vint32 _vec1;
|
| 18 |
+
};
|
| 19 |
+
struct {
|
| 20 |
+
vbool32 _vecb0;
|
| 21 |
+
vbool32 _vecb1;
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
} __attribute__((__may_alias__));
|
| 25 |
+
|
| 26 |
+
public:
|
| 27 |
+
using value_type = int32_t;
|
| 28 |
+
using vec_internal_type = vint32;
|
| 29 |
+
using vec_internal_mask_type = vbool32;
|
| 30 |
+
using size_type = int;
|
| 31 |
+
static constexpr size_type size() {
|
| 32 |
+
return 8;
|
| 33 |
+
}
|
| 34 |
+
Vectorized() {}
|
| 35 |
+
C10_ALWAYS_INLINE Vectorized(vint32 v) : _vec0{v}, _vec1{v} {}
|
| 36 |
+
C10_ALWAYS_INLINE Vectorized(vbool32 vmask) : _vecb0{vmask}, _vecb1{vmask} {}
|
| 37 |
+
C10_ALWAYS_INLINE Vectorized(vint32 v1, vint32 v2) : _vec0{v1}, _vec1{v2} {}
|
| 38 |
+
C10_ALWAYS_INLINE Vectorized(vbool32 v1, vbool32 v2) : _vecb0{v1}, _vecb1{v2} {}
|
| 39 |
+
C10_ALWAYS_INLINE Vectorized(int32_t scalar)
|
| 40 |
+
: _vec0{vec_splats(scalar)}, _vec1{vec_splats(scalar)} {}
|
| 41 |
+
C10_ALWAYS_INLINE Vectorized(
|
| 42 |
+
int32_t scalar1,
|
| 43 |
+
int32_t scalar2,
|
| 44 |
+
int32_t scalar3,
|
| 45 |
+
int32_t scalar4,
|
| 46 |
+
int32_t scalar5,
|
| 47 |
+
int32_t scalar6,
|
| 48 |
+
int32_t scalar7,
|
| 49 |
+
int32_t scalar8)
|
| 50 |
+
: _vec0{vint32{scalar1, scalar2, scalar3, scalar4}},
|
| 51 |
+
_vec1{vint32{scalar5, scalar6, scalar7, scalar8}} {}
|
| 52 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec0() const {
|
| 53 |
+
return _vec0;
|
| 54 |
+
}
|
| 55 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec1() const {
|
| 56 |
+
return _vec1;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
template <uint64_t mask>
|
| 60 |
+
static std::enable_if_t<mask == 0, Vectorized<int32_t>> C10_ALWAYS_INLINE
|
| 61 |
+
blend(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 62 |
+
return a;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
template <uint64_t mask>
|
| 66 |
+
static std::enable_if_t<(mask & 255) == 255, Vectorized<int32_t>> C10_ALWAYS_INLINE
|
| 67 |
+
blend(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 68 |
+
return b;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
template <uint64_t mask>
|
| 72 |
+
static std::enable_if_t<mask == 15, Vectorized<int32_t>> C10_ALWAYS_INLINE
|
| 73 |
+
blend(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 74 |
+
return {b._vec0, a._vec1};
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
template <uint64_t mask>
|
| 78 |
+
static std::enable_if_t<(mask > 0 && mask < 15), Vectorized<int32_t>>
|
| 79 |
+
C10_ALWAYS_INLINE blend(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 80 |
+
constexpr uint32_t g0 = (mask & 1) * 0xffffffff;
|
| 81 |
+
constexpr uint32_t g1 = ((mask & 2) >> 1) * 0xffffffff;
|
| 82 |
+
constexpr uint32_t g2 = ((mask & 4) >> 2) * 0xffffffff;
|
| 83 |
+
constexpr uint32_t g3 = ((mask & 8) >> 3) * 0xffffffff;
|
| 84 |
+
const vbool32 mask_1st = (vbool32){g0, g1, g2, g3};
|
| 85 |
+
|
| 86 |
+
return {(vint32)vec_sel(a._vec0, b._vec0, (vbool32)mask_1st), a._vec1};
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
template <uint64_t mask>
|
| 90 |
+
static std::enable_if_t<
|
| 91 |
+
(mask > 15 && (mask & 255) != 255 && ((mask & 15) == 15)),
|
| 92 |
+
Vectorized<int32_t>>
|
| 93 |
+
C10_ALWAYS_INLINE blend(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 94 |
+
constexpr uint32_t mask2 = (mask & 255) >> 4;
|
| 95 |
+
constexpr uint32_t g0_2 = (mask2 & 1) * 0xffffffff;
|
| 96 |
+
constexpr uint32_t g1_2 = ((mask2 & 2) >> 1) * 0xffffffff;
|
| 97 |
+
constexpr uint32_t g2_2 = ((mask2 & 4) >> 2) * 0xffffffff;
|
| 98 |
+
constexpr uint32_t g3_2 = ((mask2 & 8) >> 3) * 0xffffffff;
|
| 99 |
+
|
| 100 |
+
const vbool32 mask_2nd = (vbool32){g0_2, g1_2, g2_2, g3_2};
|
| 101 |
+
// generated masks
|
| 102 |
+
return {b._vec0, (vint32)vec_sel(a._vec1, b._vec1, (vbool32)mask_2nd)};
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
template <uint64_t mask>
|
| 106 |
+
static std::enable_if_t<
|
| 107 |
+
(mask > 15 && ((mask & 255) != 255) && ((mask & 15) == 0)),
|
| 108 |
+
Vectorized<int32_t>>
|
| 109 |
+
C10_ALWAYS_INLINE blend(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 110 |
+
constexpr uint32_t mask2 = (mask & 255) >> 4;
|
| 111 |
+
constexpr uint32_t g0_2 = (mask2 & 1) * 0xffffffff;
|
| 112 |
+
constexpr uint32_t g1_2 = ((mask2 & 2) >> 1) * 0xffffffff;
|
| 113 |
+
constexpr uint32_t g2_2 = ((mask2 & 4) >> 2) * 0xffffffff;
|
| 114 |
+
constexpr uint32_t g3_2 = ((mask2 & 8) >> 3) * 0xffffffff;
|
| 115 |
+
|
| 116 |
+
const vbool32 mask_2nd = (vbool32){g0_2, g1_2, g2_2, g3_2};
|
| 117 |
+
// generated masks
|
| 118 |
+
return {a, (vint32)vec_sel(a._vec1, b._vec1, (vbool32)mask_2nd)};
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
template <uint64_t mask>
|
| 122 |
+
static std::enable_if_t<
|
| 123 |
+
(mask > 15 && ((mask & 255) != 255) && ((mask & 15) != 0) &&
|
| 124 |
+
((mask & 15) != 15)),
|
| 125 |
+
Vectorized<int32_t>>
|
| 126 |
+
C10_ALWAYS_INLINE blend(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 127 |
+
constexpr uint32_t g0 = (mask & 1) * 0xffffffff;
|
| 128 |
+
constexpr uint32_t g1 = ((mask & 2) >> 1) * 0xffffffff;
|
| 129 |
+
constexpr uint32_t g2 = ((mask & 4) >> 2) * 0xffffffff;
|
| 130 |
+
constexpr uint32_t g3 = ((mask & 8) >> 3) * 0xffffffff;
|
| 131 |
+
constexpr uint32_t mask2 = (mask & 255) >> 4;
|
| 132 |
+
constexpr uint32_t g0_2 = (mask2 & 1) * 0xffffffff;
|
| 133 |
+
constexpr uint32_t g1_2 = ((mask2 & 2) >> 1) * 0xffffffff;
|
| 134 |
+
constexpr uint32_t g2_2 = ((mask2 & 4) >> 2) * 0xffffffff;
|
| 135 |
+
constexpr uint32_t g3_2 = ((mask2 & 8) >> 3) * 0xffffffff;
|
| 136 |
+
|
| 137 |
+
const vbool32 mask_1st = (vbool32){g0, g1, g2, g3};
|
| 138 |
+
const vbool32 mask_2nd = (vbool32){g0_2, g1_2, g2_2, g3_2};
|
| 139 |
+
// generated masks
|
| 140 |
+
return {
|
| 141 |
+
(vint32)vec_sel(a._vec0, b._vec0, (vbool32)mask_1st),
|
| 142 |
+
(vint32)vec_sel(a._vec1, b._vec1, (vbool32)mask_2nd)};
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
static Vectorized<int32_t> C10_ALWAYS_INLINE blendv(
|
| 146 |
+
const Vectorized<int32_t>& a,
|
| 147 |
+
const Vectorized<int32_t>& b,
|
| 148 |
+
const Vectorized<int32_t>& mask) {
|
| 149 |
+
// the mask used here returned by comparision of vec256
|
| 150 |
+
// assuming this we can use the same mask directly with vec_sel
|
| 151 |
+
// warning intel style mask will not work properly
|
| 152 |
+
return {
|
| 153 |
+
vec_sel(a._vec0, b._vec0, mask._vecb0),
|
| 154 |
+
vec_sel(a._vec1, b._vec1, mask._vecb1)};
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
template <typename step_t>
|
| 158 |
+
static Vectorized<int32_t> arange(int32_t base = 0.f, step_t step = static_cast<step_t>(1)) {
|
| 159 |
+
return Vectorized<int32_t>(
|
| 160 |
+
base,
|
| 161 |
+
base + step,
|
| 162 |
+
base + 2 * step,
|
| 163 |
+
base + 3 * step,
|
| 164 |
+
base + 4 * step,
|
| 165 |
+
base + 5 * step,
|
| 166 |
+
base + 6 * step,
|
| 167 |
+
base + 7 * step);
|
| 168 |
+
}
|
| 169 |
+
static Vectorized<int32_t> set(
|
| 170 |
+
const Vectorized<int32_t>& a,
|
| 171 |
+
const Vectorized<int32_t>& b,
|
| 172 |
+
size_t count = size()) {
|
| 173 |
+
switch (count) {
|
| 174 |
+
case 0:
|
| 175 |
+
return a;
|
| 176 |
+
case 1:
|
| 177 |
+
return blend<1>(a, b);
|
| 178 |
+
case 2:
|
| 179 |
+
return blend<3>(a, b);
|
| 180 |
+
case 3:
|
| 181 |
+
return blend<7>(a, b);
|
| 182 |
+
case 4:
|
| 183 |
+
return blend<15>(a, b);
|
| 184 |
+
case 5:
|
| 185 |
+
return blend<31>(a, b);
|
| 186 |
+
case 6:
|
| 187 |
+
return blend<63>(a, b);
|
| 188 |
+
case 7:
|
| 189 |
+
return blend<127>(a, b);
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
return b;
|
| 193 |
+
}
|
| 194 |
+
static Vectorized<value_type> C10_ALWAYS_INLINE
|
| 195 |
+
loadu(const void* ptr, int count = size()) {
|
| 196 |
+
if (count == size()) {
|
| 197 |
+
return {
|
| 198 |
+
vec_vsx_ld(offset0, reinterpret_cast<const value_type*>(ptr)),
|
| 199 |
+
vec_vsx_ld(offset16, reinterpret_cast<const value_type*>(ptr))};
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
__at_align__ value_type tmp_values[size()] = {};
|
| 203 |
+
std::memcpy(tmp_values, ptr, std::min(count, size()) * sizeof(value_type));
|
| 204 |
+
|
| 205 |
+
return {vec_vsx_ld(offset0, tmp_values), vec_vsx_ld(offset16, tmp_values)};
|
| 206 |
+
}
|
| 207 |
+
void C10_ALWAYS_INLINE store(void* ptr, int count = size()) const {
|
| 208 |
+
if (count == size()) {
|
| 209 |
+
vec_vsx_st(_vec0, offset0, reinterpret_cast<value_type*>(ptr));
|
| 210 |
+
vec_vsx_st(_vec1, offset16, reinterpret_cast<value_type*>(ptr));
|
| 211 |
+
} else if (count > 0) {
|
| 212 |
+
__at_align__ value_type tmp_values[size()];
|
| 213 |
+
vec_vsx_st(_vec0, offset0, tmp_values);
|
| 214 |
+
vec_vsx_st(_vec1, offset16, tmp_values);
|
| 215 |
+
std::memcpy(
|
| 216 |
+
ptr, tmp_values, std::min(count, size()) * sizeof(value_type));
|
| 217 |
+
}
|
| 218 |
+
}
|
| 219 |
+
const int32_t& operator[](int idx) const = delete;
|
| 220 |
+
int32_t& operator[](int idx) = delete;
|
| 221 |
+
|
| 222 |
+
Vectorized<int32_t> angle() const {
|
| 223 |
+
return blendv(
|
| 224 |
+
Vectorized<int32_t>(0), Vectorized<int32_t>(c10::pi<int32_t>), *this < Vectorized<int32_t>(0));
|
| 225 |
+
}
|
| 226 |
+
Vectorized<int32_t> real() const {
|
| 227 |
+
return *this;
|
| 228 |
+
}
|
| 229 |
+
Vectorized<int32_t> imag() const {
|
| 230 |
+
return Vectorized<int32_t>{0};
|
| 231 |
+
}
|
| 232 |
+
Vectorized<int32_t> conj() const {
|
| 233 |
+
return *this;
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
Vectorized<int32_t> C10_ALWAYS_INLINE abs() const {
|
| 237 |
+
return {vec_abs(_vec0), vec_abs(_vec1)};
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
Vectorized<int32_t> C10_ALWAYS_INLINE neg() const {
|
| 241 |
+
return {vec_neg(_vec0), vec_neg(_vec1)};
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
DEFINE_MEMBER_UNARY_OP(operator~, int32_t, vec_not)
|
| 245 |
+
DEFINE_MEMBER_OP(operator==, int32_t, vec_cmpeq)
|
| 246 |
+
DEFINE_MEMBER_OP(operator!=, int32_t, vec_cmpne)
|
| 247 |
+
DEFINE_MEMBER_OP(operator<, int32_t, vec_cmplt)
|
| 248 |
+
DEFINE_MEMBER_OP(operator<=, int32_t, vec_cmple)
|
| 249 |
+
DEFINE_MEMBER_OP(operator>, int32_t, vec_cmpgt)
|
| 250 |
+
DEFINE_MEMBER_OP(operator>=, int32_t, vec_cmpge)
|
| 251 |
+
DEFINE_MEMBER_OP_AND_ONE(eq, int32_t, vec_cmpeq)
|
| 252 |
+
DEFINE_MEMBER_OP_AND_ONE(ne, int32_t, vec_cmpne)
|
| 253 |
+
DEFINE_MEMBER_OP_AND_ONE(lt, int32_t, vec_cmplt)
|
| 254 |
+
DEFINE_MEMBER_OP_AND_ONE(le, int32_t, vec_cmple)
|
| 255 |
+
DEFINE_MEMBER_OP_AND_ONE(gt, int32_t, vec_cmpgt)
|
| 256 |
+
DEFINE_MEMBER_OP_AND_ONE(ge, int32_t, vec_cmpge)
|
| 257 |
+
DEFINE_MEMBER_OP(operator+, int32_t, vec_add)
|
| 258 |
+
DEFINE_MEMBER_OP(operator-, int32_t, vec_sub)
|
| 259 |
+
DEFINE_MEMBER_OP(operator*, int32_t, vec_mul)
|
| 260 |
+
DEFINE_MEMBER_EMULATE_BINARY_OP(operator/, int32_t, /)
|
| 261 |
+
DEFINE_MEMBER_OP(maximum, int32_t, vec_max)
|
| 262 |
+
DEFINE_MEMBER_OP(minimum, int32_t, vec_min)
|
| 263 |
+
DEFINE_MEMBER_OP(operator&, int32_t, vec_and)
|
| 264 |
+
DEFINE_MEMBER_OP(operator|, int32_t, vec_or)
|
| 265 |
+
DEFINE_MEMBER_OP(operator^, int32_t, vec_xor)
|
| 266 |
+
};
|
| 267 |
+
|
| 268 |
+
template <>
|
| 269 |
+
Vectorized<int32_t> inline operator<<(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 270 |
+
vuint32 shift_vec0 = reinterpret_cast<vuint32>(b.vec0());
|
| 271 |
+
vuint32 shift_vec1 = reinterpret_cast<vuint32>(b.vec1()) ;
|
| 272 |
+
return Vectorized<int32_t>{vec_sl(a.vec0(), shift_vec0), vec_sl(a.vec1(), shift_vec1)};
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
template <>
|
| 276 |
+
Vectorized<int32_t> inline operator>>(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 277 |
+
vuint32 shift_vec0 = reinterpret_cast<vuint32>(b.vec0());
|
| 278 |
+
vuint32 shift_vec1 = reinterpret_cast<vuint32>(b.vec1()) ;
|
| 279 |
+
return Vectorized<int32_t>{vec_sr(a.vec0(), shift_vec0), vec_sr(a.vec1(), shift_vec1)};
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
template <>
|
| 283 |
+
Vectorized<int32_t> inline maximum(
|
| 284 |
+
const Vectorized<int32_t>& a,
|
| 285 |
+
const Vectorized<int32_t>& b) {
|
| 286 |
+
return a.maximum(b);
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
template <>
|
| 290 |
+
Vectorized<int32_t> inline minimum(
|
| 291 |
+
const Vectorized<int32_t>& a,
|
| 292 |
+
const Vectorized<int32_t>& b) {
|
| 293 |
+
return a.minimum(b);
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
template <>
|
| 297 |
+
Vectorized<int32_t> C10_ALWAYS_INLINE operator+(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 298 |
+
return Vectorized<int32_t>{vec_add(a.vec0(), b.vec0()), vec_add(a.vec1(), b.vec1())};
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
template <>
|
| 302 |
+
Vectorized<int32_t> C10_ALWAYS_INLINE operator-(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 303 |
+
return Vectorized<int32_t>{vec_sub(a.vec0(), b.vec0()), vec_sub(a.vec1(), b.vec1())};
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
template <>
|
| 307 |
+
Vectorized<int32_t> C10_ALWAYS_INLINE operator*(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 308 |
+
return Vectorized<int32_t>{vec_mul(a.vec0(), b.vec0()), vec_mul(a.vec1(), b.vec1())};
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
template <>
|
| 312 |
+
Vectorized<int32_t> C10_ALWAYS_INLINE operator/(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 313 |
+
return Vectorized<int32_t>{a.vec0()/b.vec0(), a.vec1()/b.vec1()};
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
template <>
|
| 317 |
+
Vectorized<int32_t> C10_ALWAYS_INLINE operator&(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 318 |
+
return Vectorized<int32_t>{vec_and(a.vec0(), b.vec0()), vec_and(a.vec1(), b.vec1())};
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
template <>
|
| 322 |
+
Vectorized<int32_t> C10_ALWAYS_INLINE operator|(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 323 |
+
return Vectorized<int32_t>{vec_or(a.vec0(), b.vec0()), vec_or(a.vec1(), b.vec1())};
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
template <>
|
| 327 |
+
Vectorized<int32_t> C10_ALWAYS_INLINE operator^(const Vectorized<int32_t>& a, const Vectorized<int32_t>& b) {
|
| 328 |
+
return Vectorized<int32_t>{vec_xor(a.vec0(), b.vec0()), vec_xor(a.vec1(), b.vec1())};
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
} // namespace
|
| 332 |
+
} // namespace vec
|
| 333 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_int64_vsx.h
ADDED
|
@@ -0,0 +1,286 @@
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 4 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 5 |
+
#include <ATen/cpu/vec/vec256/vsx/vsx_helpers.h>
|
| 6 |
+
namespace at {
|
| 7 |
+
namespace vec {
|
| 8 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 9 |
+
inline namespace CPU_CAPABILITY {
|
| 10 |
+
|
| 11 |
+
template <>
|
| 12 |
+
class Vectorized<int64_t> {
|
| 13 |
+
private:
|
| 14 |
+
union {
|
| 15 |
+
struct {
|
| 16 |
+
vint64 _vec0;
|
| 17 |
+
vint64 _vec1;
|
| 18 |
+
};
|
| 19 |
+
struct {
|
| 20 |
+
vbool64 _vecb0;
|
| 21 |
+
vbool64 _vecb1;
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
} __attribute__((__may_alias__));
|
| 25 |
+
|
| 26 |
+
public:
|
| 27 |
+
using value_type = int64_t;
|
| 28 |
+
using vec_internal_type = vint64;
|
| 29 |
+
using vec_internal_mask_type = vbool64;
|
| 30 |
+
using size_type = int;
|
| 31 |
+
using ElementType = signed long long;
|
| 32 |
+
static constexpr size_type size() {
|
| 33 |
+
return 4;
|
| 34 |
+
}
|
| 35 |
+
Vectorized() {}
|
| 36 |
+
C10_ALWAYS_INLINE Vectorized(vint64 v) : _vec0{v}, _vec1{v} {}
|
| 37 |
+
C10_ALWAYS_INLINE Vectorized(vbool64 vmask) : _vecb0{vmask}, _vecb1{vmask} {}
|
| 38 |
+
C10_ALWAYS_INLINE Vectorized(vint64 v1, vint64 v2) : _vec0{v1}, _vec1{v2} {}
|
| 39 |
+
C10_ALWAYS_INLINE Vectorized(vbool64 v1, vbool64 v2) : _vecb0{v1}, _vecb1{v2} {}
|
| 40 |
+
C10_ALWAYS_INLINE Vectorized(int64_t scalar)
|
| 41 |
+
: _vec0{vec_splats(scalar)}, _vec1{vec_splats(scalar)} {}
|
| 42 |
+
C10_ALWAYS_INLINE Vectorized(
|
| 43 |
+
int64_t scalar1,
|
| 44 |
+
int64_t scalar2,
|
| 45 |
+
int64_t scalar3,
|
| 46 |
+
int64_t scalar4)
|
| 47 |
+
: _vec0{vint64{scalar1, scalar2}}, _vec1{vint64{scalar3, scalar4}} {}
|
| 48 |
+
|
| 49 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec0() const {
|
| 50 |
+
return _vec0;
|
| 51 |
+
}
|
| 52 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec1() const {
|
| 53 |
+
return _vec1;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
template <uint64_t mask>
|
| 57 |
+
static std::enable_if_t<mask == 0, Vectorized<int64_t>> C10_ALWAYS_INLINE
|
| 58 |
+
blend(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 59 |
+
return a;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
template <uint64_t mask>
|
| 63 |
+
static std::enable_if_t<mask == 3, Vectorized<int64_t>> C10_ALWAYS_INLINE
|
| 64 |
+
blend(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 65 |
+
return {b._vec0, a._vec1};
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
template <uint64_t mask>
|
| 69 |
+
static std::enable_if_t<(mask & 15) == 15, Vectorized<int64_t>> C10_ALWAYS_INLINE
|
| 70 |
+
blend(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 71 |
+
return b;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
template <uint64_t mask>
|
| 75 |
+
static std::enable_if_t<(mask > 0 && mask < 3), Vectorized<int64_t>> C10_ALWAYS_INLINE
|
| 76 |
+
blend(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 77 |
+
constexpr uint64_t g0 = (mask & 1) * 0xffffffffffffffff;
|
| 78 |
+
constexpr uint64_t g1 = ((mask & 2) >> 1) * 0xffffffffffffffff;
|
| 79 |
+
const vbool64 mask_1st = (vbool64){g0, g1};
|
| 80 |
+
return {(vint64)vec_sel(a._vec0, b._vec0, (vbool64)mask_1st), a._vec1};
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
template <uint64_t mask>
|
| 84 |
+
static std::enable_if_t<(mask > 3) && (mask & 3) == 0, Vectorized<int64_t>>
|
| 85 |
+
C10_ALWAYS_INLINE blend(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 86 |
+
constexpr uint64_t g0_2 = ((mask & 4) >> 2) * 0xffffffffffffffff;
|
| 87 |
+
constexpr uint64_t g1_2 = ((mask & 8) >> 3) * 0xffffffffffffffff;
|
| 88 |
+
|
| 89 |
+
const vbool64 mask_2nd = (vbool64){g0_2, g1_2};
|
| 90 |
+
return {a._vec0, (vint64)vec_sel(a._vec1, b._vec1, (vbool64)mask_2nd)};
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
template <uint64_t mask>
|
| 94 |
+
static std::enable_if_t<
|
| 95 |
+
(mask > 3) && (mask & 3) != 0 && (mask & 15) != 15,
|
| 96 |
+
Vectorized<int64_t>>
|
| 97 |
+
C10_ALWAYS_INLINE blend(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 98 |
+
constexpr uint64_t g0 = (mask & 1) * 0xffffffffffffffff;
|
| 99 |
+
constexpr uint64_t g1 = ((mask & 2) >> 1) * 0xffffffffffffffff;
|
| 100 |
+
constexpr uint64_t g0_2 = ((mask & 4) >> 2) * 0xffffffffffffffff;
|
| 101 |
+
constexpr uint64_t g1_2 = ((mask & 8) >> 3) * 0xffffffffffffffff;
|
| 102 |
+
|
| 103 |
+
const vbool64 mask_1st = (vbool64){g0, g1};
|
| 104 |
+
const vbool64 mask_2nd = (vbool64){g0_2, g1_2};
|
| 105 |
+
return {
|
| 106 |
+
(vint64)vec_sel(a._vec0, b._vec0, (vbool64)mask_1st),
|
| 107 |
+
(vint64)vec_sel(a._vec1, b._vec1, (vbool64)mask_2nd)};
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
static Vectorized<int64_t> C10_ALWAYS_INLINE blendv(
|
| 111 |
+
const Vectorized<int64_t>& a,
|
| 112 |
+
const Vectorized<int64_t>& b,
|
| 113 |
+
const Vectorized<int64_t>& mask) {
|
| 114 |
+
// the mask used here returned by comparision of vec256
|
| 115 |
+
|
| 116 |
+
return {
|
| 117 |
+
vec_sel(a._vec0, b._vec0, mask._vecb0),
|
| 118 |
+
vec_sel(a._vec1, b._vec1, mask._vecb1)};
|
| 119 |
+
}
|
| 120 |
+
template <typename step_t>
|
| 121 |
+
static Vectorized<int64_t> arange(int64_t base = 0., step_t step = static_cast<step_t>(1)) {
|
| 122 |
+
return Vectorized<int64_t>(base, base + step, base + 2 * step, base + 3 * step);
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
static Vectorized<int64_t> C10_ALWAYS_INLINE
|
| 126 |
+
set(const Vectorized<int64_t>& a,
|
| 127 |
+
const Vectorized<int64_t>& b,
|
| 128 |
+
size_t count = size()) {
|
| 129 |
+
switch (count) {
|
| 130 |
+
case 0:
|
| 131 |
+
return a;
|
| 132 |
+
case 1:
|
| 133 |
+
return blend<1>(a, b);
|
| 134 |
+
case 2:
|
| 135 |
+
return blend<3>(a, b);
|
| 136 |
+
case 3:
|
| 137 |
+
return blend<7>(a, b);
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
return b;
|
| 141 |
+
}
|
| 142 |
+
static Vectorized<value_type> C10_ALWAYS_INLINE
|
| 143 |
+
loadu(const void* ptr, int count = size()) {
|
| 144 |
+
if (count == size()) {
|
| 145 |
+
static_assert(sizeof(double) == sizeof(value_type));
|
| 146 |
+
const double* dptr = reinterpret_cast<const double*>(ptr);
|
| 147 |
+
return {// treat it as double load
|
| 148 |
+
(vint64)vec_vsx_ld(offset0, dptr),
|
| 149 |
+
(vint64)vec_vsx_ld(offset16, dptr)};
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
__at_align__ double tmp_values[size()] = {};
|
| 153 |
+
std::memcpy(tmp_values, ptr, std::min(count, size()) * sizeof(value_type));
|
| 154 |
+
|
| 155 |
+
return {
|
| 156 |
+
(vint64)vec_vsx_ld(offset0, tmp_values),
|
| 157 |
+
(vint64)vec_vsx_ld(offset16, tmp_values)};
|
| 158 |
+
}
|
| 159 |
+
void C10_ALWAYS_INLINE store(void* ptr, int count = size()) const {
|
| 160 |
+
if (count == size()) {
|
| 161 |
+
double* dptr = reinterpret_cast<double*>(ptr);
|
| 162 |
+
vec_vsx_st((vfloat64)_vec0, offset0, dptr);
|
| 163 |
+
vec_vsx_st((vfloat64)_vec1, offset16, dptr);
|
| 164 |
+
} else if (count > 0) {
|
| 165 |
+
__at_align__ double tmp_values[size()];
|
| 166 |
+
vec_vsx_st((vfloat64)_vec0, offset0, tmp_values);
|
| 167 |
+
vec_vsx_st((vfloat64)_vec1, offset16, tmp_values);
|
| 168 |
+
std::memcpy(
|
| 169 |
+
ptr, tmp_values, std::min(count, size()) * sizeof(value_type));
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
const int64_t& operator[](int idx) const = delete;
|
| 173 |
+
int64_t& operator[](int idx) = delete;
|
| 174 |
+
|
| 175 |
+
Vectorized<int64_t> angle() const {
|
| 176 |
+
return blendv(
|
| 177 |
+
Vectorized<int64_t>(0), Vectorized<int64_t>(c10::pi<int64_t>), *this < Vectorized<int64_t>(0));
|
| 178 |
+
}
|
| 179 |
+
Vectorized<int64_t> real() const {
|
| 180 |
+
return *this;
|
| 181 |
+
}
|
| 182 |
+
Vectorized<int64_t> imag() const {
|
| 183 |
+
return Vectorized<int64_t>{0};
|
| 184 |
+
}
|
| 185 |
+
Vectorized<int64_t> conj() const {
|
| 186 |
+
return *this;
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
Vectorized<int64_t> C10_ALWAYS_INLINE abs() const {
|
| 190 |
+
return {vec_abs(_vec0), vec_abs(_vec1)};
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
Vectorized<int64_t> C10_ALWAYS_INLINE neg() const {
|
| 194 |
+
return {vec_neg(_vec0), vec_neg(_vec1)};
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
DEFINE_MEMBER_UNARY_OP(operator~, int64_t, vec_not)
|
| 198 |
+
DEFINE_MEMBER_OP(operator==, int64_t, vec_cmpeq)
|
| 199 |
+
DEFINE_MEMBER_OP(operator!=, int64_t, vec_cmpne)
|
| 200 |
+
DEFINE_MEMBER_OP(operator<, int64_t, vec_cmplt)
|
| 201 |
+
DEFINE_MEMBER_OP(operator<=, int64_t, vec_cmple)
|
| 202 |
+
DEFINE_MEMBER_OP(operator>, int64_t, vec_cmpgt)
|
| 203 |
+
DEFINE_MEMBER_OP(operator>=, int64_t, vec_cmpge)
|
| 204 |
+
DEFINE_MEMBER_OP_AND_ONE(eq, int64_t, vec_cmpeq)
|
| 205 |
+
DEFINE_MEMBER_OP_AND_ONE(ne, int64_t, vec_cmpne)
|
| 206 |
+
DEFINE_MEMBER_OP_AND_ONE(lt, int64_t, vec_cmplt)
|
| 207 |
+
DEFINE_MEMBER_OP_AND_ONE(le, int64_t, vec_cmple)
|
| 208 |
+
DEFINE_MEMBER_OP_AND_ONE(gt, int64_t, vec_cmpgt)
|
| 209 |
+
DEFINE_MEMBER_OP_AND_ONE(ge, int64_t, vec_cmpge)
|
| 210 |
+
DEFINE_MEMBER_OP(operator+, int64_t, vec_add)
|
| 211 |
+
DEFINE_MEMBER_OP(operator-, int64_t, vec_sub)
|
| 212 |
+
DEFINE_MEMBER_OP(operator*, int64_t, vec_mul)
|
| 213 |
+
DEFINE_MEMBER_OP(operator/, int64_t, vec_div)
|
| 214 |
+
DEFINE_MEMBER_OP(maximum, int64_t, vec_max)
|
| 215 |
+
DEFINE_MEMBER_OP(minimum, int64_t, vec_min)
|
| 216 |
+
DEFINE_MEMBER_OP(operator&, int64_t, vec_and)
|
| 217 |
+
DEFINE_MEMBER_OP(operator|, int64_t, vec_or)
|
| 218 |
+
DEFINE_MEMBER_OP(operator^, int64_t, vec_xor)
|
| 219 |
+
};
|
| 220 |
+
|
| 221 |
+
template <>
|
| 222 |
+
Vectorized<int64_t> inline operator<<(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 223 |
+
vuint64 shift_vec0 = reinterpret_cast<vuint64>(b.vec0());
|
| 224 |
+
vuint64 shift_vec1 = reinterpret_cast<vuint64>(b.vec1()) ;
|
| 225 |
+
return Vectorized<int64_t>{vec_sl(a.vec0(), shift_vec0), vec_sl(a.vec1(), shift_vec1)};
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
template <>
|
| 229 |
+
Vectorized<int64_t> inline operator>>(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 230 |
+
vuint64 shift_vec0 = reinterpret_cast<vuint64>(b.vec0());
|
| 231 |
+
vuint64 shift_vec1 = reinterpret_cast<vuint64>(b.vec1()) ;
|
| 232 |
+
return Vectorized<int64_t>{vec_sr(a.vec0(), shift_vec0), vec_sr(a.vec1(), shift_vec1)};
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
template <>
|
| 236 |
+
Vectorized<int64_t> inline maximum(
|
| 237 |
+
const Vectorized<int64_t>& a,
|
| 238 |
+
const Vectorized<int64_t>& b) {
|
| 239 |
+
return a.maximum(b);
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
template <>
|
| 243 |
+
Vectorized<int64_t> inline minimum(
|
| 244 |
+
const Vectorized<int64_t>& a,
|
| 245 |
+
const Vectorized<int64_t>& b) {
|
| 246 |
+
return a.minimum(b);
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
template <>
|
| 250 |
+
Vectorized<int64_t> C10_ALWAYS_INLINE operator+(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 251 |
+
return Vectorized<int64_t>{vec_add(a.vec0(), b.vec0()), vec_add(a.vec1(), b.vec1())};
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
template <>
|
| 255 |
+
Vectorized<int64_t> C10_ALWAYS_INLINE operator-(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 256 |
+
return Vectorized<int64_t>{vec_sub(a.vec0(), b.vec0()), vec_sub(a.vec1(), b.vec1())};
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
template <>
|
| 260 |
+
Vectorized<int64_t> C10_ALWAYS_INLINE operator*(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 261 |
+
return Vectorized<int64_t>{vec_mul(a.vec0(), b.vec0()), vec_mul(a.vec1(), b.vec1())};
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
template <>
|
| 265 |
+
Vectorized<int64_t> C10_ALWAYS_INLINE operator/(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 266 |
+
return Vectorized<int64_t>{vec_div(a.vec0(), b.vec0()), vec_div(a.vec1(), b.vec1())};
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
template <>
|
| 270 |
+
Vectorized<int64_t> C10_ALWAYS_INLINE operator&(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 271 |
+
return Vectorized<int64_t>{vec_and(a.vec0(), b.vec0()), vec_and(a.vec1(), b.vec1())};
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
template <>
|
| 275 |
+
Vectorized<int64_t> C10_ALWAYS_INLINE operator|(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 276 |
+
return Vectorized<int64_t>{vec_or(a.vec0(), b.vec0()), vec_or(a.vec1(), b.vec1())};
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
template <>
|
| 280 |
+
Vectorized<int64_t> C10_ALWAYS_INLINE operator^(const Vectorized<int64_t>& a, const Vectorized<int64_t>& b) {
|
| 281 |
+
return Vectorized<int64_t>{vec_xor(a.vec0(), b.vec0()), vec_xor(a.vec1(), b.vec1())};
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
} // namespace
|
| 285 |
+
} // namespace vec
|
| 286 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_qint32_vsx.h
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 4 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 5 |
+
#include <ATen/cpu/vec/vec256/vsx/vsx_helpers.h>
|
| 6 |
+
#include <c10/util/qint32.h>
|
| 7 |
+
#include <array>
|
| 8 |
+
|
| 9 |
+
// This file defines Vectorized<> for the quantized types.
|
| 10 |
+
//
|
| 11 |
+
//
|
| 12 |
+
// Currently, we simply use these classes as efficient converters between
|
| 13 |
+
// the quantized types and Vectorized<float>, usually in bandwidth-bound cases
|
| 14 |
+
// where doing the arithmetic in full-precision is acceptable (e.g.
|
| 15 |
+
// elementwise operators).
|
| 16 |
+
//
|
| 17 |
+
//
|
| 18 |
+
// Conversions are as follows:
|
| 19 |
+
// Vectorized<qint32> -> 1x Vectorized<float>
|
| 20 |
+
//
|
| 21 |
+
// The size of the returned float vector is specified by the special
|
| 22 |
+
// constexpr function float_num_vecs. The type of the value returned
|
| 23 |
+
// from dequantize (and expected as an argument to quantize) is
|
| 24 |
+
// specified by float_vec_return_type.
|
| 25 |
+
//
|
| 26 |
+
// When writing kernels with these vectors, it is expected that floating-
|
| 27 |
+
// point operations will be carried out in a loop over Vectorized<T>::float_num_vecs
|
| 28 |
+
// iterations.
|
| 29 |
+
|
| 30 |
+
namespace at {
|
| 31 |
+
namespace vec {
|
| 32 |
+
inline namespace CPU_CAPABILITY {
|
| 33 |
+
|
| 34 |
+
template <>
|
| 35 |
+
struct Vectorized<c10::qint32> {
|
| 36 |
+
private:
|
| 37 |
+
union {
|
| 38 |
+
struct {
|
| 39 |
+
vint32 _vec0;
|
| 40 |
+
vint32 _vec1;
|
| 41 |
+
};
|
| 42 |
+
struct {
|
| 43 |
+
vbool32 _vecb0;
|
| 44 |
+
vbool32 _vecb1;
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
} __attribute__((__may_alias__));
|
| 48 |
+
|
| 49 |
+
public:
|
| 50 |
+
Vectorized() {}
|
| 51 |
+
|
| 52 |
+
using size_type = int;
|
| 53 |
+
static constexpr size_type size() {
|
| 54 |
+
return 8;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
static constexpr size_t float_num_vecs() {
|
| 58 |
+
return 1;
|
| 59 |
+
}
|
| 60 |
+
static constexpr int int_num_vecs() {
|
| 61 |
+
return 1;
|
| 62 |
+
}
|
| 63 |
+
using float_vec_return_type = std::array<Vectorized<float>, 1>;
|
| 64 |
+
using int_vec_return_type = std::array<Vectorized<c10::qint32>, 1>;
|
| 65 |
+
using value_type = c10::qint32::underlying;
|
| 66 |
+
using vec_internal_type = vint32;
|
| 67 |
+
using vec_internal_mask_type = vbool32;
|
| 68 |
+
C10_ALWAYS_INLINE Vectorized(vint32 v) : _vec0{v}, _vec1{v} {}
|
| 69 |
+
C10_ALWAYS_INLINE Vectorized(vbool32 vmask) : _vecb0{vmask}, _vecb1{vmask} {}
|
| 70 |
+
C10_ALWAYS_INLINE Vectorized(vint32 v1, vint32 v2) : _vec0{v1}, _vec1{v2} {}
|
| 71 |
+
C10_ALWAYS_INLINE Vectorized(vbool32 v1, vbool32 v2) : _vecb0{v1}, _vecb1{v2} {}
|
| 72 |
+
|
| 73 |
+
Vectorized(const c10::qint32& val)
|
| 74 |
+
: _vec0(vec_splats(val.val_)), _vec1(vec_splats(val.val_)) {}
|
| 75 |
+
|
| 76 |
+
static Vectorized<c10::qint32> C10_ALWAYS_INLINE
|
| 77 |
+
loadu(const void* ptr, int count = size()) {
|
| 78 |
+
if (count == size()) {
|
| 79 |
+
return {
|
| 80 |
+
vec_vsx_ld(offset0, reinterpret_cast<const value_type*>(ptr)),
|
| 81 |
+
vec_vsx_ld(offset16, reinterpret_cast<const value_type*>(ptr))};
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
__at_align__ value_type tmp_values[size()] = {};
|
| 85 |
+
std::memcpy(tmp_values, ptr, std::min(count, size()) * sizeof(value_type));
|
| 86 |
+
|
| 87 |
+
return {vec_vsx_ld(offset0, tmp_values), vec_vsx_ld(offset16, tmp_values)};
|
| 88 |
+
}
|
| 89 |
+
void C10_ALWAYS_INLINE store(void* ptr, int count = size()) const {
|
| 90 |
+
if (count == size()) {
|
| 91 |
+
vec_vsx_st(_vec0, offset0, reinterpret_cast<value_type*>(ptr));
|
| 92 |
+
vec_vsx_st(_vec1, offset16, reinterpret_cast<value_type*>(ptr));
|
| 93 |
+
} else if (count > 0) {
|
| 94 |
+
__at_align__ value_type tmp_values[size()];
|
| 95 |
+
vec_vsx_st(_vec0, offset0, tmp_values);
|
| 96 |
+
vec_vsx_st(_vec1, offset16, tmp_values);
|
| 97 |
+
std::memcpy(
|
| 98 |
+
ptr, tmp_values, std::min(count, size()) * sizeof(value_type));
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec0() const {
|
| 103 |
+
return _vec0;
|
| 104 |
+
}
|
| 105 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec1() const {
|
| 106 |
+
return _vec1;
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
float_vec_return_type dequantize(
|
| 110 |
+
Vectorized<float> scale,
|
| 111 |
+
Vectorized<float> zero_point,
|
| 112 |
+
Vectorized<float> scale_zp_premul) const {
|
| 113 |
+
vfloat32 float_vals0 = vec_float(_vec0);
|
| 114 |
+
vfloat32 float_vals1 = vec_float(_vec1);
|
| 115 |
+
vfloat32 scale_vec0 = scale.vec0();
|
| 116 |
+
vfloat32 scale_vec1 = scale.vec1();
|
| 117 |
+
vfloat32 scale_zp_premul0 = scale_zp_premul.vec0();
|
| 118 |
+
vfloat32 scale_zp_premul1 = scale_zp_premul.vec1();
|
| 119 |
+
return {Vectorized<float>{
|
| 120 |
+
vec_madd(scale_vec0, float_vals0, scale_zp_premul0),
|
| 121 |
+
vec_madd(scale_vec1, float_vals1, scale_zp_premul1)}};
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
float_vec_return_type dequantize(
|
| 125 |
+
Vectorized<float> scale,
|
| 126 |
+
Vectorized<float> zero_point) const {
|
| 127 |
+
vfloat32 float_vals0 = vec_float(_vec0);
|
| 128 |
+
vfloat32 float_vals1 = vec_float(_vec1);
|
| 129 |
+
vfloat32 scale_vec0 = scale.vec0();
|
| 130 |
+
vfloat32 scale_vec1 = scale.vec1();
|
| 131 |
+
vfloat32 zero_point0 = zero_point.vec0();
|
| 132 |
+
vfloat32 zero_point1 = zero_point.vec1();
|
| 133 |
+
return {Vectorized<float>{
|
| 134 |
+
(float_vals0 - zero_point0) * scale_vec0,
|
| 135 |
+
(float_vals1 - zero_point1) * scale_vec1}};
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
static Vectorized<c10::qint32> quantize(
|
| 139 |
+
const float_vec_return_type& rhs,
|
| 140 |
+
float scale,
|
| 141 |
+
int32_t zero_point,
|
| 142 |
+
float inverse_scale) {
|
| 143 |
+
Vectorized<c10::qint32> retval;
|
| 144 |
+
|
| 145 |
+
const vint32 vmin = vec_splats(std::numeric_limits<value_type>::min());
|
| 146 |
+
const vint32 vmax = vec_splats(std::numeric_limits<value_type>::max());
|
| 147 |
+
vfloat32 inverse_scale_v = vec_splats(inverse_scale);
|
| 148 |
+
vfloat32 vec_zero_point = vec_splats((float)(zero_point));
|
| 149 |
+
Vectorized<float> vf0 = rhs[0];
|
| 150 |
+
|
| 151 |
+
vfloat32 vecf0 = vf0.vec0();
|
| 152 |
+
vfloat32 vecf1 = vf0.vec1();
|
| 153 |
+
vecf0 = vec_mul(vecf0, inverse_scale_v);
|
| 154 |
+
vecf1 = vec_mul(vecf1, inverse_scale_v);
|
| 155 |
+
vecf0 = vec_add(vec_rint(vecf0), vec_zero_point);
|
| 156 |
+
vecf1 = vec_add(vec_rint(vecf1), vec_zero_point);
|
| 157 |
+
vint32 veci0 = vec_signed(vecf0);
|
| 158 |
+
vint32 veci1 = vec_signed(vecf1);
|
| 159 |
+
|
| 160 |
+
veci0 = vec_max(veci0, vmin);
|
| 161 |
+
veci1 = vec_max(veci1, vmin);
|
| 162 |
+
veci0 = vec_min(veci0, vmax);
|
| 163 |
+
veci1 = vec_min(veci1, vmax);
|
| 164 |
+
|
| 165 |
+
return {veci0, veci1};
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
Vectorized<c10::qint32> relu(Vectorized<c10::qint32> zero_point) const {
|
| 169 |
+
return {vec_max(_vec0, zero_point._vec0), vec_max(_vec1, zero_point._vec1)};
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
Vectorized<c10::qint32> relu6(
|
| 173 |
+
Vectorized<c10::qint32> zero_point,
|
| 174 |
+
Vectorized<c10::qint32> q_six) const {
|
| 175 |
+
vint32 max0 = vec_max(_vec0, zero_point._vec0);
|
| 176 |
+
vint32 max1 = vec_max(_vec1, zero_point._vec1);
|
| 177 |
+
return {vec_min(max0, q_six._vec0), vec_min(max1, q_six._vec1)};
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
int_vec_return_type widening_subtract(Vectorized<c10::qint32> b) const {
|
| 181 |
+
return {*this - b};
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
static Vectorized<c10::qint32> requantize_from_int(
|
| 185 |
+
const int_vec_return_type& inp,
|
| 186 |
+
float multiplier,
|
| 187 |
+
int32_t zero_point) {
|
| 188 |
+
const vint32 vmin = vec_splats(std::numeric_limits<value_type>::min());
|
| 189 |
+
const vint32 vmax = vec_splats(std::numeric_limits<value_type>::max());
|
| 190 |
+
vfloat32 vec_mult = vec_splats(multiplier);
|
| 191 |
+
vint32 vec_zero_point = vec_splats(zero_point);
|
| 192 |
+
Vectorized<c10::qint32> vi = inp[0];
|
| 193 |
+
vfloat32 vecf0 = vec_float(vi.vec0());
|
| 194 |
+
vfloat32 vecf1 = vec_float(vi.vec1());
|
| 195 |
+
|
| 196 |
+
vecf0 = vec_mul(vecf0, vec_mult);
|
| 197 |
+
vecf1 = vec_mul(vecf1, vec_mult);
|
| 198 |
+
|
| 199 |
+
vecf0 = vec_rint(vecf0);
|
| 200 |
+
vecf1 = vec_rint(vecf1);
|
| 201 |
+
|
| 202 |
+
vint32 veci0 = vec_add(vec_signed(vecf0),vec_zero_point);
|
| 203 |
+
vint32 veci1 = vec_add(vec_signed(vecf1),vec_zero_point);
|
| 204 |
+
|
| 205 |
+
veci0 = vec_max(veci0, vmin);
|
| 206 |
+
veci1 = vec_max(veci1, vmin);
|
| 207 |
+
veci0 = vec_min(veci0, vmax);
|
| 208 |
+
veci1 = vec_min(veci1, vmax);
|
| 209 |
+
|
| 210 |
+
return {veci0, veci1};
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
DEFINE_MEMBER_OP(operator==, c10::qint32, vec_cmpeq)
|
| 214 |
+
DEFINE_MEMBER_OP(operator!=, c10::qint32, vec_cmpne)
|
| 215 |
+
DEFINE_MEMBER_OP(operator<, c10::qint32, vec_cmplt)
|
| 216 |
+
DEFINE_MEMBER_OP(operator<=, c10::qint32, vec_cmple)
|
| 217 |
+
DEFINE_MEMBER_OP(operator>, c10::qint32, vec_cmpgt)
|
| 218 |
+
DEFINE_MEMBER_OP(operator>=, c10::qint32, vec_cmpge)
|
| 219 |
+
DEFINE_MEMBER_OP(operator+, c10::qint32, vec_add)
|
| 220 |
+
DEFINE_MEMBER_OP(operator-, c10::qint32, vec_sub)
|
| 221 |
+
DEFINE_MEMBER_OP(operator*, c10::qint32, vec_mul)
|
| 222 |
+
DEFINE_MEMBER_EMULATE_BINARY_OP(operator/, c10::qint32, /)
|
| 223 |
+
DEFINE_MEMBER_OP(maximum, c10::qint32, vec_max)
|
| 224 |
+
DEFINE_MEMBER_OP(minimum, c10::qint32, vec_min)
|
| 225 |
+
DEFINE_MEMBER_OP(operator&, c10::qint32, vec_and)
|
| 226 |
+
DEFINE_MEMBER_OP(operator|, c10::qint32, vec_or)
|
| 227 |
+
DEFINE_MEMBER_OP(operator^, c10::qint32, vec_xor)
|
| 228 |
+
};
|
| 229 |
+
|
| 230 |
+
template <>
|
| 231 |
+
Vectorized<c10::qint32> inline maximum(
|
| 232 |
+
const Vectorized<c10::qint32>& a,
|
| 233 |
+
const Vectorized<c10::qint32>& b) {
|
| 234 |
+
return a.maximum(b);
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
template <>
|
| 238 |
+
Vectorized<c10::qint32> inline minimum(
|
| 239 |
+
const Vectorized<c10::qint32>& a,
|
| 240 |
+
const Vectorized<c10::qint32>& b) {
|
| 241 |
+
return a.minimum(b);
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
template <>
|
| 245 |
+
Vectorized<c10::qint32> C10_ALWAYS_INLINE operator+(const Vectorized<c10::qint32>& a, const Vectorized<c10::qint32>& b) {
|
| 246 |
+
return Vectorized<c10::qint32>{vec_add(a.vec0(), b.vec0()), vec_add(a.vec1(), b.vec1())};
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
template <>
|
| 250 |
+
Vectorized<c10::qint32> C10_ALWAYS_INLINE operator-(const Vectorized<c10::qint32>& a, const Vectorized<c10::qint32>& b) {
|
| 251 |
+
return Vectorized<c10::qint32>{vec_sub(a.vec0(), b.vec0()), vec_sub(a.vec1(), b.vec1())};
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
template <>
|
| 255 |
+
Vectorized<c10::qint32> C10_ALWAYS_INLINE operator*(const Vectorized<c10::qint32>& a, const Vectorized<c10::qint32>& b) {
|
| 256 |
+
return Vectorized<c10::qint32>{vec_mul(a.vec0(), b.vec0()), vec_mul(a.vec1(), b.vec1())};
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
template <>
|
| 260 |
+
Vectorized<c10::qint32> C10_ALWAYS_INLINE operator/(const Vectorized<c10::qint32>& a, const Vectorized<c10::qint32>& b) {
|
| 261 |
+
return Vectorized<c10::qint32>{a.vec0()/b.vec0(), a.vec1()/b.vec1()};
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
template <>
|
| 265 |
+
Vectorized<c10::qint32> C10_ALWAYS_INLINE operator&(const Vectorized<c10::qint32>& a, const Vectorized<c10::qint32>& b) {
|
| 266 |
+
return Vectorized<c10::qint32>{vec_and(a.vec0(), b.vec0()), vec_and(a.vec1(), b.vec1())};
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
template <>
|
| 270 |
+
Vectorized<c10::qint32> C10_ALWAYS_INLINE operator|(const Vectorized<c10::qint32>& a, const Vectorized<c10::qint32>& b) {
|
| 271 |
+
return Vectorized<c10::qint32>{vec_or(a.vec0(), b.vec0()), vec_or(a.vec1(), b.vec1())};
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
template <>
|
| 275 |
+
Vectorized<c10::qint32> C10_ALWAYS_INLINE operator^(const Vectorized<c10::qint32>& a, const Vectorized<c10::qint32>& b) {
|
| 276 |
+
return Vectorized<c10::qint32>{vec_xor(a.vec0(), b.vec0()), vec_xor(a.vec1(), b.vec1())};
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
} // namespace
|
| 280 |
+
} // namespace vec
|
| 281 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/v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sx_helpers.h>
|
| 6 |
+
#include <c10/util/qint8.h>
|
| 7 |
+
#include <array>
|
| 8 |
+
|
| 9 |
+
// This file defines Vectorized<> for the quantized types.
|
| 10 |
+
//
|
| 11 |
+
//
|
| 12 |
+
// Currently, we simply use these classes as efficient converters between
|
| 13 |
+
// the quantized types and Vectorized<float>, usually in bandwidth-bound cases
|
| 14 |
+
// where doing the arithmetic in full-precision is acceptable (e.g.
|
| 15 |
+
// elementwise operators).
|
| 16 |
+
//
|
| 17 |
+
//
|
| 18 |
+
// Conversions are as follows:
|
| 19 |
+
// Vectorized<qint8> -> 4x Vectorized<float>
|
| 20 |
+
//
|
| 21 |
+
// The size of the returned float vector is specified by the special
|
| 22 |
+
// constexpr function float_num_vecs. The type of the value returned
|
| 23 |
+
// from dequantize (and expected as an argument to quantize) is
|
| 24 |
+
// specified by float_vec_return_type.
|
| 25 |
+
//
|
| 26 |
+
// When writing kernels with these vectors, it is expected that floating-
|
| 27 |
+
// point operations will be carried out in a loop over Vectorized<T>::float_num_vecs
|
| 28 |
+
// iterations.
|
| 29 |
+
|
| 30 |
+
namespace at {
|
| 31 |
+
namespace vec {
|
| 32 |
+
inline namespace CPU_CAPABILITY {
|
| 33 |
+
|
| 34 |
+
template <>
|
| 35 |
+
struct Vectorized<c10::qint8> {
|
| 36 |
+
private:
|
| 37 |
+
union {
|
| 38 |
+
struct {
|
| 39 |
+
vint8 _vec0;
|
| 40 |
+
vint8 _vec1;
|
| 41 |
+
};
|
| 42 |
+
struct {
|
| 43 |
+
vbool8 _vecb0;
|
| 44 |
+
vbool8 _vecb1;
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
} __attribute__((__may_alias__));
|
| 48 |
+
|
| 49 |
+
public:
|
| 50 |
+
Vectorized() {}
|
| 51 |
+
using size_type = int;
|
| 52 |
+
static constexpr size_type size() {
|
| 53 |
+
return 32;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
static constexpr size_t float_num_vecs() {
|
| 57 |
+
return 4;
|
| 58 |
+
}
|
| 59 |
+
static constexpr int int_num_vecs() {
|
| 60 |
+
return 4;
|
| 61 |
+
}
|
| 62 |
+
using float_vec_return_type = std::array<Vectorized<float>, 4>;
|
| 63 |
+
using int_vec_return_type = std::array<Vectorized<c10::qint32>, 4>;
|
| 64 |
+
using value_type = typename c10::qint8::underlying;
|
| 65 |
+
using vec_internal_type = vint8;
|
| 66 |
+
using vec_internal_mask_type = vbool8;
|
| 67 |
+
// Broadcast constructor
|
| 68 |
+
C10_ALWAYS_INLINE Vectorized(const c10::qint8& val)
|
| 69 |
+
: _vec0{vec_splats(val.val_)}, _vec1{vec_splats(val.val_)} {}
|
| 70 |
+
|
| 71 |
+
C10_ALWAYS_INLINE Vectorized(const Vectorized<c10::qint8>& other)
|
| 72 |
+
: _vec0{other._vec0}, _vec1(other._vec1) {}
|
| 73 |
+
|
| 74 |
+
C10_ALWAYS_INLINE Vectorized(vint8 v) : _vec0{v}, _vec1{v} {}
|
| 75 |
+
C10_ALWAYS_INLINE Vectorized(vbool8 vmask) : _vecb0{vmask}, _vecb1{vmask} {}
|
| 76 |
+
C10_ALWAYS_INLINE Vectorized(vint8 v1, vint8 v2) : _vec0{v1}, _vec1{v2} {}
|
| 77 |
+
C10_ALWAYS_INLINE Vectorized(vbool8 v1, vbool8 v2) : _vecb0{v1}, _vecb1{v2} {}
|
| 78 |
+
|
| 79 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec0() const {
|
| 80 |
+
return _vec0;
|
| 81 |
+
}
|
| 82 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec1() const {
|
| 83 |
+
return _vec1;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
static C10_ALWAYS_INLINE Vectorized<c10::qint8> loadu(
|
| 87 |
+
const void* ptr,
|
| 88 |
+
int count = size()) {
|
| 89 |
+
if (count == size()) {
|
| 90 |
+
return {
|
| 91 |
+
vec_vsx_ld(offset0, reinterpret_cast<const vint8*>(ptr)),
|
| 92 |
+
vec_vsx_ld(offset16, reinterpret_cast<const vint8*>(ptr))};
|
| 93 |
+
}
|
| 94 |
+
__at_align__ value_type tmp_values[size()] = {};
|
| 95 |
+
std::memcpy(tmp_values, ptr, std::min(count, size()) * sizeof(value_type));
|
| 96 |
+
return {vec_vsx_ld(offset0, tmp_values), vec_vsx_ld(offset16, tmp_values)};
|
| 97 |
+
}
|
| 98 |
+
void C10_ALWAYS_INLINE store(void* ptr, int count = size()) const {
|
| 99 |
+
if (count == size()) {
|
| 100 |
+
vec_vsx_st(_vec0, offset0, reinterpret_cast<value_type*>(ptr));
|
| 101 |
+
vec_vsx_st(_vec1, offset16, reinterpret_cast<value_type*>(ptr));
|
| 102 |
+
} else if (count > 0) {
|
| 103 |
+
__at_align__ value_type tmp_values[size()];
|
| 104 |
+
vec_vsx_st(_vec0, offset0, tmp_values);
|
| 105 |
+
vec_vsx_st(_vec1, offset16, tmp_values);
|
| 106 |
+
std::memcpy(
|
| 107 |
+
ptr, tmp_values, std::min(count, size()) * sizeof(value_type));
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
public:
|
| 112 |
+
float_vec_return_type C10_ALWAYS_INLINE dequantize(
|
| 113 |
+
Vectorized<float> scale,
|
| 114 |
+
Vectorized<float> zero_point,
|
| 115 |
+
Vectorized<float> scale_zp_premul) const {
|
| 116 |
+
vint16 vecshi0 = vec_unpackh(_vec0);
|
| 117 |
+
vint16 vecshi1 = vec_unpackl(_vec0);
|
| 118 |
+
|
| 119 |
+
vint16 vecshi2 = vec_unpackh(_vec1);
|
| 120 |
+
vint16 vecshi3 = vec_unpackl(_vec1);
|
| 121 |
+
|
| 122 |
+
vint32 veci0 = vec_unpackh(vecshi0);
|
| 123 |
+
vint32 veci1 = vec_unpackl(vecshi0);
|
| 124 |
+
|
| 125 |
+
vint32 veci2 = vec_unpackh(vecshi1);
|
| 126 |
+
vint32 veci3 = vec_unpackl(vecshi1);
|
| 127 |
+
|
| 128 |
+
vint32 veci4 = vec_unpackh(vecshi2);
|
| 129 |
+
vint32 veci5 = vec_unpackl(vecshi2);
|
| 130 |
+
|
| 131 |
+
vint32 veci6 = vec_unpackh(vecshi3);
|
| 132 |
+
vint32 veci7 = vec_unpackl(vecshi3);
|
| 133 |
+
|
| 134 |
+
vfloat32 vecf0_0 = vec_float(veci0);
|
| 135 |
+
vfloat32 vecf1_0 = vec_float(veci1);
|
| 136 |
+
|
| 137 |
+
vfloat32 vecf0_1 = vec_float(veci2);
|
| 138 |
+
vfloat32 vecf1_1 = vec_float(veci3);
|
| 139 |
+
|
| 140 |
+
vfloat32 vecf0_2 = vec_float(veci4);
|
| 141 |
+
vfloat32 vecf1_2 = vec_float(veci5);
|
| 142 |
+
|
| 143 |
+
vfloat32 vecf0_3 = vec_float(veci6);
|
| 144 |
+
vfloat32 vecf1_3 = vec_float(veci7);
|
| 145 |
+
vfloat32 scale_vec0 = scale.vec0();
|
| 146 |
+
vfloat32 scale_vec1 = scale.vec1();
|
| 147 |
+
vfloat32 scale_zp_premul0 = scale_zp_premul.vec0();
|
| 148 |
+
vfloat32 scale_zp_premul1 = scale_zp_premul.vec1();
|
| 149 |
+
return {
|
| 150 |
+
Vectorized<float>{
|
| 151 |
+
vec_madd(scale_vec0, vecf0_0, scale_zp_premul0),
|
| 152 |
+
vec_madd(scale_vec1, vecf1_0, scale_zp_premul1)},
|
| 153 |
+
Vectorized<float>{
|
| 154 |
+
vec_madd(scale_vec0, vecf0_1, scale_zp_premul0),
|
| 155 |
+
vec_madd(scale_vec1, vecf1_1, scale_zp_premul1)},
|
| 156 |
+
Vectorized<float>{
|
| 157 |
+
vec_madd(scale_vec0, vecf0_2, scale_zp_premul0),
|
| 158 |
+
vec_madd(scale_vec1, vecf1_2, scale_zp_premul1)},
|
| 159 |
+
Vectorized<float>{
|
| 160 |
+
vec_madd(scale_vec0, vecf0_3, scale_zp_premul0),
|
| 161 |
+
vec_madd(scale_vec1, vecf1_3, scale_zp_premul1)}};
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
float_vec_return_type C10_ALWAYS_INLINE dequantize(
|
| 165 |
+
Vectorized<float> scale,
|
| 166 |
+
Vectorized<float> zero_point) const {
|
| 167 |
+
vint16 vecshi0 = vec_unpackh(_vec0);
|
| 168 |
+
vint16 vecshi1 = vec_unpackl(_vec0);
|
| 169 |
+
|
| 170 |
+
vint16 vecshi2 = vec_unpackh(_vec1);
|
| 171 |
+
vint16 vecshi3 = vec_unpackl(_vec1);
|
| 172 |
+
|
| 173 |
+
vint32 veci0 = vec_unpackh(vecshi0);
|
| 174 |
+
vint32 veci1 = vec_unpackl(vecshi0);
|
| 175 |
+
|
| 176 |
+
vint32 veci2 = vec_unpackh(vecshi1);
|
| 177 |
+
vint32 veci3 = vec_unpackl(vecshi1);
|
| 178 |
+
|
| 179 |
+
vint32 veci4 = vec_unpackh(vecshi2);
|
| 180 |
+
vint32 veci5 = vec_unpackl(vecshi2);
|
| 181 |
+
|
| 182 |
+
vint32 veci6 = vec_unpackh(vecshi3);
|
| 183 |
+
vint32 veci7 = vec_unpackl(vecshi3);
|
| 184 |
+
|
| 185 |
+
vfloat32 vecf0_0 = vec_float(veci0);
|
| 186 |
+
vfloat32 vecf1_0 = vec_float(veci1);
|
| 187 |
+
|
| 188 |
+
vfloat32 vecf0_1 = vec_float(veci2);
|
| 189 |
+
vfloat32 vecf1_1 = vec_float(veci3);
|
| 190 |
+
|
| 191 |
+
vfloat32 vecf0_2 = vec_float(veci4);
|
| 192 |
+
vfloat32 vecf1_2 = vec_float(veci5);
|
| 193 |
+
|
| 194 |
+
vfloat32 vecf0_3 = vec_float(veci6);
|
| 195 |
+
vfloat32 vecf1_3 = vec_float(veci7);
|
| 196 |
+
vfloat32 scale_vec0 = scale.vec0();
|
| 197 |
+
vfloat32 scale_vec1 = scale.vec1();
|
| 198 |
+
vfloat32 zero_point0 = zero_point.vec0();
|
| 199 |
+
vfloat32 zero_point1 = zero_point.vec1();
|
| 200 |
+
return {
|
| 201 |
+
Vectorized<float>{
|
| 202 |
+
(vecf0_0 - zero_point0) * scale_vec0,
|
| 203 |
+
(vecf1_0 - zero_point1) * scale_vec1},
|
| 204 |
+
Vectorized<float>{
|
| 205 |
+
(vecf0_1 - zero_point0) * scale_vec0,
|
| 206 |
+
(vecf1_1 - zero_point1) * scale_vec1},
|
| 207 |
+
Vectorized<float>{
|
| 208 |
+
(vecf0_2 - zero_point0) * scale_vec0,
|
| 209 |
+
(vecf1_2 - zero_point1) * scale_vec1},
|
| 210 |
+
Vectorized<float>{
|
| 211 |
+
(vecf0_3 - zero_point0) * scale_vec0,
|
| 212 |
+
(vecf1_3 - zero_point1) * scale_vec1}};
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
static Vectorized<c10::qint8> quantize(
|
| 216 |
+
const float_vec_return_type& rhs,
|
| 217 |
+
float scale,
|
| 218 |
+
int32_t zero_point,
|
| 219 |
+
float inverse_scale) {
|
| 220 |
+
// constexpr int32_t min_val = std::numeric_limits<value_type>::min();
|
| 221 |
+
// constexpr int32_t max_val = std::numeric_limits<value_type>::max();
|
| 222 |
+
|
| 223 |
+
vfloat32 inverse_scale_v = vec_splats(inverse_scale);
|
| 224 |
+
vfloat32 vec_zero_point = vec_splats((float)zero_point);
|
| 225 |
+
// vint32 vmin = vec_splats(min_val);
|
| 226 |
+
// vint32 vmax = vec_splats(max_val);
|
| 227 |
+
|
| 228 |
+
Vectorized<float> vf0 = rhs[0];
|
| 229 |
+
Vectorized<float> vf1 = rhs[1];
|
| 230 |
+
Vectorized<float> vf2 = rhs[2];
|
| 231 |
+
Vectorized<float> vf3 = rhs[3];
|
| 232 |
+
vfloat32 vecf0 = vf0.vec0();
|
| 233 |
+
vfloat32 vecf1 = vf0.vec1();
|
| 234 |
+
vfloat32 vecf2 = vf1.vec0();
|
| 235 |
+
vfloat32 vecf3 = vf1.vec1();
|
| 236 |
+
|
| 237 |
+
vfloat32 vecf4 = vf2.vec0();
|
| 238 |
+
vfloat32 vecf5 = vf2.vec1();
|
| 239 |
+
vfloat32 vecf6 = vf3.vec0();
|
| 240 |
+
vfloat32 vecf7 = vf3.vec1();
|
| 241 |
+
|
| 242 |
+
vecf0 = vec_mul(vecf0, inverse_scale_v);
|
| 243 |
+
vecf1 = vec_mul(vecf1, inverse_scale_v);
|
| 244 |
+
vecf2 = vec_mul(vecf2, inverse_scale_v);
|
| 245 |
+
vecf3 = vec_mul(vecf3, inverse_scale_v);
|
| 246 |
+
|
| 247 |
+
vecf4 = vec_mul(vecf4, inverse_scale_v);
|
| 248 |
+
vecf5 = vec_mul(vecf5, inverse_scale_v);
|
| 249 |
+
vecf6 = vec_mul(vecf6, inverse_scale_v);
|
| 250 |
+
vecf7 = vec_mul(vecf7, inverse_scale_v);
|
| 251 |
+
|
| 252 |
+
vecf0 = vec_add(vec_rint(vecf0), vec_zero_point);
|
| 253 |
+
vecf1 = vec_add(vec_rint(vecf1), vec_zero_point);
|
| 254 |
+
vecf2 = vec_add(vec_rint(vecf2), vec_zero_point);
|
| 255 |
+
vecf3 = vec_add(vec_rint(vecf3), vec_zero_point);
|
| 256 |
+
|
| 257 |
+
vecf4 = vec_add(vec_rint(vecf4), vec_zero_point);
|
| 258 |
+
vecf5 = vec_add(vec_rint(vecf5), vec_zero_point);
|
| 259 |
+
vecf6 = vec_add(vec_rint(vecf6), vec_zero_point);
|
| 260 |
+
vecf7 = vec_add(vec_rint(vecf7), vec_zero_point);
|
| 261 |
+
|
| 262 |
+
vint32 veci0 = vec_signed(vecf0);
|
| 263 |
+
vint32 veci1 = vec_signed(vecf1);
|
| 264 |
+
vint32 veci2 = vec_signed(vecf2);
|
| 265 |
+
vint32 veci3 = vec_signed(vecf3);
|
| 266 |
+
|
| 267 |
+
vint32 veci4 = vec_signed(vecf4);
|
| 268 |
+
vint32 veci5 = vec_signed(vecf5);
|
| 269 |
+
vint32 veci6 = vec_signed(vecf6);
|
| 270 |
+
vint32 veci7 = vec_signed(vecf7);
|
| 271 |
+
|
| 272 |
+
// veci0 = vec_min(vmax, vec_max( vmin, vecf0)) ;
|
| 273 |
+
// veci1 = vec_min(vmax, vec_max( vmin, vecf1)) ;
|
| 274 |
+
// veci2 = vec_min(vmax, vec_max( vmin, vecf2)) ;
|
| 275 |
+
// veci3 = vec_min(vmax, vec_max( vmin, vecf3)) ;
|
| 276 |
+
|
| 277 |
+
// veci4 = vec_min(vmax, vec_max( vmin, vecf4)) ;
|
| 278 |
+
// veci5 = vec_min(vmax, vec_max( vmin, vecf5)) ;
|
| 279 |
+
// veci6 = vec_min(vmax, vec_max( vmin, vecf6)) ;
|
| 280 |
+
// veci7 = vec_min(vmax, vec_max( vmin, vecf7)) ;
|
| 281 |
+
// vec_packs CLAMP already
|
| 282 |
+
vint16 vecshi0 = vec_packs(veci0, veci1);
|
| 283 |
+
vint16 vecshi1 = vec_packs(veci2, veci3);
|
| 284 |
+
vint16 vecshi2 = vec_packs(veci4, veci5);
|
| 285 |
+
vint16 vecshi3 = vec_packs(veci6, veci7);
|
| 286 |
+
|
| 287 |
+
vint8 vec0 = vec_packs(vecshi0, vecshi1);
|
| 288 |
+
vint8 vec1 = vec_packs(vecshi2, vecshi3);
|
| 289 |
+
|
| 290 |
+
return {vec0, vec1};
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
Vectorized<c10::qint8> C10_ALWAYS_INLINE relu(Vectorized<c10::qint8> zero_point) const {
|
| 294 |
+
return {vec_max(_vec0, zero_point._vec0), vec_max(_vec1, zero_point._vec1)};
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
Vectorized<c10::qint8> C10_ALWAYS_INLINE
|
| 298 |
+
relu6(Vectorized<c10::qint8> zero_point, Vectorized<c10::qint8> q_six) const {
|
| 299 |
+
vint8 max0 = vec_max(_vec0, zero_point._vec0);
|
| 300 |
+
vint8 max1 = vec_max(_vec1, zero_point._vec1);
|
| 301 |
+
return {vec_min(max0, q_six._vec0), vec_min(max1, q_six._vec1)};
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
int_vec_return_type widening_subtract(Vectorized<c10::qint8> b) const {
|
| 305 |
+
vint16 vecshi0 = vec_unpackh(_vec0);
|
| 306 |
+
vint16 vecBshi0 = vec_unpackh(b._vec0);
|
| 307 |
+
vint16 vecshi1 = vec_unpackl(_vec0);
|
| 308 |
+
vint16 vecBshi1 = vec_unpackl(b._vec0);
|
| 309 |
+
|
| 310 |
+
vint16 vecshi2 = vec_unpackh(_vec1);
|
| 311 |
+
vint16 vecBshi2 = vec_unpackh(b._vec1);
|
| 312 |
+
vint16 vecshi3 = vec_unpackl(_vec1);
|
| 313 |
+
vint16 vecBshi3 = vec_unpackl(b._vec1);
|
| 314 |
+
|
| 315 |
+
vint32 veci0 = vec_unpackh(vecshi0);
|
| 316 |
+
vint32 vecBi0 = vec_unpackh(vecBshi0);
|
| 317 |
+
vint32 veci1 = vec_unpackl(vecshi0);
|
| 318 |
+
vint32 vecBi1 = vec_unpackl(vecBshi0);
|
| 319 |
+
|
| 320 |
+
vint32 veci2 = vec_unpackh(vecshi1);
|
| 321 |
+
vint32 vecBi2 = vec_unpackh(vecBshi1);
|
| 322 |
+
vint32 veci3 = vec_unpackl(vecshi1);
|
| 323 |
+
vint32 vecBi3 = vec_unpackl(vecBshi1);
|
| 324 |
+
|
| 325 |
+
vint32 veci4 = vec_unpackh(vecshi2);
|
| 326 |
+
vint32 vecBi4 = vec_unpackh(vecBshi2);
|
| 327 |
+
vint32 veci5 = vec_unpackl(vecshi2);
|
| 328 |
+
vint32 vecBi5 = vec_unpackl(vecBshi2);
|
| 329 |
+
|
| 330 |
+
vint32 veci6 = vec_unpackh(vecshi3);
|
| 331 |
+
vint32 vecBi6 = vec_unpackh(vecBshi3);
|
| 332 |
+
vint32 veci7 = vec_unpackl(vecshi3);
|
| 333 |
+
vint32 vecBi7 = vec_unpackl(vecBshi3);
|
| 334 |
+
|
| 335 |
+
return {
|
| 336 |
+
Vectorized<c10::qint32>(veci0 - vecBi0, veci1 - vecBi1),
|
| 337 |
+
Vectorized<c10::qint32>(veci2 - vecBi2, veci3 - vecBi3),
|
| 338 |
+
Vectorized<c10::qint32>(veci4 - vecBi4, veci5 - vecBi5),
|
| 339 |
+
Vectorized<c10::qint32>(veci6 - vecBi6, veci7 - vecBi7)};
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
static Vectorized<c10::qint8> requantize_from_int(
|
| 343 |
+
const int_vec_return_type& inp,
|
| 344 |
+
float multiplier,
|
| 345 |
+
int32_t zero_point) {
|
| 346 |
+
vfloat32 vec_multiplier = vec_splats(multiplier);
|
| 347 |
+
vint32 vec_zero_point = vec_splats(zero_point);
|
| 348 |
+
|
| 349 |
+
Vectorized<c10::qint32> vi0 = inp[0];
|
| 350 |
+
Vectorized<c10::qint32> vi1 = inp[1];
|
| 351 |
+
Vectorized<c10::qint32> vi2 = inp[2];
|
| 352 |
+
Vectorized<c10::qint32> vi3 = inp[3];
|
| 353 |
+
|
| 354 |
+
vfloat32 vecf0 = vec_float(vi0.vec0());
|
| 355 |
+
vfloat32 vecf1 = vec_float(vi0.vec1());
|
| 356 |
+
vfloat32 vecf2 = vec_float(vi1.vec0());
|
| 357 |
+
vfloat32 vecf3 = vec_float(vi1.vec1());
|
| 358 |
+
|
| 359 |
+
vfloat32 vecf4 = vec_float(vi2.vec0());
|
| 360 |
+
vfloat32 vecf5 = vec_float(vi2.vec1());
|
| 361 |
+
vfloat32 vecf6 = vec_float(vi3.vec0());
|
| 362 |
+
vfloat32 vecf7 = vec_float(vi3.vec1());
|
| 363 |
+
|
| 364 |
+
vecf0 = vec_mul(vecf0, vec_multiplier);
|
| 365 |
+
vecf1 = vec_mul(vecf1, vec_multiplier);
|
| 366 |
+
vecf2 = vec_mul(vecf2, vec_multiplier);
|
| 367 |
+
vecf3 = vec_mul(vecf3, vec_multiplier);
|
| 368 |
+
|
| 369 |
+
vecf4 = vec_mul(vecf4, vec_multiplier);
|
| 370 |
+
vecf5 = vec_mul(vecf5, vec_multiplier);
|
| 371 |
+
vecf6 = vec_mul(vecf6, vec_multiplier);
|
| 372 |
+
vecf7 = vec_mul(vecf7, vec_multiplier);
|
| 373 |
+
|
| 374 |
+
vecf0 = vec_rint(vecf0);
|
| 375 |
+
vecf1 = vec_rint(vecf1);
|
| 376 |
+
vecf2 = vec_rint(vecf2);
|
| 377 |
+
vecf3 = vec_rint(vecf3);
|
| 378 |
+
|
| 379 |
+
vecf4 = vec_rint(vecf4);
|
| 380 |
+
vecf5 = vec_rint(vecf5);
|
| 381 |
+
vecf6 = vec_rint(vecf6);
|
| 382 |
+
vecf7 = vec_rint(vecf7);
|
| 383 |
+
|
| 384 |
+
vint32 veci0 = vec_signed(vecf0);
|
| 385 |
+
vint32 veci1 = vec_signed(vecf1);
|
| 386 |
+
vint32 veci2 = vec_signed(vecf2);
|
| 387 |
+
vint32 veci3 = vec_signed(vecf3);
|
| 388 |
+
|
| 389 |
+
vint32 veci4 = vec_signed(vecf4);
|
| 390 |
+
vint32 veci5 = vec_signed(vecf5);
|
| 391 |
+
vint32 veci6 = vec_signed(vecf6);
|
| 392 |
+
vint32 veci7 = vec_signed(vecf7);
|
| 393 |
+
|
| 394 |
+
veci0 = vec_add(veci0, vec_zero_point);
|
| 395 |
+
veci1 = vec_add(veci1, vec_zero_point);
|
| 396 |
+
veci2 = vec_add(veci2, vec_zero_point);
|
| 397 |
+
veci3 = vec_add(veci3, vec_zero_point);
|
| 398 |
+
|
| 399 |
+
veci4 = vec_add(veci4, vec_zero_point);
|
| 400 |
+
veci5 = vec_add(veci5, vec_zero_point);
|
| 401 |
+
veci6 = vec_add(veci6, vec_zero_point);
|
| 402 |
+
veci7 = vec_add(veci7, vec_zero_point);
|
| 403 |
+
|
| 404 |
+
vint16 vecshi0 = vec_packs(veci0, veci1);
|
| 405 |
+
vint16 vecshi1 = vec_packs(veci2, veci3);
|
| 406 |
+
vint16 vecshi2 = vec_packs(veci4, veci5);
|
| 407 |
+
vint16 vecshi3 = vec_packs(veci6, veci7);
|
| 408 |
+
|
| 409 |
+
vint8 vec0 = vec_packs(vecshi0, vecshi1);
|
| 410 |
+
vint8 vec1 = vec_packs(vecshi2, vecshi3);
|
| 411 |
+
|
| 412 |
+
return {vec0, vec1};
|
| 413 |
+
}
|
| 414 |
+
|
| 415 |
+
DEFINE_MEMBER_OP(operator==, c10::qint8, vec_cmpeq)
|
| 416 |
+
DEFINE_MEMBER_OP(operator!=, c10::qint8, vec_cmpne)
|
| 417 |
+
DEFINE_MEMBER_OP(operator<, c10::qint8, vec_cmplt)
|
| 418 |
+
DEFINE_MEMBER_OP(operator<=, c10::qint8, vec_cmple)
|
| 419 |
+
DEFINE_MEMBER_OP(operator>, c10::qint8, vec_cmpgt)
|
| 420 |
+
DEFINE_MEMBER_OP(operator>=, c10::qint8, vec_cmpge)
|
| 421 |
+
DEFINE_MEMBER_OP(operator+, c10::qint8, vec_add)
|
| 422 |
+
DEFINE_MEMBER_OP(operator-, c10::qint8, vec_sub)
|
| 423 |
+
DEFINE_MEMBER_OP(operator*, c10::qint8, vec_mul)
|
| 424 |
+
DEFINE_MEMBER_EMULATE_BINARY_OP(operator/, c10::qint8, /)
|
| 425 |
+
DEFINE_MEMBER_OP(maximum, c10::qint8, vec_max)
|
| 426 |
+
DEFINE_MEMBER_OP(minimum, c10::qint8, vec_min)
|
| 427 |
+
DEFINE_MEMBER_OP(operator&, c10::qint8, vec_and)
|
| 428 |
+
DEFINE_MEMBER_OP(operator|, c10::qint8, vec_or)
|
| 429 |
+
DEFINE_MEMBER_OP(operator^, c10::qint8, vec_xor)
|
| 430 |
+
};
|
| 431 |
+
|
| 432 |
+
template <>
|
| 433 |
+
Vectorized<c10::qint8> inline maximum(
|
| 434 |
+
const Vectorized<c10::qint8>& a,
|
| 435 |
+
const Vectorized<c10::qint8>& b) {
|
| 436 |
+
return a.maximum(b);
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
template <>
|
| 440 |
+
Vectorized<c10::qint8> inline minimum(
|
| 441 |
+
const Vectorized<c10::qint8>& a,
|
| 442 |
+
const Vectorized<c10::qint8>& b) {
|
| 443 |
+
return a.minimum(b);
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
template <>
|
| 447 |
+
Vectorized<c10::qint8> C10_ALWAYS_INLINE operator+(const Vectorized<c10::qint8>& a, const Vectorized<c10::qint8>& b) {
|
| 448 |
+
return Vectorized<c10::qint8>{vec_add(a.vec0(), b.vec0()), vec_add(a.vec1(), b.vec1())};
|
| 449 |
+
}
|
| 450 |
+
|
| 451 |
+
template <>
|
| 452 |
+
Vectorized<c10::qint8> C10_ALWAYS_INLINE operator-(const Vectorized<c10::qint8>& a, const Vectorized<c10::qint8>& b) {
|
| 453 |
+
return Vectorized<c10::qint8>{vec_sub(a.vec0(), b.vec0()), vec_sub(a.vec1(), b.vec1())};
|
| 454 |
+
}
|
| 455 |
+
|
| 456 |
+
template <>
|
| 457 |
+
Vectorized<c10::qint8> C10_ALWAYS_INLINE operator*(const Vectorized<c10::qint8>& a, const Vectorized<c10::qint8>& b) {
|
| 458 |
+
return Vectorized<c10::qint8>{vec_mul(a.vec0(), b.vec0()), vec_mul(a.vec1(), b.vec1())};
|
| 459 |
+
}
|
| 460 |
+
|
| 461 |
+
template <>
|
| 462 |
+
Vectorized<c10::qint8> C10_ALWAYS_INLINE operator/(const Vectorized<c10::qint8>& a, const Vectorized<c10::qint8>& b) {
|
| 463 |
+
return Vectorized<c10::qint8>{a.vec0()/b.vec0(), a.vec1()/b.vec1()};
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
template <>
|
| 467 |
+
Vectorized<c10::qint8> C10_ALWAYS_INLINE operator&(const Vectorized<c10::qint8>& a, const Vectorized<c10::qint8>& b) {
|
| 468 |
+
return Vectorized<c10::qint8>{vec_and(a.vec0(), b.vec0()), vec_and(a.vec1(), b.vec1())};
|
| 469 |
+
}
|
| 470 |
+
|
| 471 |
+
template <>
|
| 472 |
+
Vectorized<c10::qint8> C10_ALWAYS_INLINE operator|(const Vectorized<c10::qint8>& a, const Vectorized<c10::qint8>& b) {
|
| 473 |
+
return Vectorized<c10::qint8>{vec_or(a.vec0(), b.vec0()), vec_or(a.vec1(), b.vec1())};
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
template <>
|
| 477 |
+
Vectorized<c10::qint8> C10_ALWAYS_INLINE operator^(const Vectorized<c10::qint8>& a, const Vectorized<c10::qint8>& b) {
|
| 478 |
+
return Vectorized<c10::qint8>{vec_xor(a.vec0(), b.vec0()), vec_xor(a.vec1(), b.vec1())};
|
| 479 |
+
}
|
| 480 |
+
|
| 481 |
+
} // namespace
|
| 482 |
+
} // namespace vec
|
| 483 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vec256_quint8_vsx.h
ADDED
|
@@ -0,0 +1,501 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 4 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 5 |
+
#include <ATen/cpu/vec/vec256/vsx/vsx_helpers.h>
|
| 6 |
+
|
| 7 |
+
#include <c10/util/irange.h>
|
| 8 |
+
#include <c10/util/quint8.h>
|
| 9 |
+
#include <array>
|
| 10 |
+
|
| 11 |
+
// This file defines Vectorized<> for the quantized types.
|
| 12 |
+
//
|
| 13 |
+
//
|
| 14 |
+
// Currently, we simply use these classes as efficient converters between
|
| 15 |
+
// the quantized types and Vectorized<float>, usually in bandwidth-bound cases
|
| 16 |
+
// where doing the arithmetic in full-precision is acceptable (e.g.
|
| 17 |
+
// elementwise operators).
|
| 18 |
+
//
|
| 19 |
+
//
|
| 20 |
+
// Conversions are as follows:
|
| 21 |
+
// Vectorized<quint8> -> 4x Vectorized<float>
|
| 22 |
+
//
|
| 23 |
+
// The size of the returned float vector is specified by the special
|
| 24 |
+
// constexpr function float_num_vecs. The type of the value returned
|
| 25 |
+
// from dequantize (and expected as an argument to quantize) is
|
| 26 |
+
// specified by float_vec_return_type.
|
| 27 |
+
//
|
| 28 |
+
// When writing kernels with these vectors, it is expected that floating-
|
| 29 |
+
// point operations will be carried out in a loop over Vectorized<T>::float_num_vecs
|
| 30 |
+
// iterations.
|
| 31 |
+
|
| 32 |
+
namespace at {
|
| 33 |
+
namespace vec {
|
| 34 |
+
inline namespace CPU_CAPABILITY {
|
| 35 |
+
|
| 36 |
+
const vint16 mask_unsigned = vec_splats((short int)0xFF);
|
| 37 |
+
template <>
|
| 38 |
+
struct Vectorized<c10::quint8> {
|
| 39 |
+
private:
|
| 40 |
+
union {
|
| 41 |
+
struct {
|
| 42 |
+
vuint8 _vec0;
|
| 43 |
+
vuint8 _vec1;
|
| 44 |
+
};
|
| 45 |
+
struct {
|
| 46 |
+
vbool8 _vecb0;
|
| 47 |
+
vbool8 _vecb1;
|
| 48 |
+
};
|
| 49 |
+
|
| 50 |
+
} __attribute__((__may_alias__));
|
| 51 |
+
|
| 52 |
+
public:
|
| 53 |
+
Vectorized() {}
|
| 54 |
+
using size_type = int;
|
| 55 |
+
static constexpr size_type size() {
|
| 56 |
+
return 32;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
static constexpr size_t float_num_vecs() {
|
| 60 |
+
return 4;
|
| 61 |
+
}
|
| 62 |
+
static constexpr int int_num_vecs() {
|
| 63 |
+
return 4;
|
| 64 |
+
}
|
| 65 |
+
using float_vec_return_type = std::array<Vectorized<float>, 4>;
|
| 66 |
+
using int_vec_return_type = std::array<Vectorized<c10::qint32>, 4>;
|
| 67 |
+
using value_type = typename c10::quint8::underlying;
|
| 68 |
+
using vec_internal_type = vuint8;
|
| 69 |
+
using vec_internal_mask_type = vbool8;
|
| 70 |
+
// Broadcast constructor
|
| 71 |
+
C10_ALWAYS_INLINE Vectorized(const c10::quint8& val)
|
| 72 |
+
: _vec0(vec_splats(val.val_)), _vec1(vec_splats(val.val_)) {}
|
| 73 |
+
|
| 74 |
+
C10_ALWAYS_INLINE Vectorized(const Vectorized<c10::quint8>& other)
|
| 75 |
+
: _vec0{other._vec0}, _vec1(other._vec1) {}
|
| 76 |
+
|
| 77 |
+
C10_ALWAYS_INLINE Vectorized(vuint8 v) : _vec0{v}, _vec1{v} {}
|
| 78 |
+
C10_ALWAYS_INLINE Vectorized(vbool8 vmask) : _vecb0{vmask}, _vecb1{vmask} {}
|
| 79 |
+
C10_ALWAYS_INLINE Vectorized(vuint8 v1, vuint8 v2) : _vec0{v1}, _vec1{v2} {}
|
| 80 |
+
C10_ALWAYS_INLINE Vectorized(vbool8 v1, vbool8 v2) : _vecb0{v1}, _vecb1{v2} {}
|
| 81 |
+
|
| 82 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec0() const {
|
| 83 |
+
return _vec0;
|
| 84 |
+
}
|
| 85 |
+
C10_ALWAYS_INLINE const vec_internal_type& vec1() const {
|
| 86 |
+
return _vec1;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
static C10_ALWAYS_INLINE Vectorized<c10::quint8> loadu(
|
| 90 |
+
const void* ptr,
|
| 91 |
+
int count = size()) {
|
| 92 |
+
if (count == size()) {
|
| 93 |
+
return {
|
| 94 |
+
vec_vsx_ld(offset0, reinterpret_cast<const value_type*>(ptr)),
|
| 95 |
+
vec_vsx_ld(offset16, reinterpret_cast<const value_type*>(ptr))};
|
| 96 |
+
}
|
| 97 |
+
__at_align__ value_type tmp_values[size()] = {};
|
| 98 |
+
std::memcpy(tmp_values, ptr, std::min(count, size()) * sizeof(value_type));
|
| 99 |
+
return {vec_vsx_ld(offset0, tmp_values), vec_vsx_ld(offset16, tmp_values)};
|
| 100 |
+
}
|
| 101 |
+
void C10_ALWAYS_INLINE store(void* ptr, int count = size()) const {
|
| 102 |
+
if (count == size()) {
|
| 103 |
+
vec_vsx_st(_vec0, offset0, reinterpret_cast<value_type*>(ptr));
|
| 104 |
+
vec_vsx_st(_vec1, offset16, reinterpret_cast<value_type*>(ptr));
|
| 105 |
+
} else if (count > 0) {
|
| 106 |
+
__at_align__ value_type tmp_values[size()];
|
| 107 |
+
vec_vsx_st(_vec0, offset0, tmp_values);
|
| 108 |
+
vec_vsx_st(_vec1, offset16, tmp_values);
|
| 109 |
+
std::memcpy(
|
| 110 |
+
ptr, tmp_values, std::min(count, size()) * sizeof(value_type));
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
public:
|
| 115 |
+
float_vec_return_type C10_ALWAYS_INLINE dequantize(
|
| 116 |
+
Vectorized<float> scale,
|
| 117 |
+
Vectorized<float> zero_point,
|
| 118 |
+
Vectorized<float> scale_zp_premul) const {
|
| 119 |
+
// unpacking unsigned as signed
|
| 120 |
+
vint16 vecshi0 = vec_unpackh((vint8)_vec0);
|
| 121 |
+
vint16 vecshi1 = vec_unpackl((vint8)_vec0);
|
| 122 |
+
|
| 123 |
+
vint16 vecshi2 = vec_unpackh((vint8)_vec1);
|
| 124 |
+
vint16 vecshi3 = vec_unpackl((vint8)_vec1);
|
| 125 |
+
|
| 126 |
+
// signed -> unsigned
|
| 127 |
+
vecshi0 = vec_and(vecshi0, mask_unsigned);
|
| 128 |
+
vecshi1 = vec_and(vecshi1, mask_unsigned);
|
| 129 |
+
|
| 130 |
+
vecshi2 = vec_and(vecshi2, mask_unsigned);
|
| 131 |
+
vecshi3 = vec_and(vecshi3, mask_unsigned);
|
| 132 |
+
|
| 133 |
+
vint32 veci0 = vec_unpackh(vecshi0);
|
| 134 |
+
vint32 veci1 = vec_unpackl(vecshi0);
|
| 135 |
+
|
| 136 |
+
vint32 veci2 = vec_unpackh(vecshi1);
|
| 137 |
+
vint32 veci3 = vec_unpackl(vecshi1);
|
| 138 |
+
|
| 139 |
+
vint32 veci4 = vec_unpackh(vecshi2);
|
| 140 |
+
vint32 veci5 = vec_unpackl(vecshi2);
|
| 141 |
+
|
| 142 |
+
vint32 veci6 = vec_unpackh(vecshi3);
|
| 143 |
+
vint32 veci7 = vec_unpackl(vecshi3);
|
| 144 |
+
|
| 145 |
+
vfloat32 vecf0_0 = vec_float(veci0);
|
| 146 |
+
vfloat32 vecf1_0 = vec_float(veci1);
|
| 147 |
+
|
| 148 |
+
vfloat32 vecf0_1 = vec_float(veci2);
|
| 149 |
+
vfloat32 vecf1_1 = vec_float(veci3);
|
| 150 |
+
|
| 151 |
+
vfloat32 vecf0_2 = vec_float(veci4);
|
| 152 |
+
vfloat32 vecf1_2 = vec_float(veci5);
|
| 153 |
+
|
| 154 |
+
vfloat32 vecf0_3 = vec_float(veci6);
|
| 155 |
+
vfloat32 vecf1_3 = vec_float(veci7);
|
| 156 |
+
vfloat32 scale_vec0 = scale.vec0();
|
| 157 |
+
vfloat32 scale_vec1 = scale.vec1();
|
| 158 |
+
vfloat32 scale_zp_premul0 = scale_zp_premul.vec0();
|
| 159 |
+
vfloat32 scale_zp_premul1 = scale_zp_premul.vec1();
|
| 160 |
+
return {
|
| 161 |
+
Vectorized<float>{
|
| 162 |
+
vec_madd(scale_vec0, vecf0_0, scale_zp_premul0),
|
| 163 |
+
vec_madd(scale_vec1, vecf1_0, scale_zp_premul1)},
|
| 164 |
+
Vectorized<float>{
|
| 165 |
+
vec_madd(scale_vec0, vecf0_1, scale_zp_premul0),
|
| 166 |
+
vec_madd(scale_vec1, vecf1_1, scale_zp_premul1)},
|
| 167 |
+
Vectorized<float>{
|
| 168 |
+
vec_madd(scale_vec0, vecf0_2, scale_zp_premul0),
|
| 169 |
+
vec_madd(scale_vec1, vecf1_2, scale_zp_premul1)},
|
| 170 |
+
Vectorized<float>{
|
| 171 |
+
vec_madd(scale_vec0, vecf0_3, scale_zp_premul0),
|
| 172 |
+
vec_madd(scale_vec1, vecf1_3, scale_zp_premul1)}};
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
float_vec_return_type C10_ALWAYS_INLINE dequantize(
|
| 176 |
+
Vectorized<float> scale,
|
| 177 |
+
Vectorized<float> zero_point) const {
|
| 178 |
+
// unpacking unsigned as signed
|
| 179 |
+
vint16 vecshi0 = vec_unpackh((vint8)_vec0);
|
| 180 |
+
vint16 vecshi1 = vec_unpackl((vint8)_vec0);
|
| 181 |
+
|
| 182 |
+
vint16 vecshi2 = vec_unpackh((vint8)_vec1);
|
| 183 |
+
vint16 vecshi3 = vec_unpackl((vint8)_vec1);
|
| 184 |
+
|
| 185 |
+
// signed -> unsigned
|
| 186 |
+
vecshi0 = vec_and(vecshi0, mask_unsigned);
|
| 187 |
+
vecshi1 = vec_and(vecshi1, mask_unsigned);
|
| 188 |
+
|
| 189 |
+
vecshi2 = vec_and(vecshi2, mask_unsigned);
|
| 190 |
+
vecshi3 = vec_and(vecshi3, mask_unsigned);
|
| 191 |
+
|
| 192 |
+
vint32 veci0 = vec_unpackh(vecshi0);
|
| 193 |
+
vint32 veci1 = vec_unpackl(vecshi0);
|
| 194 |
+
|
| 195 |
+
vint32 veci2 = vec_unpackh(vecshi1);
|
| 196 |
+
vint32 veci3 = vec_unpackl(vecshi1);
|
| 197 |
+
|
| 198 |
+
vint32 veci4 = vec_unpackh(vecshi2);
|
| 199 |
+
vint32 veci5 = vec_unpackl(vecshi2);
|
| 200 |
+
|
| 201 |
+
vint32 veci6 = vec_unpackh(vecshi3);
|
| 202 |
+
vint32 veci7 = vec_unpackl(vecshi3);
|
| 203 |
+
|
| 204 |
+
vfloat32 vecf0_0 = vec_float(veci0);
|
| 205 |
+
vfloat32 vecf1_0 = vec_float(veci1);
|
| 206 |
+
|
| 207 |
+
vfloat32 vecf0_1 = vec_float(veci2);
|
| 208 |
+
vfloat32 vecf1_1 = vec_float(veci3);
|
| 209 |
+
|
| 210 |
+
vfloat32 vecf0_2 = vec_float(veci4);
|
| 211 |
+
vfloat32 vecf1_2 = vec_float(veci5);
|
| 212 |
+
|
| 213 |
+
vfloat32 vecf0_3 = vec_float(veci6);
|
| 214 |
+
vfloat32 vecf1_3 = vec_float(veci7);
|
| 215 |
+
vfloat32 scale_vec0 = scale.vec0();
|
| 216 |
+
vfloat32 scale_vec1 = scale.vec1();
|
| 217 |
+
vfloat32 zero_point0 = zero_point.vec0();
|
| 218 |
+
vfloat32 zero_point1 = zero_point.vec1();
|
| 219 |
+
return {
|
| 220 |
+
Vectorized<float>{
|
| 221 |
+
(vecf0_0 - zero_point0) * scale_vec0,
|
| 222 |
+
(vecf1_0 - zero_point1) * scale_vec1},
|
| 223 |
+
Vectorized<float>{
|
| 224 |
+
(vecf0_1 - zero_point0) * scale_vec0,
|
| 225 |
+
(vecf1_1 - zero_point1) * scale_vec1},
|
| 226 |
+
Vectorized<float>{
|
| 227 |
+
(vecf0_2 - zero_point0) * scale_vec0,
|
| 228 |
+
(vecf1_2 - zero_point1) * scale_vec1},
|
| 229 |
+
Vectorized<float>{
|
| 230 |
+
(vecf0_3 - zero_point0) * scale_vec0,
|
| 231 |
+
(vecf1_3 - zero_point1) * scale_vec1}};
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
static Vectorized<c10::quint8> quantize(
|
| 235 |
+
const float_vec_return_type& rhs,
|
| 236 |
+
float scale,
|
| 237 |
+
int32_t zero_point,
|
| 238 |
+
float inverse_scale) {
|
| 239 |
+
// constexpr int32_t min_val = std::numeric_limits<value_type>::min();
|
| 240 |
+
// constexpr int32_t max_val = std::numeric_limits<value_type>::max();
|
| 241 |
+
|
| 242 |
+
vfloat32 vec_inverse = vec_splats(inverse_scale);
|
| 243 |
+
vfloat32 vec_zero_point = vec_splats((float)zero_point);
|
| 244 |
+
// vuint32 vmin = vec_splats(min_val);
|
| 245 |
+
// vuint32 vmax = vec_splats(max_val);
|
| 246 |
+
Vectorized<float> vf0 = rhs[0];
|
| 247 |
+
Vectorized<float> vf1 = rhs[1];
|
| 248 |
+
Vectorized<float> vf2 = rhs[2];
|
| 249 |
+
Vectorized<float> vf3 = rhs[3];
|
| 250 |
+
vfloat32 vecf0 = vf0.vec0();
|
| 251 |
+
vfloat32 vecf1 = vf0.vec1();
|
| 252 |
+
vfloat32 vecf2 = vf1.vec0();
|
| 253 |
+
vfloat32 vecf3 = vf1.vec1();
|
| 254 |
+
|
| 255 |
+
vfloat32 vecf4 = vf2.vec0();
|
| 256 |
+
vfloat32 vecf5 = vf2.vec1();
|
| 257 |
+
vfloat32 vecf6 = vf3.vec0();
|
| 258 |
+
vfloat32 vecf7 = vf3.vec1();
|
| 259 |
+
|
| 260 |
+
vecf0 = vec_mul(vecf0, vec_inverse);
|
| 261 |
+
vecf1 = vec_mul(vecf1, vec_inverse);
|
| 262 |
+
vecf2 = vec_mul(vecf2, vec_inverse);
|
| 263 |
+
vecf3 = vec_mul(vecf3, vec_inverse);
|
| 264 |
+
|
| 265 |
+
vecf4 = vec_mul(vecf4, vec_inverse);
|
| 266 |
+
vecf5 = vec_mul(vecf5, vec_inverse);
|
| 267 |
+
vecf6 = vec_mul(vecf6, vec_inverse);
|
| 268 |
+
vecf7 = vec_mul(vecf7, vec_inverse);
|
| 269 |
+
|
| 270 |
+
vecf0 = vec_add(vec_rint(vecf0), vec_zero_point);
|
| 271 |
+
vecf1 = vec_add(vec_rint(vecf1), vec_zero_point);
|
| 272 |
+
vecf2 = vec_add(vec_rint(vecf2), vec_zero_point);
|
| 273 |
+
vecf3 = vec_add(vec_rint(vecf3), vec_zero_point);
|
| 274 |
+
|
| 275 |
+
vecf4 = vec_add(vec_rint(vecf4), vec_zero_point);
|
| 276 |
+
vecf5 = vec_add(vec_rint(vecf5), vec_zero_point);
|
| 277 |
+
vecf6 = vec_add(vec_rint(vecf6), vec_zero_point);
|
| 278 |
+
vecf7 = vec_add(vec_rint(vecf7), vec_zero_point);
|
| 279 |
+
|
| 280 |
+
vint32 veci0 = vec_signed(vecf0);
|
| 281 |
+
vint32 veci1 = vec_signed(vecf1);
|
| 282 |
+
vint32 veci2 = vec_signed(vecf2);
|
| 283 |
+
vint32 veci3 = vec_signed(vecf3);
|
| 284 |
+
|
| 285 |
+
vint32 veci4 = vec_signed(vecf4);
|
| 286 |
+
vint32 veci5 = vec_signed(vecf5);
|
| 287 |
+
vint32 veci6 = vec_signed(vecf6);
|
| 288 |
+
vint32 veci7 = vec_signed(vecf7);
|
| 289 |
+
|
| 290 |
+
vint16 vecshi0 = vec_packs(veci0, veci1);
|
| 291 |
+
vint16 vecshi1 = vec_packs(veci2, veci3);
|
| 292 |
+
vint16 vecshi2 = vec_packs(veci4, veci5);
|
| 293 |
+
vint16 vecshi3 = vec_packs(veci6, veci7);
|
| 294 |
+
|
| 295 |
+
vuint8 vec0 = vec_packsu(vecshi0, vecshi1);
|
| 296 |
+
vuint8 vec1 = vec_packsu(vecshi2, vecshi3);
|
| 297 |
+
|
| 298 |
+
return {vec0, vec1};
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
Vectorized<c10::quint8> C10_ALWAYS_INLINE relu(Vectorized<c10::quint8> zero_point) const {
|
| 302 |
+
return {vec_max(_vec0, zero_point._vec0), vec_max(_vec1, zero_point._vec1)};
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
Vectorized<c10::quint8> C10_ALWAYS_INLINE
|
| 306 |
+
relu6(Vectorized<c10::quint8> zero_point, Vectorized<c10::quint8> q_six) const {
|
| 307 |
+
vuint8 max0 = vec_max(_vec0, zero_point._vec0);
|
| 308 |
+
vuint8 max1 = vec_max(_vec1, zero_point._vec1);
|
| 309 |
+
return {vec_min(max0, q_six._vec0), vec_min(max1, q_six._vec1)};
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
int_vec_return_type widening_subtract(Vectorized<c10::quint8> b) const {
|
| 313 |
+
vint16 vecshi0 = vec_unpackh((vint8)_vec0);
|
| 314 |
+
vint16 vecBshi0 = vec_unpackh((vint8)b._vec0);
|
| 315 |
+
vint16 vecshi1 = vec_unpackl((vint8)_vec0);
|
| 316 |
+
vint16 vecBshi1 = vec_unpackl((vint8)b._vec0);
|
| 317 |
+
|
| 318 |
+
vint16 vecshi2 = vec_unpackh((vint8)_vec1);
|
| 319 |
+
vint16 vecBshi2 = vec_unpackh((vint8)b._vec1);
|
| 320 |
+
vint16 vecshi3 = vec_unpackl((vint8)_vec1);
|
| 321 |
+
vint16 vecBshi3 = vec_unpackl((vint8)b._vec1);
|
| 322 |
+
|
| 323 |
+
vecshi0 = vec_and(vecshi0, mask_unsigned);
|
| 324 |
+
vecBshi0 = vec_and(vecBshi0, mask_unsigned);
|
| 325 |
+
vecshi1 = vec_and(vecshi1, mask_unsigned);
|
| 326 |
+
vecBshi1 = vec_and(vecBshi1, mask_unsigned);
|
| 327 |
+
|
| 328 |
+
vecshi2 = vec_and(vecshi2, mask_unsigned);
|
| 329 |
+
vecBshi2 = vec_and(vecBshi2, mask_unsigned);
|
| 330 |
+
vecshi3 = vec_and(vecshi3, mask_unsigned);
|
| 331 |
+
vecBshi3 = vec_and(vecBshi3, mask_unsigned);
|
| 332 |
+
|
| 333 |
+
vint32 veci0 = vec_unpackh(vecshi0);
|
| 334 |
+
vint32 vecBi0 = vec_unpackh(vecBshi0);
|
| 335 |
+
vint32 veci1 = vec_unpackl(vecshi0);
|
| 336 |
+
vint32 vecBi1 = vec_unpackl(vecBshi0);
|
| 337 |
+
|
| 338 |
+
vint32 veci2 = vec_unpackh(vecshi1);
|
| 339 |
+
vint32 vecBi2 = vec_unpackh(vecBshi1);
|
| 340 |
+
vint32 veci3 = vec_unpackl(vecshi1);
|
| 341 |
+
vint32 vecBi3 = vec_unpackl(vecBshi1);
|
| 342 |
+
|
| 343 |
+
vint32 veci4 = vec_unpackh(vecshi2);
|
| 344 |
+
vint32 vecBi4 = vec_unpackh(vecBshi2);
|
| 345 |
+
vint32 veci5 = vec_unpackl(vecshi2);
|
| 346 |
+
vint32 vecBi5 = vec_unpackl(vecBshi2);
|
| 347 |
+
|
| 348 |
+
vint32 veci6 = vec_unpackh(vecshi3);
|
| 349 |
+
vint32 vecBi6 = vec_unpackh(vecBshi3);
|
| 350 |
+
vint32 veci7 = vec_unpackl(vecshi3);
|
| 351 |
+
vint32 vecBi7 = vec_unpackl(vecBshi3);
|
| 352 |
+
|
| 353 |
+
return {
|
| 354 |
+
Vectorized<c10::qint32>(veci0 - vecBi0, veci1 - vecBi1),
|
| 355 |
+
Vectorized<c10::qint32>(veci2 - vecBi2, veci3 - vecBi3),
|
| 356 |
+
Vectorized<c10::qint32>(veci4 - vecBi4, veci5 - vecBi5),
|
| 357 |
+
Vectorized<c10::qint32>(veci6 - vecBi6, veci7 - vecBi7)};
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
static Vectorized<c10::quint8> requantize_from_int(
|
| 361 |
+
const int_vec_return_type& inp,
|
| 362 |
+
float multiplier,
|
| 363 |
+
int32_t zero_point) {
|
| 364 |
+
vfloat32 vec_multiplier = vec_splats(multiplier);
|
| 365 |
+
vint32 vec_zero_point = vec_splats(zero_point);
|
| 366 |
+
|
| 367 |
+
Vectorized<c10::qint32> vi0 = inp[0];
|
| 368 |
+
Vectorized<c10::qint32> vi1 = inp[1];
|
| 369 |
+
Vectorized<c10::qint32> vi2 = inp[2];
|
| 370 |
+
Vectorized<c10::qint32> vi3 = inp[3];
|
| 371 |
+
|
| 372 |
+
vfloat32 vecf0 = vec_float(vi0.vec0());
|
| 373 |
+
vfloat32 vecf1 = vec_float(vi0.vec1());
|
| 374 |
+
vfloat32 vecf2 = vec_float(vi1.vec0());
|
| 375 |
+
vfloat32 vecf3 = vec_float(vi1.vec1());
|
| 376 |
+
|
| 377 |
+
vfloat32 vecf4 = vec_float(vi2.vec0());
|
| 378 |
+
vfloat32 vecf5 = vec_float(vi2.vec1());
|
| 379 |
+
vfloat32 vecf6 = vec_float(vi3.vec0());
|
| 380 |
+
vfloat32 vecf7 = vec_float(vi3.vec1());
|
| 381 |
+
|
| 382 |
+
vecf0 = vec_mul(vecf0, vec_multiplier);
|
| 383 |
+
vecf1 = vec_mul(vecf1, vec_multiplier);
|
| 384 |
+
vecf2 = vec_mul(vecf2, vec_multiplier);
|
| 385 |
+
vecf3 = vec_mul(vecf3, vec_multiplier);
|
| 386 |
+
|
| 387 |
+
vecf4 = vec_mul(vecf4, vec_multiplier);
|
| 388 |
+
vecf5 = vec_mul(vecf5, vec_multiplier);
|
| 389 |
+
vecf6 = vec_mul(vecf6, vec_multiplier);
|
| 390 |
+
vecf7 = vec_mul(vecf7, vec_multiplier);
|
| 391 |
+
|
| 392 |
+
vecf0 = vec_rint(vecf0);
|
| 393 |
+
vecf1 = vec_rint(vecf1);
|
| 394 |
+
vecf2 = vec_rint(vecf2);
|
| 395 |
+
vecf3 = vec_rint(vecf3);
|
| 396 |
+
|
| 397 |
+
vecf4 = vec_rint(vecf4);
|
| 398 |
+
vecf5 = vec_rint(vecf5);
|
| 399 |
+
vecf6 = vec_rint(vecf6);
|
| 400 |
+
vecf7 = vec_rint(vecf7);
|
| 401 |
+
|
| 402 |
+
vint32 veci0 = vec_signed(vecf0);
|
| 403 |
+
vint32 veci1 = vec_signed(vecf1);
|
| 404 |
+
vint32 veci2 = vec_signed(vecf2);
|
| 405 |
+
vint32 veci3 = vec_signed(vecf3);
|
| 406 |
+
|
| 407 |
+
vint32 veci4 = vec_signed(vecf4);
|
| 408 |
+
vint32 veci5 = vec_signed(vecf5);
|
| 409 |
+
vint32 veci6 = vec_signed(vecf6);
|
| 410 |
+
vint32 veci7 = vec_signed(vecf7);
|
| 411 |
+
|
| 412 |
+
veci0 = vec_add(veci0, vec_zero_point);
|
| 413 |
+
veci1 = vec_add(veci1, vec_zero_point);
|
| 414 |
+
veci2 = vec_add(veci2, vec_zero_point);
|
| 415 |
+
veci3 = vec_add(veci3, vec_zero_point);
|
| 416 |
+
|
| 417 |
+
veci4 = vec_add(veci4, vec_zero_point);
|
| 418 |
+
veci5 = vec_add(veci5, vec_zero_point);
|
| 419 |
+
veci6 = vec_add(veci6, vec_zero_point);
|
| 420 |
+
veci7 = vec_add(veci7, vec_zero_point);
|
| 421 |
+
|
| 422 |
+
vint16 vecshi0 = vec_packs(veci0, veci1);
|
| 423 |
+
vint16 vecshi1 = vec_packs(veci2, veci3);
|
| 424 |
+
vint16 vecshi2 = vec_packs(veci4, veci5);
|
| 425 |
+
vint16 vecshi3 = vec_packs(veci6, veci7);
|
| 426 |
+
|
| 427 |
+
vuint8 vec0 = vec_packsu(vecshi0, vecshi1);
|
| 428 |
+
vuint8 vec1 = vec_packsu(vecshi2, vecshi3);
|
| 429 |
+
|
| 430 |
+
return {vec0, vec1};
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
DEFINE_MEMBER_OP(operator==, c10::quint8, vec_cmpeq)
|
| 434 |
+
DEFINE_MEMBER_OP(operator!=, c10::quint8, vec_cmpne)
|
| 435 |
+
DEFINE_MEMBER_OP(operator<, c10::quint8, vec_cmplt)
|
| 436 |
+
DEFINE_MEMBER_OP(operator<=, c10::quint8, vec_cmple)
|
| 437 |
+
DEFINE_MEMBER_OP(operator>, c10::quint8, vec_cmpgt)
|
| 438 |
+
DEFINE_MEMBER_OP(operator>=, c10::quint8, vec_cmpge)
|
| 439 |
+
DEFINE_MEMBER_OP(operator+, c10::quint8, vec_add)
|
| 440 |
+
DEFINE_MEMBER_OP(operator-, c10::quint8, vec_sub)
|
| 441 |
+
DEFINE_MEMBER_OP(operator*, c10::quint8, vec_mul)
|
| 442 |
+
DEFINE_MEMBER_EMULATE_BINARY_OP(operator/, c10::quint8, /)
|
| 443 |
+
DEFINE_MEMBER_OP(maximum, c10::quint8, vec_max)
|
| 444 |
+
DEFINE_MEMBER_OP(minimum, c10::quint8, vec_min)
|
| 445 |
+
DEFINE_MEMBER_OP(operator&, c10::quint8, vec_and)
|
| 446 |
+
DEFINE_MEMBER_OP(operator|, c10::quint8, vec_or)
|
| 447 |
+
DEFINE_MEMBER_OP(operator^, c10::quint8, vec_xor)
|
| 448 |
+
};
|
| 449 |
+
|
| 450 |
+
template <>
|
| 451 |
+
Vectorized<c10::quint8> inline maximum(
|
| 452 |
+
const Vectorized<c10::quint8>& a,
|
| 453 |
+
const Vectorized<c10::quint8>& b) {
|
| 454 |
+
return a.maximum(b);
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
template <>
|
| 458 |
+
Vectorized<c10::quint8> inline minimum(
|
| 459 |
+
const Vectorized<c10::quint8>& a,
|
| 460 |
+
const Vectorized<c10::quint8>& b) {
|
| 461 |
+
return a.minimum(b);
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
template <>
|
| 465 |
+
Vectorized<c10::quint8> C10_ALWAYS_INLINE operator+(const Vectorized<c10::quint8>& a, const Vectorized<c10::quint8>& b) {
|
| 466 |
+
return Vectorized<c10::quint8>{vec_add(a.vec0(), b.vec0()), vec_add(a.vec1(), b.vec1())};
|
| 467 |
+
}
|
| 468 |
+
|
| 469 |
+
template <>
|
| 470 |
+
Vectorized<c10::quint8> C10_ALWAYS_INLINE operator-(const Vectorized<c10::quint8>& a, const Vectorized<c10::quint8>& b) {
|
| 471 |
+
return Vectorized<c10::quint8>{vec_sub(a.vec0(), b.vec0()), vec_sub(a.vec1(), b.vec1())};
|
| 472 |
+
}
|
| 473 |
+
|
| 474 |
+
template <>
|
| 475 |
+
Vectorized<c10::quint8> C10_ALWAYS_INLINE operator*(const Vectorized<c10::quint8>& a, const Vectorized<c10::quint8>& b) {
|
| 476 |
+
return Vectorized<c10::quint8>{vec_mul(a.vec0(), b.vec0()), vec_mul(a.vec1(), b.vec1())};
|
| 477 |
+
}
|
| 478 |
+
|
| 479 |
+
template <>
|
| 480 |
+
Vectorized<c10::quint8> C10_ALWAYS_INLINE operator/(const Vectorized<c10::quint8>& a, const Vectorized<c10::quint8>& b) {
|
| 481 |
+
return Vectorized<c10::quint8>{a.vec0()/b.vec0(), a.vec1()/b.vec1()};
|
| 482 |
+
}
|
| 483 |
+
|
| 484 |
+
template <>
|
| 485 |
+
Vectorized<c10::quint8> C10_ALWAYS_INLINE operator&(const Vectorized<c10::quint8>& a, const Vectorized<c10::quint8>& b) {
|
| 486 |
+
return Vectorized<c10::quint8>{vec_and(a.vec0(), b.vec0()), vec_and(a.vec1(), b.vec1())};
|
| 487 |
+
}
|
| 488 |
+
|
| 489 |
+
template <>
|
| 490 |
+
Vectorized<c10::quint8> C10_ALWAYS_INLINE operator|(const Vectorized<c10::quint8>& a, const Vectorized<c10::quint8>& b) {
|
| 491 |
+
return Vectorized<c10::quint8>{vec_or(a.vec0(), b.vec0()), vec_or(a.vec1(), b.vec1())};
|
| 492 |
+
}
|
| 493 |
+
|
| 494 |
+
template <>
|
| 495 |
+
Vectorized<c10::quint8> C10_ALWAYS_INLINE operator^(const Vectorized<c10::quint8>& a, const Vectorized<c10::quint8>& b) {
|
| 496 |
+
return Vectorized<c10::quint8>{vec_xor(a.vec0(), b.vec0()), vec_xor(a.vec1(), b.vec1())};
|
| 497 |
+
}
|
| 498 |
+
|
| 499 |
+
} // namespace
|
| 500 |
+
} // namespace vec
|
| 501 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/vsx/vsx_helpers.h
ADDED
|
@@ -0,0 +1,474 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
#include <cstdint>
|
| 3 |
+
#include <c10/macros/Macros.h>
|
| 4 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 5 |
+
|
| 6 |
+
#if defined(__clang__)
|
| 7 |
+
typedef __vector __bool char vbool8;
|
| 8 |
+
typedef __vector __bool short vbool16;
|
| 9 |
+
typedef __vector __bool int vbool32;
|
| 10 |
+
typedef __vector __bool long long vbool64;
|
| 11 |
+
using vint8 = __attribute__((vector_size(16))) signed char;
|
| 12 |
+
using vint16 = __attribute__((vector_size(16))) signed short;
|
| 13 |
+
using vint32 = __attribute__((vector_size(16))) signed int;
|
| 14 |
+
using vint64 = __attribute__((vector_size(16))) signed long long;
|
| 15 |
+
using vuint8 = __attribute__((vector_size(16))) unsigned char;
|
| 16 |
+
using vuint16 = __attribute__((vector_size(16))) unsigned short;
|
| 17 |
+
using vuint32 = __attribute__((vector_size(16))) unsigned int;
|
| 18 |
+
using vuint64 = __attribute__((vector_size(16))) unsigned long long;
|
| 19 |
+
using vfloat32 = __attribute__((vector_size(16))) float;
|
| 20 |
+
using vfloat64 = __attribute__((vector_size(16))) double;
|
| 21 |
+
#else
|
| 22 |
+
using vbool8 = __attribute__((altivec(vector__))) __attribute__((altivec(bool__))) char;
|
| 23 |
+
using vbool16 = __attribute__((altivec(vector__))) __attribute__((altivec(bool__))) short;
|
| 24 |
+
using vbool32 = __attribute__((altivec(vector__))) __attribute__((altivec(bool__))) int;
|
| 25 |
+
using vbool64 = __attribute__((altivec(vector__))) __attribute__((altivec(bool__))) long long;
|
| 26 |
+
using vint8 = __attribute__((altivec(vector__))) signed char;
|
| 27 |
+
using vint16 = __attribute__((altivec(vector__))) signed short;
|
| 28 |
+
using vint32 = __attribute__((altivec(vector__))) signed int;
|
| 29 |
+
using vint64 = __attribute__((altivec(vector__))) signed long long;
|
| 30 |
+
using vuint8 = __attribute__((altivec(vector__))) unsigned char;
|
| 31 |
+
using vuint16 = __attribute__((altivec(vector__))) unsigned short;
|
| 32 |
+
using vuint32 = __attribute__((altivec(vector__))) unsigned int;
|
| 33 |
+
using vuint64 = __attribute__((altivec(vector__))) unsigned long long;
|
| 34 |
+
using vfloat32 = __attribute__((altivec(vector__))) float;
|
| 35 |
+
using vfloat64 = __attribute__((altivec(vector__))) double;
|
| 36 |
+
#endif
|
| 37 |
+
|
| 38 |
+
#if !defined(vec_float)
|
| 39 |
+
C10_ALWAYS_INLINE vfloat32 vec_float(const vint32& vec_in) {
|
| 40 |
+
vfloat32 vec_out;
|
| 41 |
+
__asm__("xvcvsxwsp %x0,%x1" : "=wf"(vec_out) : "wa"(vec_in));
|
| 42 |
+
return vec_out;
|
| 43 |
+
}
|
| 44 |
+
#endif
|
| 45 |
+
|
| 46 |
+
#if !defined(vec_signed)
|
| 47 |
+
C10_ALWAYS_INLINE vint32 vec_signed(const vfloat32& vec_in) {
|
| 48 |
+
vint32 vec_out;
|
| 49 |
+
__asm__("xvcvspsxws %x0,%x1" : "=wa"(vec_out) : "wf"(vec_in));
|
| 50 |
+
return vec_out;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
C10_ALWAYS_INLINE vint64 vec_signed(const vfloat64& vec_in) {
|
| 54 |
+
vint64 vec_out;
|
| 55 |
+
__asm__("xvcvdpsxds %x0,%x1" : "=wa"(vec_out) : "wd"(vec_in));
|
| 56 |
+
return vec_out;
|
| 57 |
+
}
|
| 58 |
+
#endif
|
| 59 |
+
|
| 60 |
+
#if !defined(vec_neg)
|
| 61 |
+
C10_ALWAYS_INLINE vfloat32 vec_neg(const vfloat32& vec_in) {
|
| 62 |
+
vfloat32 vec_out;
|
| 63 |
+
__asm__("xvnegsp %x0,%x1" : "=wf"(vec_out) : "wf"(vec_in));
|
| 64 |
+
return vec_out;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
C10_ALWAYS_INLINE vfloat64 vec_neg(const vfloat64& vec_in) {
|
| 68 |
+
vfloat64 vec_out;
|
| 69 |
+
__asm__("xvnegdp %x0,%x1" : "=wd"(vec_out) : "wd"(vec_in));
|
| 70 |
+
return vec_out;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
C10_ALWAYS_INLINE vint16 vec_neg(const vint16& vec_in) {
|
| 74 |
+
vint16 vint0 = {0, 0, 0, 0 ,0, 0, 0, 0};
|
| 75 |
+
return vec_vsubuhm(vint0, vec_in);
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
C10_ALWAYS_INLINE vint32 vec_neg(const vint32& vec_in) {
|
| 79 |
+
vint32 vint0 = {0, 0, 0, 0};
|
| 80 |
+
return vec_vsubuwm(vint0, vec_in);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
C10_ALWAYS_INLINE vint64 vec_neg(const vint64& vec_in) {
|
| 84 |
+
return -vec_in;
|
| 85 |
+
}
|
| 86 |
+
#endif
|
| 87 |
+
|
| 88 |
+
#if !defined(vec_sldw)
|
| 89 |
+
template <unsigned int C>
|
| 90 |
+
C10_ALWAYS_INLINE vfloat32
|
| 91 |
+
vec_sldw_aux(const vfloat32& vec_in0, const vfloat32& vec_in1) {
|
| 92 |
+
vfloat32 vec_out;
|
| 93 |
+
__asm("xxsldwi %x0, %x1, %x2, %3 "
|
| 94 |
+
: "=wa"(vec_out)
|
| 95 |
+
: "wa"(vec_in0), "wa"(vec_in1), "I"(C));
|
| 96 |
+
return vec_out;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
#define vec_sldw(a, b, c) vec_sldw_aux<c>(a, b)
|
| 100 |
+
#endif
|
| 101 |
+
|
| 102 |
+
#define vec_not(a) vec_nor(a, a)
|
| 103 |
+
#if defined(__clang__) && !defined(vec_splats)
|
| 104 |
+
C10_ALWAYS_INLINE vint64 vec_splats(const int64_t& a) {
|
| 105 |
+
return vec_splats(a);
|
| 106 |
+
}
|
| 107 |
+
#endif
|
| 108 |
+
// Vectorized min/max which return a if any operand is nan
|
| 109 |
+
template <class T>
|
| 110 |
+
C10_ALWAYS_INLINE T vec_min_nan(const T& a, const T& b) {
|
| 111 |
+
return vec_min(a, b);
|
| 112 |
+
}
|
| 113 |
+
template <class T>
|
| 114 |
+
C10_ALWAYS_INLINE T vec_max_nan(const T& a, const T& b) {
|
| 115 |
+
return vec_max(a, b);
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
// Specializations for float/double taken from Eigen
|
| 119 |
+
template<>
|
| 120 |
+
C10_ALWAYS_INLINE vfloat32 vec_min_nan<vfloat32>(const vfloat32& a, const vfloat32& b)
|
| 121 |
+
{
|
| 122 |
+
// NOTE: about 10% slower than vec_min, but consistent with std::min and SSE regarding NaN
|
| 123 |
+
vfloat32 ret;
|
| 124 |
+
__asm__ ("xvcmpgesp %x0,%x1,%x2\n\txxsel %x0,%x1,%x2,%x0" : "=&wa" (ret) : "wa" (a), "wa" (b));
|
| 125 |
+
return ret;
|
| 126 |
+
}
|
| 127 |
+
// Specializations for float/double taken from Eigen
|
| 128 |
+
template<>
|
| 129 |
+
C10_ALWAYS_INLINE vfloat32 vec_max_nan<vfloat32>(const vfloat32& a, const vfloat32& b)
|
| 130 |
+
{
|
| 131 |
+
// NOTE: about 10% slower than vec_max, but consistent with std::min and SSE regarding NaN
|
| 132 |
+
vfloat32 ret;
|
| 133 |
+
__asm__ ("xvcmpgtsp %x0,%x2,%x1\n\txxsel %x0,%x1,%x2,%x0" : "=&wa" (ret) : "wa" (a), "wa" (b));
|
| 134 |
+
return ret;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
template<>
|
| 138 |
+
C10_ALWAYS_INLINE vfloat64 vec_min_nan<vfloat64>(const vfloat64& a, const vfloat64& b)
|
| 139 |
+
{
|
| 140 |
+
// NOTE: about 10% slower than vec_min, but consistent with std::min and SSE regarding NaN
|
| 141 |
+
vfloat64 ret;
|
| 142 |
+
__asm__ ("xvcmpgedp %x0,%x1,%x2\n\txxsel %x0,%x1,%x2,%x0" : "=&wa" (ret) : "wa" (a), "wa" (b));
|
| 143 |
+
return ret;
|
| 144 |
+
}
|
| 145 |
+
template<>
|
| 146 |
+
C10_ALWAYS_INLINE vfloat64 vec_max_nan<vfloat64>(const vfloat64& a, const vfloat64& b)
|
| 147 |
+
{
|
| 148 |
+
// NOTE: about 10% slower than vec_max, but consistent with std::max and SSE regarding NaN
|
| 149 |
+
vfloat64 ret;
|
| 150 |
+
__asm__ ("xvcmpgtdp %x0,%x2,%x1\n\txxsel %x0,%x1,%x2,%x0" : "=&wa" (ret) : "wa" (a), "wa" (b));
|
| 151 |
+
return ret;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
// Vectorizes min/max function which returns nan if any side is nan
|
| 155 |
+
#define C10_VSX_VEC_NAN_PROPAG(name, type, btype, func) \
|
| 156 |
+
C10_ALWAYS_INLINE type name(const type& a, const type& b) { \
|
| 157 |
+
type tmp = func(a, b); \
|
| 158 |
+
btype nan_a = vec_cmpne(a, a); \
|
| 159 |
+
btype nan_b = vec_cmpne(b, b); \
|
| 160 |
+
tmp = vec_sel(tmp, a, nan_a); \
|
| 161 |
+
return vec_sel(tmp, b, nan_b); \
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
C10_VSX_VEC_NAN_PROPAG(vec_min_nan2, vfloat32, vbool32, vec_min)
|
| 165 |
+
C10_VSX_VEC_NAN_PROPAG(vec_max_nan2, vfloat32, vbool32, vec_max)
|
| 166 |
+
C10_VSX_VEC_NAN_PROPAG(vec_min_nan2, vfloat64, vbool64, vec_min)
|
| 167 |
+
C10_VSX_VEC_NAN_PROPAG(vec_max_nan2, vfloat64, vbool64, vec_max)
|
| 168 |
+
|
| 169 |
+
#undef C10_VSX_VEC_NAN_PROPAG
|
| 170 |
+
|
| 171 |
+
#define DEFINE_MEMBER_UNARY_OP(op, op_type, func) \
|
| 172 |
+
Vectorized<op_type> C10_ALWAYS_INLINE op() const { \
|
| 173 |
+
return Vectorized<op_type>{func(_vec0), func(_vec1)}; \
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
#define DEFINE_MEMBER_OP(op, op_type, func) \
|
| 177 |
+
Vectorized<op_type> C10_ALWAYS_INLINE op(const Vectorized<op_type>& other) const { \
|
| 178 |
+
return Vectorized<op_type>{ \
|
| 179 |
+
func(_vec0, other._vec0), func(_vec1, other._vec1)}; \
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
#define DEFINE_MEMBER_BITWISE_OP(op, op_type, func) \
|
| 183 |
+
Vectorized<op_type> C10_ALWAYS_INLINE op(const Vectorized<op_type>& other) const { \
|
| 184 |
+
return Vectorized<op_type>{ \
|
| 185 |
+
func(_vecb0, other._vecb0), func(_vecb1, other._vecb1)}; \
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
#define DEFINE_MEMBER_TERNARY_OP(op, op_type, func) \
|
| 189 |
+
Vectorized<op_type> C10_ALWAYS_INLINE op( \
|
| 190 |
+
const Vectorized<op_type>& b, const Vectorized<op_type>& c) const { \
|
| 191 |
+
return Vectorized<op_type>{ \
|
| 192 |
+
func(_vec0, b._vec0, c._vec0), func(_vec1, b._vec1, c._vec1)}; \
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
#define DEFINE_MEMBER_EMULATE_BINARY_OP(op, op_type, binary_op) \
|
| 196 |
+
Vectorized<op_type> C10_ALWAYS_INLINE op(const Vectorized<op_type>& b) const { \
|
| 197 |
+
Vectorized<op_type>::vec_internal_type ret_0; \
|
| 198 |
+
Vectorized<op_type>::vec_internal_type ret_1; \
|
| 199 |
+
for (int i = 0; i < Vectorized<op_type>::size() / 2; i++) { \
|
| 200 |
+
ret_0[i] = _vec0[i] binary_op b._vec0[i]; \
|
| 201 |
+
ret_1[i] = _vec1[i] binary_op b._vec1[i]; \
|
| 202 |
+
} \
|
| 203 |
+
return Vectorized<op_type>{ret_0, ret_1}; \
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
#define DEFINE_MEMBER_OP_AND_ONE(op, op_type, func) \
|
| 208 |
+
Vectorized<op_type> C10_ALWAYS_INLINE op(const Vectorized<op_type>& other) const { \
|
| 209 |
+
using vvtype = Vectorized<op_type>::vec_internal_type; \
|
| 210 |
+
const vvtype v_one = vec_splats(static_cast<op_type>(1.0)); \
|
| 211 |
+
vvtype ret0 = (vvtype)func(_vec0, other._vec0); \
|
| 212 |
+
vvtype ret1 = (vvtype)func(_vec1, other._vec1); \
|
| 213 |
+
return Vectorized<op_type>{vec_and(ret0, v_one), vec_and(ret1, v_one)}; \
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
#define DEFINE_CLAMP_FUNCS(operand_type) \
|
| 217 |
+
template <> \
|
| 218 |
+
Vectorized<operand_type> C10_ALWAYS_INLINE clamp( \
|
| 219 |
+
const Vectorized<operand_type>& a, \
|
| 220 |
+
const Vectorized<operand_type>& min, \
|
| 221 |
+
const Vectorized<operand_type>& max) { \
|
| 222 |
+
return Vectorized<operand_type>{ \
|
| 223 |
+
vec_min_nan(vec_max_nan(a.vec0(), min.vec0()), max.vec0()), \
|
| 224 |
+
vec_min_nan(vec_max_nan(a.vec1(), min.vec1()), max.vec1())}; \
|
| 225 |
+
} \
|
| 226 |
+
template <> \
|
| 227 |
+
Vectorized<operand_type> C10_ALWAYS_INLINE clamp_min( \
|
| 228 |
+
const Vectorized<operand_type>& a, const Vectorized<operand_type>& min) { \
|
| 229 |
+
return Vectorized<operand_type>{ \
|
| 230 |
+
vec_max_nan(a.vec0(), min.vec0()), \
|
| 231 |
+
vec_max_nan(a.vec1(), min.vec1())}; \
|
| 232 |
+
} \
|
| 233 |
+
template <> \
|
| 234 |
+
Vectorized<operand_type> C10_ALWAYS_INLINE clamp_max( \
|
| 235 |
+
const Vectorized<operand_type>& a, const Vectorized<operand_type>& max) { \
|
| 236 |
+
return Vectorized<operand_type>{ \
|
| 237 |
+
vec_min_nan(a.vec0(), max.vec0()), \
|
| 238 |
+
vec_min_nan(a.vec1(), max.vec1())}; \
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
#define DEFINE_REINTERPRET_CAST_FUNCS( \
|
| 242 |
+
first_type, cast_type, cast_inner_vector_type) \
|
| 243 |
+
template <> \
|
| 244 |
+
C10_ALWAYS_INLINE Vectorized<cast_type> cast<cast_type, first_type>( \
|
| 245 |
+
const Vectorized<first_type>& src) { \
|
| 246 |
+
return Vectorized<cast_type>{(cast_inner_vector_type)src.vec0(), \
|
| 247 |
+
(cast_inner_vector_type)src.vec1()}; \
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
#define DEFINE_REINTERPRET_CAST_TO_ALL_FUNCS(first_type) \
|
| 251 |
+
DEFINE_REINTERPRET_CAST_FUNCS(first_type, double, vfloat64) \
|
| 252 |
+
DEFINE_REINTERPRET_CAST_FUNCS(first_type, float, vfloat32) \
|
| 253 |
+
DEFINE_REINTERPRET_CAST_FUNCS(first_type, int64_t, vint64) \
|
| 254 |
+
DEFINE_REINTERPRET_CAST_FUNCS(first_type, int32_t, vint32) \
|
| 255 |
+
DEFINE_REINTERPRET_CAST_FUNCS(first_type, int16_t, vint16)
|
| 256 |
+
|
| 257 |
+
// it can be used to emulate blend faster
|
| 258 |
+
constexpr int blendChoice(uint32_t mask, uint32_t half1 = 0xF, uint32_t half2 = 0xF0) {
|
| 259 |
+
uint32_t none = 0;
|
| 260 |
+
uint32_t both = half1 | half2;
|
| 261 |
+
// clamp it between 0 and both
|
| 262 |
+
mask = mask & both;
|
| 263 |
+
// return (a._vec0, a._vec1)
|
| 264 |
+
if (mask == none) return 0;
|
| 265 |
+
// return (b._vec0,b._vec1)
|
| 266 |
+
else if (mask == both)
|
| 267 |
+
return 1;
|
| 268 |
+
// return (b._vec0,a._vec1)
|
| 269 |
+
else if (mask == half1)
|
| 270 |
+
return 2;
|
| 271 |
+
// return (a._vec0,b._vec1)
|
| 272 |
+
else if (mask == half2)
|
| 273 |
+
return 3;
|
| 274 |
+
// return (*_vec0,a._vec1)
|
| 275 |
+
else if (mask > 0 && mask < half1)
|
| 276 |
+
return 4;
|
| 277 |
+
// return (*_vec0,b._vec1)
|
| 278 |
+
else if ((mask & half2) == half2)
|
| 279 |
+
return 5;
|
| 280 |
+
// return (a._vec0,*_vec1)
|
| 281 |
+
else if ((mask & half1) == 0 && mask > half1)
|
| 282 |
+
return 6;
|
| 283 |
+
// return (b._vec0,*_vec1)
|
| 284 |
+
else if ((mask & half1) == half1 && mask > half1)
|
| 285 |
+
return 7;
|
| 286 |
+
// return (*_vec0,*_vec1)
|
| 287 |
+
return 8;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
// it can be used to emulate blend faster
|
| 291 |
+
constexpr int blendChoiceDbl(uint32_t mask) {
|
| 292 |
+
// clamp it 0 and 0xF
|
| 293 |
+
return blendChoice(mask, 0x3, 0xC);
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
constexpr vbool32 VsxMask1(uint32_t mask) {
|
| 297 |
+
uint32_t g0 = (mask & 1) * 0xffffffff;
|
| 298 |
+
uint32_t g1 = ((mask & 2) >> 1) * 0xffffffff;
|
| 299 |
+
uint32_t g2 = ((mask & 4) >> 2) * 0xffffffff;
|
| 300 |
+
uint32_t g3 = ((mask & 8) >> 3) * 0xffffffff;
|
| 301 |
+
return (vbool32){g0, g1, g2, g3};
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
constexpr vbool32 VsxMask2(uint32_t mask) {
|
| 305 |
+
uint32_t mask2 = (mask & 0xFF) >> 4;
|
| 306 |
+
return VsxMask1(mask2);
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
constexpr vbool64 VsxDblMask1(uint32_t mask) {
|
| 310 |
+
uint64_t g0 = (mask & 1) * 0xffffffffffffffff;
|
| 311 |
+
uint64_t g1 = ((mask & 2) >> 1) * 0xffffffffffffffff;
|
| 312 |
+
return (vbool64){g0, g1};
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
constexpr vbool64 VsxDblMask2(uint32_t mask) {
|
| 316 |
+
uint32_t mask2 = (mask & 0xF) >> 2;
|
| 317 |
+
return VsxDblMask1(mask2);
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
constexpr int maskForComplex(uint32_t mask) {
|
| 321 |
+
mask = mask & 0xF;
|
| 322 |
+
int complex_mask = 0;
|
| 323 |
+
if (mask & 1) complex_mask |= 3;
|
| 324 |
+
if (mask & 2) complex_mask |= (3 << 2);
|
| 325 |
+
if (mask & 4) complex_mask |= (3 << 4);
|
| 326 |
+
if (mask & 8) complex_mask |= (3 << 6);
|
| 327 |
+
return complex_mask;
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
constexpr int maskForComplexDbl(uint32_t mask) {
|
| 331 |
+
mask = mask & 0x3;
|
| 332 |
+
int complex_mask = 0;
|
| 333 |
+
if (mask & 1) complex_mask |= 3;
|
| 334 |
+
if (mask & 2) complex_mask |= (3 << 2);
|
| 335 |
+
return complex_mask;
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
constexpr int blendChoiceComplex(uint32_t mask) {
|
| 339 |
+
return blendChoice(maskForComplex(mask));
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
constexpr int blendChoiceComplexDbl(uint32_t mask) {
|
| 343 |
+
return blendChoiceDbl(maskForComplexDbl(mask));
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
constexpr vbool32 VsxComplexMask1(uint32_t mask) {
|
| 347 |
+
return VsxMask1(maskForComplex(mask));
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
constexpr vbool32 VsxComplexMask2(uint32_t mask) {
|
| 351 |
+
uint32_t mask2 = (mask & 0xF) >> 2;
|
| 352 |
+
return VsxMask1(maskForComplex(mask2));
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
constexpr vbool64 VsxComplexDblMask1(uint32_t mask) { return VsxDblMask1(mask); }
|
| 356 |
+
|
| 357 |
+
constexpr vbool64 VsxComplexDblMask2(uint32_t mask) {
|
| 358 |
+
uint32_t mask2 = (mask & 0xF) >> 2;
|
| 359 |
+
return VsxDblMask1(mask2);
|
| 360 |
+
}
|
| 361 |
+
|
| 362 |
+
// constants
|
| 363 |
+
namespace at {
|
| 364 |
+
namespace vec {
|
| 365 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 366 |
+
inline namespace CPU_CAPABILITY {
|
| 367 |
+
//
|
| 368 |
+
constexpr int offset0 = 0;
|
| 369 |
+
constexpr int offset16 = 16;
|
| 370 |
+
|
| 371 |
+
// #Constants
|
| 372 |
+
const vuint8 mask_zero_bits = vuint8{128, 128, 128, 128, 128, 128, 128, 128,
|
| 373 |
+
128, 128, 128, 128, 96, 64, 32, 0};
|
| 374 |
+
|
| 375 |
+
const vuint8 swap_mask =
|
| 376 |
+
vuint8{4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11};
|
| 377 |
+
|
| 378 |
+
const vint32 v0x7f = vec_splats(0x7f);
|
| 379 |
+
const vint32 vi_0 = vec_splats((int)(0));
|
| 380 |
+
const vint32 vi_1 = vec_splats((int)1);
|
| 381 |
+
const vint32 vi_2 = vec_splats((int)2);
|
| 382 |
+
const vint32 vi_4 = vec_splats((int)4);
|
| 383 |
+
const vint32 vi_inv1 = vec_splats((int)~1);
|
| 384 |
+
const vuint32 vu_29 = vec_splats(29u);
|
| 385 |
+
const vuint32 vu_23 = vec_splats(23u);
|
| 386 |
+
|
| 387 |
+
const vbool32 inv_mant_mask = (vbool32)vec_splats((unsigned int)~0xff800000);
|
| 388 |
+
const vbool32 sign_mask = (vbool32)vec_splats((int)0x80000000);
|
| 389 |
+
const vbool32 real_mask = vbool32{0xFFFFFFFF, 0x0, 0xFFFFFFFF, 0x0};
|
| 390 |
+
const vbool32 imag_mask = vbool32{0x0, 0xFFFFFFFF, 0x0, 0xFFFFFFFF};
|
| 391 |
+
const vbool32 isign_mask = vbool32{0x0, 0x80000000, 0x0, 0x80000000};
|
| 392 |
+
const vbool32 rsign_mask = vbool32{0x80000000, 0x0, 0x80000000, 0x0};
|
| 393 |
+
|
| 394 |
+
const vbool64 vd_sign_mask = vbool64{0x8000000000000000, 0x8000000000000000};
|
| 395 |
+
const vbool64 vd_imag_mask = vbool64{0x0, 0xFFFFFFFFFFFFFFFF};
|
| 396 |
+
const vbool64 vd_real_mask = vbool64{0xFFFFFFFFFFFFFFFF, 0x0};
|
| 397 |
+
const vbool64 vd_isign_mask = vbool64{0x0, 0x8000000000000000};
|
| 398 |
+
const vbool64 vd_rsign_mask = vbool64{0x8000000000000000, 0x0};
|
| 399 |
+
|
| 400 |
+
const vfloat32 zero = vec_splats(0.f);
|
| 401 |
+
const vfloat32 half = vec_splats(0.5f);
|
| 402 |
+
const vfloat32 one = vec_splats(1.f);
|
| 403 |
+
const vfloat32 two = vec_splats(2.0f);
|
| 404 |
+
const vfloat32 _4div_pi = vec_splats(1.27323954473516f);
|
| 405 |
+
const vfloat32 v_inf = (vfloat32)vec_splats(0x7f800000u);
|
| 406 |
+
const vfloat32 v_minus_inf = vfloat32{ 0xff800000u, 0xff800000u, 0xff800000u, 0xff800000u };
|
| 407 |
+
const vfloat32 v_nan = (vfloat32)vec_splats(0x7fffffff);
|
| 408 |
+
const vfloat32 log10e_inv = vec_splats(0.43429448190325176f);
|
| 409 |
+
const vfloat32 log2e_inv = vec_splats(1.4426950408889634f);
|
| 410 |
+
const vfloat32 log2eB_inv = vec_splats(1.442695036924675f);
|
| 411 |
+
const vfloat32 cephes_SQRTHF = vec_splats(0.707106781186547524f);
|
| 412 |
+
const vfloat32 coscof_p0 = vec_splats(2.443315711809948E-005f);
|
| 413 |
+
const vfloat32 coscof_p1 = vec_splats(-1.388731625493765E-003f);
|
| 414 |
+
const vfloat32 coscof_p2 = vec_splats(4.166664568298827E-002f);
|
| 415 |
+
const vfloat32 exp_hi = vec_splats(104.f);
|
| 416 |
+
const vfloat32 exp_lo = vec_splats(-104.f);
|
| 417 |
+
const vfloat32 exp_p0 = vec_splats(0.000198527617612853646278381f);
|
| 418 |
+
const vfloat32 exp_p1 = vec_splats((0.00139304355252534151077271f));
|
| 419 |
+
const vfloat32 exp_p2 = vec_splats(0.00833336077630519866943359f);
|
| 420 |
+
const vfloat32 exp_p3 = vec_splats(0.0416664853692054748535156f);
|
| 421 |
+
const vfloat32 exp_p4 = vec_splats(0.166666671633720397949219f);
|
| 422 |
+
const vfloat32 exp_p5 = vec_splats(0.5f);
|
| 423 |
+
const vfloat32 log_p0 = vec_splats(7.0376836292E-2f);
|
| 424 |
+
const vfloat32 log_p1 = vec_splats(-1.1514610310E-1f);
|
| 425 |
+
const vfloat32 log_p2 = vec_splats(1.1676998740E-1f);
|
| 426 |
+
const vfloat32 log_p3 = vec_splats(-1.2420140846E-1f);
|
| 427 |
+
const vfloat32 log_p4 = vec_splats(+1.4249322787E-1f);
|
| 428 |
+
const vfloat32 log_p5 = vec_splats(-1.6668057665E-1f);
|
| 429 |
+
const vfloat32 log_p6 = vec_splats(+2.0000714765E-1f);
|
| 430 |
+
const vfloat32 log_p7 = vec_splats(-2.4999993993E-1f);
|
| 431 |
+
const vfloat32 log_p8 = vec_splats(+3.3333331174E-1f);
|
| 432 |
+
const vfloat32 log_q1 = vec_splats(-2.12194440e-4f);
|
| 433 |
+
const vfloat32 log_q2 = vec_splats(0.693359375f);
|
| 434 |
+
const vfloat32 max_logf = vec_splats(88.02969187150841f);
|
| 435 |
+
const vfloat32 max_numf = vec_splats(1.7014117331926442990585209174225846272e38f);
|
| 436 |
+
const vfloat32 min_inf = (vfloat32)vec_splats(0xff800000u);
|
| 437 |
+
const vfloat32 min_norm_pos = (vfloat32)vec_splats(0x0800000u);
|
| 438 |
+
const vfloat32 minus_cephes_dp1 = vec_splats(-0.78515625f);
|
| 439 |
+
const vfloat32 minus_cephes_dp2 = vec_splats(-2.4187564849853515625e-4f);
|
| 440 |
+
const vfloat32 minus_cephes_dp3 = vec_splats(-3.77489497744594108e-8f);
|
| 441 |
+
const vfloat32 negln2f_hi = vec_splats(-0.693145751953125f);
|
| 442 |
+
const vfloat32 negln2f_lo = vec_splats(-1.428606765330187045e-06f);
|
| 443 |
+
const vfloat32 p0 = vec_splats(2.03721912945E-4f);
|
| 444 |
+
const vfloat32 p1 = vec_splats(8.33028376239E-3f);
|
| 445 |
+
const vfloat32 p2 = vec_splats(1.66667160211E-1f);
|
| 446 |
+
const vfloat32 sincof_p0 = vec_splats(-1.9515295891E-4f);
|
| 447 |
+
const vfloat32 sincof_p1 = vec_splats(8.3321608736E-3f);
|
| 448 |
+
const vfloat32 sincof_p2 = vec_splats(-1.6666654611E-1f);
|
| 449 |
+
const vfloat32 tanh_0p625 = vec_splats(0.625f);
|
| 450 |
+
const vfloat32 tanh_half_max = vec_splats(44.014845935754205f);
|
| 451 |
+
const vfloat32 tanh_p0 = vec_splats(-5.70498872745E-3f);
|
| 452 |
+
const vfloat32 tanh_p1 = vec_splats(2.06390887954E-2f);
|
| 453 |
+
const vfloat32 tanh_p2 = vec_splats(-5.37397155531E-2f);
|
| 454 |
+
const vfloat32 tanh_p3 = vec_splats(1.33314422036E-1f);
|
| 455 |
+
const vfloat32 tanh_p4 = vec_splats(-3.33332819422E-1f);
|
| 456 |
+
const vfloat32 vcheck = vec_splats((float)(1LL << 24));
|
| 457 |
+
const vfloat32 imag_one = vfloat32{0.f, 1.f, 0.f, 1.f};
|
| 458 |
+
const vfloat32 imag_half = vfloat32{0.f, 0.5f, 0.f, 0.5f};
|
| 459 |
+
const vfloat32 sqrt2_2 = vfloat32{0.70710676908493042f, 0.70710676908493042,
|
| 460 |
+
0.70710676908493042, 0.70710676908493042};
|
| 461 |
+
const vfloat32 pi_2 = vfloat32{M_PI / 2, 0.0, M_PI / 2, 0.0};
|
| 462 |
+
const vfloat32 vf_89 = vfloat32{89.f, 89.f, 89.f, 89.f};
|
| 463 |
+
const vfloat64 vd_one = vec_splats(1.0);
|
| 464 |
+
const vfloat64 vd_zero = vec_splats(0.0);
|
| 465 |
+
const vfloat64 vd_log10e_inv = vec_splats(0.43429448190325176);
|
| 466 |
+
const vfloat64 vd_log2e_inv = vec_splats(1.4426950408889634);
|
| 467 |
+
const vfloat64 vd_imag_one = vfloat64{0.0, 1.0};
|
| 468 |
+
const vfloat64 vd_imag_half = vfloat64{0.0, 0.5};
|
| 469 |
+
const vfloat64 vd_sqrt2_2 = vfloat64{0.70710678118654757, 0.70710678118654757};
|
| 470 |
+
const vfloat64 vd_pi_2 = vfloat64{M_PI / 2.0, 0.0};
|
| 471 |
+
|
| 472 |
+
} // namespace
|
| 473 |
+
} // namespace vec
|
| 474 |
+
} // namespace at
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec256/zarch/vec256_zarch.h
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec512/vec512_complex_double.h
ADDED
|
@@ -0,0 +1,513 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// DO NOT DEFINE STATIC DATA IN THIS HEADER!
|
| 4 |
+
// See Note [Do not compile initializers with AVX]
|
| 5 |
+
|
| 6 |
+
#include <c10/util/complex.h>
|
| 7 |
+
#include <c10/util/irange.h>
|
| 8 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 9 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 10 |
+
#if defined(CPU_CAPABILITY_AVX512)
|
| 11 |
+
#define SLEEF_STATIC_LIBS
|
| 12 |
+
#include <sleef.h>
|
| 13 |
+
#endif
|
| 14 |
+
|
| 15 |
+
namespace at {
|
| 16 |
+
namespace vec {
|
| 17 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 18 |
+
inline namespace CPU_CAPABILITY {
|
| 19 |
+
|
| 20 |
+
#if defined(CPU_CAPABILITY_AVX512)
|
| 21 |
+
|
| 22 |
+
template <> class Vectorized<c10::complex<double>> {
|
| 23 |
+
private:
|
| 24 |
+
__m512d values;
|
| 25 |
+
static constexpr __m512i zero_vector {0, 0, 0, 0, 0, 0, 0, 0};
|
| 26 |
+
public:
|
| 27 |
+
using value_type = c10::complex<double>;
|
| 28 |
+
using size_type = int;
|
| 29 |
+
static constexpr size_type size() {
|
| 30 |
+
return 4;
|
| 31 |
+
}
|
| 32 |
+
Vectorized() {}
|
| 33 |
+
Vectorized(__m512d v) : values(v) {}
|
| 34 |
+
Vectorized(c10::complex<double> val) {
|
| 35 |
+
double real_value = val.real();
|
| 36 |
+
double imag_value = val.imag();
|
| 37 |
+
values = _mm512_setr_pd(real_value, imag_value, real_value, imag_value,
|
| 38 |
+
real_value, imag_value, real_value, imag_value);
|
| 39 |
+
}
|
| 40 |
+
Vectorized(c10::complex<double> val1, c10::complex<double> val2,
|
| 41 |
+
c10::complex<double> val3, c10::complex<double> val4) {
|
| 42 |
+
values = _mm512_setr_pd(val1.real(), val1.imag(),
|
| 43 |
+
val2.real(), val2.imag(),
|
| 44 |
+
val3.real(), val3.imag(),
|
| 45 |
+
val4.real(), val4.imag());
|
| 46 |
+
}
|
| 47 |
+
operator __m512d() const {
|
| 48 |
+
return values;
|
| 49 |
+
}
|
| 50 |
+
template <int64_t mask>
|
| 51 |
+
static Vectorized<c10::complex<double>> blend(const Vectorized<c10::complex<double>>& a,
|
| 52 |
+
const Vectorized<c10::complex<double>>& b) {
|
| 53 |
+
// convert c10::complex<V> index mask to V index mask: xy -> xxyy
|
| 54 |
+
// NOLINTNEXTLINE(clang-diagnostic-warning)
|
| 55 |
+
switch (mask) {
|
| 56 |
+
case 0:
|
| 57 |
+
return a;
|
| 58 |
+
case 1:
|
| 59 |
+
return _mm512_mask_blend_pd(0x03, a.values, b.values); //b0000 0001 = b0000 0011
|
| 60 |
+
case 2:
|
| 61 |
+
return _mm512_mask_blend_pd(0x0C, a.values, b.values); //b0000 0010 = b0000 1100
|
| 62 |
+
case 3:
|
| 63 |
+
return _mm512_mask_blend_pd(0x0F, a.values, b.values); //b0000 0011 = b0000 1111
|
| 64 |
+
case 4:
|
| 65 |
+
return _mm512_mask_blend_pd(0x30, a.values, b.values); //b0000 0100 = b0011 0000
|
| 66 |
+
case 5:
|
| 67 |
+
return _mm512_mask_blend_pd(0x33, a.values, b.values); //b0000 0101 = b0011 0011
|
| 68 |
+
case 6:
|
| 69 |
+
return _mm512_mask_blend_pd(0x3C, a.values, b.values); //b0000 0110 = b0011 1100
|
| 70 |
+
case 7:
|
| 71 |
+
return _mm512_mask_blend_pd(0x3F, a.values, b.values); //b0000 0111 = b0011 1111
|
| 72 |
+
case 8:
|
| 73 |
+
return _mm512_mask_blend_pd(0xC0, a.values, b.values); //b0000 1000 = b1100 0000
|
| 74 |
+
case 9:
|
| 75 |
+
return _mm512_mask_blend_pd(0xC3, a.values, b.values); //b0000 1001 = b1100 0011
|
| 76 |
+
case 10:
|
| 77 |
+
return _mm512_mask_blend_pd(0xCC, a.values, b.values); //b0000 1010 = b1100 1100
|
| 78 |
+
case 11:
|
| 79 |
+
return _mm512_mask_blend_pd(0xCF, a.values, b.values); //b0000 1011 = b1100 1111
|
| 80 |
+
case 12:
|
| 81 |
+
return _mm512_mask_blend_pd(0xF0, a.values, b.values); //b0000 1100 = b1111 0000
|
| 82 |
+
case 13:
|
| 83 |
+
return _mm512_mask_blend_pd(0xF3, a.values, b.values); //b0000 1101 = b1111 0011
|
| 84 |
+
case 14:
|
| 85 |
+
return _mm512_mask_blend_pd(0xFC, a.values, b.values); //b0000 1110 = b1111 1100
|
| 86 |
+
case 15:
|
| 87 |
+
return _mm512_mask_blend_pd(0xFF, a.values, b.values); //b0000 1111 = b1111 1111
|
| 88 |
+
}
|
| 89 |
+
return b;
|
| 90 |
+
}
|
| 91 |
+
static Vectorized<c10::complex<double>> blendv(const Vectorized<c10::complex<double>>& a,
|
| 92 |
+
const Vectorized<c10::complex<double>>& b,
|
| 93 |
+
const Vectorized<c10::complex<double>>& mask) {
|
| 94 |
+
// convert c10::complex<V> index mask to V index mask: xy -> xxyy
|
| 95 |
+
auto mask_ = _mm512_unpacklo_pd(mask.values, mask.values);
|
| 96 |
+
auto all_ones = _mm512_set1_epi64(0xFFFFFFFFFFFFFFFF);
|
| 97 |
+
auto mmask = _mm512_cmp_epi64_mask(_mm512_castpd_si512(mask_), all_ones, _MM_CMPINT_EQ);
|
| 98 |
+
return _mm512_mask_blend_pd(mmask, a.values, b.values);
|
| 99 |
+
}
|
| 100 |
+
template<typename step_t>
|
| 101 |
+
static Vectorized<c10::complex<double>> arange(c10::complex<double> base = 0.,
|
| 102 |
+
step_t step = static_cast<step_t>(1)) {
|
| 103 |
+
return Vectorized<c10::complex<double>>(base,
|
| 104 |
+
base + c10::complex<double>(1)*step,
|
| 105 |
+
base + c10::complex<double>(2)*step,
|
| 106 |
+
base + c10::complex<double>(3)*step);
|
| 107 |
+
}
|
| 108 |
+
static Vectorized<c10::complex<double>> set(const Vectorized<c10::complex<double>>& a,
|
| 109 |
+
const Vectorized<c10::complex<double>>& b,
|
| 110 |
+
int64_t count = size()) {
|
| 111 |
+
switch (count) {
|
| 112 |
+
case 0:
|
| 113 |
+
return a;
|
| 114 |
+
case 1:
|
| 115 |
+
return blend<1>(a, b);
|
| 116 |
+
case 2:
|
| 117 |
+
return blend<3>(a, b);
|
| 118 |
+
case 3:
|
| 119 |
+
return blend<7>(a, b);
|
| 120 |
+
}
|
| 121 |
+
return b;
|
| 122 |
+
}
|
| 123 |
+
static Vectorized<c10::complex<double>> loadu(const void* ptr, int64_t count = size()) {
|
| 124 |
+
if (count == size())
|
| 125 |
+
return _mm512_loadu_pd(reinterpret_cast<const double*>(ptr));
|
| 126 |
+
|
| 127 |
+
__at_align__ double tmp_values[2*size()];
|
| 128 |
+
// Ensure uninitialized memory does not change the output value See https://github.com/pytorch/pytorch/issues/32502
|
| 129 |
+
// for more details. We do not initialize arrays to zero using "={0}" because gcc would compile it to two
|
| 130 |
+
// instructions while a loop would be compiled to one instruction.
|
| 131 |
+
for (const auto i : c10::irange(2*size())) {
|
| 132 |
+
tmp_values[i] = 0.0;
|
| 133 |
+
}
|
| 134 |
+
std::memcpy(
|
| 135 |
+
tmp_values,
|
| 136 |
+
reinterpret_cast<const double*>(ptr),
|
| 137 |
+
count * sizeof(c10::complex<double>));
|
| 138 |
+
return _mm512_load_pd(tmp_values);
|
| 139 |
+
}
|
| 140 |
+
void store(void* ptr, int count = size()) const {
|
| 141 |
+
if (count == size()) {
|
| 142 |
+
_mm512_storeu_pd(reinterpret_cast<double*>(ptr), values);
|
| 143 |
+
} else if (count > 0) {
|
| 144 |
+
double tmp_values[2*size()];
|
| 145 |
+
_mm512_storeu_pd(reinterpret_cast<double*>(tmp_values), values);
|
| 146 |
+
std::memcpy(ptr, tmp_values, count * sizeof(c10::complex<double>));
|
| 147 |
+
}
|
| 148 |
+
}
|
| 149 |
+
const c10::complex<double>& operator[](int idx) const = delete;
|
| 150 |
+
c10::complex<double>& operator[](int idx) = delete;
|
| 151 |
+
Vectorized<c10::complex<double>> map(c10::complex<double> (*const f)(const c10::complex<double> &)) const {
|
| 152 |
+
__at_align__ c10::complex<double> tmp[size()];
|
| 153 |
+
store(tmp);
|
| 154 |
+
for (const auto i : c10::irange(size())) {
|
| 155 |
+
tmp[i] = f(tmp[i]);
|
| 156 |
+
}
|
| 157 |
+
return loadu(tmp);
|
| 158 |
+
}
|
| 159 |
+
// AVX512 doesn't have horizontal add & horizontal sub instructions.
|
| 160 |
+
// TODO: hadd_pd() & hsub_pd() may have scope for improvement.
|
| 161 |
+
static inline __m512d hadd_pd(__m512d a, __m512d b) {
|
| 162 |
+
__m512i idx1 = _mm512_set_epi64(14, 6, 12, 4, 10, 2, 8, 0);
|
| 163 |
+
__m512i idx2 = _mm512_set_epi64(15, 7, 13, 5, 11, 3, 9, 1);
|
| 164 |
+
return _mm512_add_pd(_mm512_mask_permutex2var_pd(a, 0xff, idx1, b),
|
| 165 |
+
_mm512_mask_permutex2var_pd(a, 0xff, idx2, b));
|
| 166 |
+
}
|
| 167 |
+
static inline __m512d hsub_pd(__m512d a, __m512d b) {
|
| 168 |
+
__m512i idx1 = _mm512_set_epi64(14, 6, 12, 4, 10, 2, 8, 0);
|
| 169 |
+
__m512i idx2 = _mm512_set_epi64(15, 7, 13, 5, 11, 3, 9, 1);
|
| 170 |
+
return _mm512_sub_pd(_mm512_mask_permutex2var_pd(a, 0xff, idx1, b),
|
| 171 |
+
_mm512_mask_permutex2var_pd(a, 0xff, idx2, b));
|
| 172 |
+
}
|
| 173 |
+
__m512d abs_2_() const {
|
| 174 |
+
auto val_2 = _mm512_mul_pd(values, values); // a*a b*b
|
| 175 |
+
return hadd_pd(val_2, val_2); // a*a+b*b a*a+b*b
|
| 176 |
+
}
|
| 177 |
+
__m512d abs_() const {
|
| 178 |
+
auto real = _mm512_movedup_pd(values); // real real
|
| 179 |
+
// movehdup_pd does not exist...
|
| 180 |
+
auto imag = _mm512_permute_pd(values, 0xff); // imag imag
|
| 181 |
+
return Sleef_hypotd8_u05(real, imag); // abs abs
|
| 182 |
+
}
|
| 183 |
+
Vectorized<c10::complex<double>> abs() const {
|
| 184 |
+
const __m512d real_mask = _mm512_castsi512_pd(_mm512_setr_epi64(0xFFFFFFFFFFFFFFFF, 0x0000000000000000,
|
| 185 |
+
0xFFFFFFFFFFFFFFFF, 0x0000000000000000,
|
| 186 |
+
0xFFFFFFFFFFFFFFFF, 0x0000000000000000,
|
| 187 |
+
0xFFFFFFFFFFFFFFFF, 0x0000000000000000));
|
| 188 |
+
return _mm512_and_pd(abs_(), real_mask); // abs 0
|
| 189 |
+
}
|
| 190 |
+
__m512d angle_() const {
|
| 191 |
+
//angle = atan2(b/a)
|
| 192 |
+
auto b_a = _mm512_permute_pd(values, 0x55); // b a
|
| 193 |
+
return Sleef_atan2d8_u10(values, b_a); // 90-angle angle
|
| 194 |
+
}
|
| 195 |
+
Vectorized<c10::complex<double>> angle() const {
|
| 196 |
+
const __m512d real_mask = _mm512_castsi512_pd(_mm512_setr_epi64(0xFFFFFFFFFFFFFFFF, 0x0000000000000000,
|
| 197 |
+
0xFFFFFFFFFFFFFFFF, 0x0000000000000000,
|
| 198 |
+
0xFFFFFFFFFFFFFFFF, 0x0000000000000000,
|
| 199 |
+
0xFFFFFFFFFFFFFFFF, 0x0000000000000000));
|
| 200 |
+
auto angle = _mm512_permute_pd(angle_(), 0x55); // angle 90-angle
|
| 201 |
+
return _mm512_and_pd(angle, real_mask); // angle 0
|
| 202 |
+
}
|
| 203 |
+
Vectorized<c10::complex<double>> sgn() const {
|
| 204 |
+
auto abs = abs_();
|
| 205 |
+
auto zero = _mm512_setzero_pd();
|
| 206 |
+
auto mask = _mm512_cmp_pd_mask(abs, zero, _CMP_EQ_OQ);
|
| 207 |
+
auto div = _mm512_div_pd(values, abs);
|
| 208 |
+
return _mm512_mask_blend_pd(mask, div, zero);
|
| 209 |
+
}
|
| 210 |
+
__m512d real_() const {
|
| 211 |
+
const __m512d real_mask = _mm512_castsi512_pd(_mm512_setr_epi64(0xFFFFFFFFFFFFFFFF, 0x0000000000000000,
|
| 212 |
+
0xFFFFFFFFFFFFFFFF, 0x0000000000000000,
|
| 213 |
+
0xFFFFFFFFFFFFFFFF, 0x0000000000000000,
|
| 214 |
+
0xFFFFFFFFFFFFFFFF, 0x0000000000000000));
|
| 215 |
+
return _mm512_and_pd(values, real_mask);
|
| 216 |
+
}
|
| 217 |
+
Vectorized<c10::complex<double>> real() const {
|
| 218 |
+
return real_();
|
| 219 |
+
}
|
| 220 |
+
__m512d imag_() const {
|
| 221 |
+
const __m512d imag_mask = _mm512_castsi512_pd(_mm512_setr_epi64(0x0000000000000000, 0xFFFFFFFFFFFFFFFF,
|
| 222 |
+
0x0000000000000000, 0xFFFFFFFFFFFFFFFF,
|
| 223 |
+
0x0000000000000000, 0xFFFFFFFFFFFFFFFF,
|
| 224 |
+
0x0000000000000000, 0xFFFFFFFFFFFFFFFF));
|
| 225 |
+
return _mm512_and_pd(values, imag_mask);
|
| 226 |
+
}
|
| 227 |
+
Vectorized<c10::complex<double>> imag() const {
|
| 228 |
+
return _mm512_permute_pd(imag_(), 0x55); //b a
|
| 229 |
+
}
|
| 230 |
+
__m512d conj_() const {
|
| 231 |
+
const __m512d sign_mask = _mm512_setr_pd(0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0);
|
| 232 |
+
return _mm512_xor_pd(values, sign_mask); // a -b
|
| 233 |
+
}
|
| 234 |
+
Vectorized<c10::complex<double>> conj() const {
|
| 235 |
+
return conj_();
|
| 236 |
+
}
|
| 237 |
+
Vectorized<c10::complex<double>> log() const {
|
| 238 |
+
// Most trigonomic ops use the log() op to improve complex number performance.
|
| 239 |
+
return map(std::log);
|
| 240 |
+
}
|
| 241 |
+
Vectorized<c10::complex<double>> log2() const {
|
| 242 |
+
const __m512d log2_ = _mm512_set1_pd(std::log(2));
|
| 243 |
+
return _mm512_div_pd(log(), log2_);
|
| 244 |
+
}
|
| 245 |
+
Vectorized<c10::complex<double>> log10() const {
|
| 246 |
+
const __m512d log10_ = _mm512_set1_pd(std::log(10));
|
| 247 |
+
return _mm512_div_pd(log(), log10_);
|
| 248 |
+
}
|
| 249 |
+
Vectorized<c10::complex<double>> log1p() const {
|
| 250 |
+
return map(std::log1p);
|
| 251 |
+
}
|
| 252 |
+
Vectorized<c10::complex<double>> asin() const {
|
| 253 |
+
// asin(x)
|
| 254 |
+
// = -i*ln(iz + sqrt(1 -z^2))
|
| 255 |
+
// = -i*ln((ai - b) + sqrt(1 - (a + bi)*(a + bi)))
|
| 256 |
+
// = -i*ln((-b + ai) + sqrt(1 - (a**2 - b**2) - 2*abi))
|
| 257 |
+
const __m512d one = _mm512_set1_pd(1);
|
| 258 |
+
|
| 259 |
+
auto conj = conj_();
|
| 260 |
+
auto b_a = _mm512_permute_pd(conj, 0x55); //-b a
|
| 261 |
+
auto ab = _mm512_mul_pd(conj, b_a); //-ab -ab
|
| 262 |
+
auto im = _mm512_add_pd(ab, ab); //-2ab -2ab
|
| 263 |
+
|
| 264 |
+
auto val_2 = _mm512_mul_pd(values, values); // a*a b*b
|
| 265 |
+
auto re = hsub_pd(val_2, _mm512_permute_pd(val_2, 0x55)); // a*a-b*b b*b-a*a
|
| 266 |
+
re = _mm512_sub_pd(one, re);
|
| 267 |
+
|
| 268 |
+
auto root = Vectorized(_mm512_mask_blend_pd(0xAA, re, im)).sqrt(); //sqrt(re + i*im)
|
| 269 |
+
auto ln = Vectorized(_mm512_add_pd(b_a, root)).log(); //ln(iz + sqrt())
|
| 270 |
+
return Vectorized(_mm512_permute_pd(ln.values, 0x55)).conj(); //-i*ln()
|
| 271 |
+
}
|
| 272 |
+
Vectorized<c10::complex<double>> acos() const {
|
| 273 |
+
// acos(x) = pi/2 - asin(x)
|
| 274 |
+
constexpr auto pi_2d = c10::pi<double> / 2;
|
| 275 |
+
const __m512d pi_2 = _mm512_setr_pd(pi_2d, 0.0, pi_2d, 0.0, pi_2d, 0.0, pi_2d, 0.0);
|
| 276 |
+
return _mm512_sub_pd(pi_2, asin());
|
| 277 |
+
}
|
| 278 |
+
Vectorized<c10::complex<double>> atan() const;
|
| 279 |
+
Vectorized<c10::complex<double>> atanh() const {
|
| 280 |
+
return map(std::atanh);
|
| 281 |
+
}
|
| 282 |
+
Vectorized<c10::complex<double>> exp() const {
|
| 283 |
+
//exp(a + bi)
|
| 284 |
+
// = exp(a)*(cos(b) + sin(b)i)
|
| 285 |
+
auto exp = Sleef_expd8_u10(values); //exp(a) exp(b)
|
| 286 |
+
exp = _mm512_mask_blend_pd(0xAA, exp, _mm512_permute_pd(exp, 0x55)); //exp(a) exp(a)
|
| 287 |
+
|
| 288 |
+
auto sin_cos = Sleef_sincosd8_u10(values); //[sin(a), cos(a)] [sin(b), cos(b)]
|
| 289 |
+
auto cos_sin = _mm512_mask_blend_pd(0xAA, _mm512_permute_pd(sin_cos.y, 0x55),
|
| 290 |
+
sin_cos.x); //cos(b) sin(b)
|
| 291 |
+
return _mm512_mul_pd(exp, cos_sin);
|
| 292 |
+
}
|
| 293 |
+
Vectorized<c10::complex<double>> exp2() const {
|
| 294 |
+
// Use identity 2**x = exp(log(2) * x)
|
| 295 |
+
const __m512d ln_2 = _mm512_set1_pd(c10::ln_2<double>);
|
| 296 |
+
Vectorized<c10::complex<double>> scaled_values = _mm512_mul_pd(values, ln_2);
|
| 297 |
+
return scaled_values.exp();
|
| 298 |
+
}
|
| 299 |
+
Vectorized<c10::complex<double>> expm1() const {
|
| 300 |
+
return map(std::expm1);
|
| 301 |
+
}
|
| 302 |
+
Vectorized<c10::complex<double>> sin() const {
|
| 303 |
+
return map(std::sin);
|
| 304 |
+
}
|
| 305 |
+
Vectorized<c10::complex<double>> sinh() const {
|
| 306 |
+
return map(std::sinh);
|
| 307 |
+
}
|
| 308 |
+
Vectorized<c10::complex<double>> cos() const {
|
| 309 |
+
return map(std::cos);
|
| 310 |
+
}
|
| 311 |
+
Vectorized<c10::complex<double>> cosh() const {
|
| 312 |
+
return map(std::cosh);
|
| 313 |
+
}
|
| 314 |
+
Vectorized<c10::complex<double>> ceil() const {
|
| 315 |
+
return _mm512_ceil_pd(values);
|
| 316 |
+
}
|
| 317 |
+
Vectorized<c10::complex<double>> floor() const {
|
| 318 |
+
return _mm512_floor_pd(values);
|
| 319 |
+
}
|
| 320 |
+
Vectorized<c10::complex<double>> neg() const {
|
| 321 |
+
auto zero = _mm512_setzero_pd();
|
| 322 |
+
return _mm512_sub_pd(zero, values);
|
| 323 |
+
}
|
| 324 |
+
Vectorized<c10::complex<double>> round() const {
|
| 325 |
+
return _mm512_roundscale_pd(values, (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
|
| 326 |
+
}
|
| 327 |
+
Vectorized<c10::complex<double>> tan() const {
|
| 328 |
+
return map(std::tan);
|
| 329 |
+
}
|
| 330 |
+
Vectorized<c10::complex<double>> tanh() const {
|
| 331 |
+
return map(std::tanh);
|
| 332 |
+
}
|
| 333 |
+
Vectorized<c10::complex<double>> trunc() const {
|
| 334 |
+
return _mm512_roundscale_pd(values, (_MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC));
|
| 335 |
+
}
|
| 336 |
+
Vectorized<c10::complex<double>> sqrt() const {
|
| 337 |
+
return map(std::sqrt);
|
| 338 |
+
}
|
| 339 |
+
Vectorized<c10::complex<double>> reciprocal() const;
|
| 340 |
+
Vectorized<c10::complex<double>> rsqrt() const {
|
| 341 |
+
return sqrt().reciprocal();
|
| 342 |
+
}
|
| 343 |
+
Vectorized<c10::complex<double>> pow(const Vectorized<c10::complex<double>> &exp) const {
|
| 344 |
+
__at_align__ c10::complex<double> x_tmp[size()];
|
| 345 |
+
__at_align__ c10::complex<double> y_tmp[size()];
|
| 346 |
+
store(x_tmp);
|
| 347 |
+
exp.store(y_tmp);
|
| 348 |
+
for (const auto i : c10::irange(size())) {
|
| 349 |
+
x_tmp[i] = std::pow(x_tmp[i], y_tmp[i]);
|
| 350 |
+
}
|
| 351 |
+
return loadu(x_tmp);
|
| 352 |
+
}
|
| 353 |
+
// Comparison using the _CMP_**_OQ predicate.
|
| 354 |
+
// `O`: get false if an operand is NaN
|
| 355 |
+
// `Q`: do not raise if an operand is NaN
|
| 356 |
+
Vectorized<c10::complex<double>> operator==(const Vectorized<c10::complex<double>>& other) const {
|
| 357 |
+
auto mask = _mm512_cmp_pd_mask(values, other.values, _CMP_EQ_OQ);
|
| 358 |
+
return _mm512_castsi512_pd(_mm512_mask_set1_epi64(zero_vector, mask,
|
| 359 |
+
0xFFFFFFFFFFFFFFFF));
|
| 360 |
+
}
|
| 361 |
+
Vectorized<c10::complex<double>> operator!=(const Vectorized<c10::complex<double>>& other) const {
|
| 362 |
+
auto mask = _mm512_cmp_pd_mask(values, other.values, _CMP_NEQ_UQ);
|
| 363 |
+
return _mm512_castsi512_pd(_mm512_mask_set1_epi64(zero_vector, mask,
|
| 364 |
+
0xFFFFFFFFFFFFFFFF));
|
| 365 |
+
}
|
| 366 |
+
Vectorized<c10::complex<double>> operator<(const Vectorized<c10::complex<double>>& other [[maybe_unused]]) const {
|
| 367 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 368 |
+
}
|
| 369 |
+
Vectorized<c10::complex<double>> operator<=(const Vectorized<c10::complex<double>>& other [[maybe_unused]]) const {
|
| 370 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 371 |
+
}
|
| 372 |
+
Vectorized<c10::complex<double>> operator>(const Vectorized<c10::complex<double>>& other [[maybe_unused]]) const {
|
| 373 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 374 |
+
}
|
| 375 |
+
Vectorized<c10::complex<double>> operator>=(const Vectorized<c10::complex<double>>& other [[maybe_unused]]) const {
|
| 376 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
Vectorized<c10::complex<double>> eq(const Vectorized<c10::complex<double>>& other) const;
|
| 380 |
+
Vectorized<c10::complex<double>> ne(const Vectorized<c10::complex<double>>& other) const;
|
| 381 |
+
};
|
| 382 |
+
|
| 383 |
+
template <> Vectorized<c10::complex<double>> inline operator+(const Vectorized<c10::complex<double>> &a,
|
| 384 |
+
const Vectorized<c10::complex<double>> &b) {
|
| 385 |
+
return _mm512_add_pd(a, b);
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
template <> Vectorized<c10::complex<double>> inline operator-(const Vectorized<c10::complex<double>> &a,
|
| 389 |
+
const Vectorized<c10::complex<double>> &b) {
|
| 390 |
+
return _mm512_sub_pd(a, b);
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
template <> Vectorized<c10::complex<double>> inline operator*(const Vectorized<c10::complex<double>> &a,
|
| 394 |
+
const Vectorized<c10::complex<double>> &b) {
|
| 395 |
+
//(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
|
| 396 |
+
const __m512d sign_mask = _mm512_setr_pd(0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0);
|
| 397 |
+
auto ac_bd = _mm512_mul_pd(a, b); //ac bd
|
| 398 |
+
|
| 399 |
+
auto d_c = _mm512_permute_pd(b, 0x55); //d c
|
| 400 |
+
d_c = _mm512_xor_pd(sign_mask, d_c); //d -c
|
| 401 |
+
auto ad_bc = _mm512_mul_pd(a, d_c); //ad -bc
|
| 402 |
+
|
| 403 |
+
auto ret = Vectorized<c10::complex<double>>::hsub_pd(ac_bd, ad_bc); //ac - bd ad + bc
|
| 404 |
+
return ret;
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
template <> Vectorized<c10::complex<double>> inline operator/(const Vectorized<c10::complex<double>> &a,
|
| 408 |
+
const Vectorized<c10::complex<double>> &b) {
|
| 409 |
+
//re + im*i = (a + bi) / (c + di)
|
| 410 |
+
auto mask = _mm512_set1_pd(-0.f);
|
| 411 |
+
auto fabs_cd = _mm512_andnot_pd(mask, b); // |c| |d|
|
| 412 |
+
auto fabs_dc = _mm512_permute_pd(fabs_cd, 0x55); // |d| |c|
|
| 413 |
+
auto scale = _mm512_rcp14_pd(_mm512_max_pd(fabs_cd, fabs_dc)); // 1/sc 1/sc
|
| 414 |
+
auto a2 = _mm512_mul_pd(a, scale); // a/sc b/sc
|
| 415 |
+
auto b2 = _mm512_mul_pd(b, scale); // c/sc d/sc
|
| 416 |
+
auto acbd2 = _mm512_mul_pd(a2, b2);
|
| 417 |
+
|
| 418 |
+
const __m512d sign_mask = _mm512_setr_pd(-0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0);
|
| 419 |
+
auto dc2 = _mm512_permute_pd(b2, 0x55); // d/sc c/sc
|
| 420 |
+
dc2 = _mm512_xor_pd(sign_mask, dc2); // -d/|c,d| c/sc
|
| 421 |
+
auto adbc2 = _mm512_mul_pd(a2, dc2); //-ad/sc^2 bc/sc^2
|
| 422 |
+
auto res2 = Vectorized<c10::complex<double>>::hadd_pd(acbd2, adbc2); //(ac+bd)/sc^2 (bc-ad)/sc^2
|
| 423 |
+
|
| 424 |
+
// get the denominator
|
| 425 |
+
auto denom2 = Vectorized<c10::complex<double>>(b2).abs_2_(); // (c^2+d^2)/sc^2 (c^2+d^2)/sc^2
|
| 426 |
+
res2 = _mm512_div_pd(res2, denom2);
|
| 427 |
+
return res2;
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
// reciprocal. Implement this here so we can use multiplication.
|
| 431 |
+
inline Vectorized<c10::complex<double>> Vectorized<c10::complex<double>>::reciprocal() const{
|
| 432 |
+
//re + im*i = (a + bi) / (c + di)
|
| 433 |
+
//re = (ac + bd)/abs_2() = c/abs_2()
|
| 434 |
+
//im = (bc - ad)/abs_2() = d/abs_2()
|
| 435 |
+
const __m512d sign_mask = _mm512_setr_pd(0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0);
|
| 436 |
+
auto c_d = _mm512_xor_pd(sign_mask, values); //c -d
|
| 437 |
+
return _mm512_div_pd(c_d, abs_2_());
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
inline Vectorized<c10::complex<double>> Vectorized<c10::complex<double>>::atan() const {
|
| 441 |
+
// atan(x) = i/2 * ln((i + z)/(i - z))
|
| 442 |
+
const __m512d i = _mm512_setr_pd(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
|
| 443 |
+
const Vectorized i_half = _mm512_setr_pd(0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5);
|
| 444 |
+
|
| 445 |
+
auto sum = Vectorized(_mm512_add_pd(i, values)); // a 1+b
|
| 446 |
+
auto sub = Vectorized(_mm512_sub_pd(i, values)); // -a 1-b
|
| 447 |
+
auto ln = (sum/sub).log(); // ln((i + z)/(i - z))
|
| 448 |
+
return i_half*ln; // i/2*ln()
|
| 449 |
+
}
|
| 450 |
+
|
| 451 |
+
template <>
|
| 452 |
+
Vectorized<c10::complex<double>> inline maximum(const Vectorized<c10::complex<double>>& a,
|
| 453 |
+
const Vectorized<c10::complex<double>>& b) {
|
| 454 |
+
auto zero_vec = _mm512_set1_epi64(0);
|
| 455 |
+
auto abs_a = a.abs_2_();
|
| 456 |
+
auto abs_b = b.abs_2_();
|
| 457 |
+
auto mask = _mm512_cmp_pd_mask(abs_a, abs_b, _CMP_LT_OQ);
|
| 458 |
+
auto max = _mm512_mask_blend_pd(mask, a, b);
|
| 459 |
+
// Exploit the fact that all-ones is a NaN.
|
| 460 |
+
auto isnan_mask = _mm512_cmp_pd_mask(abs_a, abs_b, _CMP_UNORD_Q);
|
| 461 |
+
auto isnan = _mm512_mask_set1_epi64(zero_vec, isnan_mask,
|
| 462 |
+
0xFFFFFFFFFFFFFFFF);
|
| 463 |
+
return _mm512_or_pd(max, _mm512_castsi512_pd(isnan));
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
template <>
|
| 467 |
+
Vectorized<c10::complex<double>> inline minimum(const Vectorized<c10::complex<double>>& a,
|
| 468 |
+
const Vectorized<c10::complex<double>>& b) {
|
| 469 |
+
auto zero_vec = _mm512_set1_epi64(0);
|
| 470 |
+
auto abs_a = a.abs_2_();
|
| 471 |
+
auto abs_b = b.abs_2_();
|
| 472 |
+
auto mask = _mm512_cmp_pd_mask(abs_a, abs_b, _CMP_GT_OQ);
|
| 473 |
+
auto min = _mm512_mask_blend_pd(mask, a, b);
|
| 474 |
+
// Exploit the fact that all-ones is a NaN.
|
| 475 |
+
auto isnan_mask = _mm512_cmp_pd_mask(abs_a, abs_b, _CMP_UNORD_Q);
|
| 476 |
+
auto isnan = _mm512_mask_set1_epi64(zero_vec, isnan_mask,
|
| 477 |
+
0xFFFFFFFFFFFFFFFF);
|
| 478 |
+
return _mm512_or_pd(min, _mm512_castsi512_pd(isnan));
|
| 479 |
+
}
|
| 480 |
+
|
| 481 |
+
template <>
|
| 482 |
+
Vectorized<c10::complex<double>> inline operator&(const Vectorized<c10::complex<double>>& a,
|
| 483 |
+
const Vectorized<c10::complex<double>>& b) {
|
| 484 |
+
return _mm512_and_pd(a, b);
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
template <>
|
| 488 |
+
Vectorized<c10::complex<double>> inline operator|(const Vectorized<c10::complex<double>>& a,
|
| 489 |
+
const Vectorized<c10::complex<double>>& b) {
|
| 490 |
+
return _mm512_or_pd(a, b);
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
+
template <>
|
| 494 |
+
Vectorized<c10::complex<double>> inline operator^(const Vectorized<c10::complex<double>>& a,
|
| 495 |
+
const Vectorized<c10::complex<double>>& b) {
|
| 496 |
+
return _mm512_xor_pd(a, b);
|
| 497 |
+
}
|
| 498 |
+
|
| 499 |
+
inline Vectorized<c10::complex<double>> Vectorized<c10::complex<double>>::eq(const Vectorized<c10::complex<double>>& other) const {
|
| 500 |
+
auto eq = (*this == other); // compares real and imag individually
|
| 501 |
+
// If both real numbers and imag numbers are equal, then the complex numbers are equal
|
| 502 |
+
return (eq.real() & eq.imag()) & Vectorized<c10::complex<double>>(_mm512_set1_pd(1.0));
|
| 503 |
+
}
|
| 504 |
+
|
| 505 |
+
inline Vectorized<c10::complex<double>> Vectorized<c10::complex<double>>::ne(const Vectorized<c10::complex<double>>& other) const {
|
| 506 |
+
auto ne = (*this != other); // compares real and imag individually
|
| 507 |
+
// If either real numbers or imag numbers are not equal, then the complex numbers are not equal
|
| 508 |
+
return (ne.real() | ne.imag()) & Vectorized<c10::complex<double>>(_mm512_set1_pd(1.0));
|
| 509 |
+
}
|
| 510 |
+
|
| 511 |
+
#endif
|
| 512 |
+
|
| 513 |
+
}}}
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec512/vec512_complex_float.h
ADDED
|
@@ -0,0 +1,1019 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// DO NOT DEFINE STATIC DATA IN THIS HEADER!
|
| 4 |
+
// See Note [Do not compile initializers with AVX]
|
| 5 |
+
|
| 6 |
+
#include <c10/util/complex.h>
|
| 7 |
+
#include <c10/util/irange.h>
|
| 8 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 9 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 10 |
+
#if defined(CPU_CAPABILITY_AVX512)
|
| 11 |
+
#define SLEEF_STATIC_LIBS
|
| 12 |
+
#include <sleef.h>
|
| 13 |
+
#endif
|
| 14 |
+
|
| 15 |
+
namespace at {
|
| 16 |
+
namespace vec {
|
| 17 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 18 |
+
inline namespace CPU_CAPABILITY {
|
| 19 |
+
|
| 20 |
+
#if defined(CPU_CAPABILITY_AVX512)
|
| 21 |
+
|
| 22 |
+
template <> class Vectorized<c10::complex<float>> {
|
| 23 |
+
private:
|
| 24 |
+
__m512 values;
|
| 25 |
+
static constexpr __m512i zero_vector {0, 0, 0, 0, 0, 0, 0, 0};
|
| 26 |
+
public:
|
| 27 |
+
using value_type = c10::complex<float>;
|
| 28 |
+
using size_type = int;
|
| 29 |
+
static constexpr size_type size() {
|
| 30 |
+
return 8;
|
| 31 |
+
}
|
| 32 |
+
Vectorized() {}
|
| 33 |
+
Vectorized(__m512 v) : values(v) {}
|
| 34 |
+
Vectorized(c10::complex<float> val) {
|
| 35 |
+
float real_value = val.real();
|
| 36 |
+
float imag_value = val.imag();
|
| 37 |
+
values = _mm512_setr_ps(real_value, imag_value,
|
| 38 |
+
real_value, imag_value,
|
| 39 |
+
real_value, imag_value,
|
| 40 |
+
real_value, imag_value,
|
| 41 |
+
real_value, imag_value,
|
| 42 |
+
real_value, imag_value,
|
| 43 |
+
real_value, imag_value,
|
| 44 |
+
real_value, imag_value);
|
| 45 |
+
}
|
| 46 |
+
Vectorized(c10::complex<float> val1, c10::complex<float> val2,
|
| 47 |
+
c10::complex<float> val3, c10::complex<float> val4,
|
| 48 |
+
c10::complex<float> val5, c10::complex<float> val6,
|
| 49 |
+
c10::complex<float> val7, c10::complex<float> val8) {
|
| 50 |
+
values = _mm512_setr_ps(val1.real(), val1.imag(),
|
| 51 |
+
val2.real(), val2.imag(),
|
| 52 |
+
val3.real(), val3.imag(),
|
| 53 |
+
val4.real(), val4.imag(),
|
| 54 |
+
val5.real(), val5.imag(),
|
| 55 |
+
val6.real(), val6.imag(),
|
| 56 |
+
val7.real(), val7.imag(),
|
| 57 |
+
val8.real(), val8.imag());
|
| 58 |
+
}
|
| 59 |
+
operator __m512() const {
|
| 60 |
+
return values;
|
| 61 |
+
}
|
| 62 |
+
template <int64_t mask>
|
| 63 |
+
static Vectorized<c10::complex<float>> blend(const Vectorized<c10::complex<float>>& a,
|
| 64 |
+
const Vectorized<c10::complex<float>>& b) {
|
| 65 |
+
// convert c10::complex<V> index mask to V index mask: xy -> xxyy
|
| 66 |
+
static_assert(mask > -1 && mask < 256, "Unexpected mask value");
|
| 67 |
+
// The compiler would hopefully convert this switch condition
|
| 68 |
+
// into a jump table
|
| 69 |
+
switch (mask) {
|
| 70 |
+
case 0:
|
| 71 |
+
return a;
|
| 72 |
+
case 1:
|
| 73 |
+
return _mm512_mask_blend_ps(0x03, a.values, b.values);
|
| 74 |
+
case 2:
|
| 75 |
+
return _mm512_mask_blend_ps(0x0C, a.values, b.values);
|
| 76 |
+
case 3:
|
| 77 |
+
return _mm512_mask_blend_ps(0x0F, a.values, b.values);
|
| 78 |
+
case 4:
|
| 79 |
+
return _mm512_mask_blend_ps(0x30, a.values, b.values);
|
| 80 |
+
case 5:
|
| 81 |
+
return _mm512_mask_blend_ps(0x33, a.values, b.values);
|
| 82 |
+
case 6:
|
| 83 |
+
return _mm512_mask_blend_ps(0x3C, a.values, b.values);
|
| 84 |
+
case 7:
|
| 85 |
+
return _mm512_mask_blend_ps(0x3F, a.values, b.values);
|
| 86 |
+
case 8:
|
| 87 |
+
return _mm512_mask_blend_ps(0xC0, a.values, b.values);
|
| 88 |
+
case 9:
|
| 89 |
+
return _mm512_mask_blend_ps(0xC3, a.values, b.values);
|
| 90 |
+
case 10:
|
| 91 |
+
return _mm512_mask_blend_ps(0xCC, a.values, b.values);
|
| 92 |
+
case 11:
|
| 93 |
+
return _mm512_mask_blend_ps(0xCF, a.values, b.values);
|
| 94 |
+
case 12:
|
| 95 |
+
return _mm512_mask_blend_ps(0xF0, a.values, b.values);
|
| 96 |
+
case 13:
|
| 97 |
+
return _mm512_mask_blend_ps(0xF3, a.values, b.values);
|
| 98 |
+
case 14:
|
| 99 |
+
return _mm512_mask_blend_ps(0xFC, a.values, b.values);
|
| 100 |
+
case 15:
|
| 101 |
+
return _mm512_mask_blend_ps(0xFF, a.values, b.values);
|
| 102 |
+
case 16:
|
| 103 |
+
return _mm512_mask_blend_ps(0x300, a.values, b.values);
|
| 104 |
+
case 17:
|
| 105 |
+
return _mm512_mask_blend_ps(0x303, a.values, b.values);
|
| 106 |
+
case 18:
|
| 107 |
+
return _mm512_mask_blend_ps(0x30C, a.values, b.values);
|
| 108 |
+
case 19:
|
| 109 |
+
return _mm512_mask_blend_ps(0x30F, a.values, b.values);
|
| 110 |
+
case 20:
|
| 111 |
+
return _mm512_mask_blend_ps(0x330, a.values, b.values);
|
| 112 |
+
case 21:
|
| 113 |
+
return _mm512_mask_blend_ps(0x333, a.values, b.values);
|
| 114 |
+
case 22:
|
| 115 |
+
return _mm512_mask_blend_ps(0x33C, a.values, b.values);
|
| 116 |
+
case 23:
|
| 117 |
+
return _mm512_mask_blend_ps(0x33F, a.values, b.values);
|
| 118 |
+
case 24:
|
| 119 |
+
return _mm512_mask_blend_ps(0x3C0, a.values, b.values);
|
| 120 |
+
case 25:
|
| 121 |
+
return _mm512_mask_blend_ps(0x3C3, a.values, b.values);
|
| 122 |
+
case 26:
|
| 123 |
+
return _mm512_mask_blend_ps(0x3CC, a.values, b.values);
|
| 124 |
+
case 27:
|
| 125 |
+
return _mm512_mask_blend_ps(0x3CF, a.values, b.values);
|
| 126 |
+
case 28:
|
| 127 |
+
return _mm512_mask_blend_ps(0x3F0, a.values, b.values);
|
| 128 |
+
case 29:
|
| 129 |
+
return _mm512_mask_blend_ps(0x3F3, a.values, b.values);
|
| 130 |
+
case 30:
|
| 131 |
+
return _mm512_mask_blend_ps(0x3FC, a.values, b.values);
|
| 132 |
+
case 31:
|
| 133 |
+
return _mm512_mask_blend_ps(0x3FF, a.values, b.values);
|
| 134 |
+
case 32:
|
| 135 |
+
return _mm512_mask_blend_ps(0xC00, a.values, b.values);
|
| 136 |
+
case 33:
|
| 137 |
+
return _mm512_mask_blend_ps(0xC03, a.values, b.values);
|
| 138 |
+
case 34:
|
| 139 |
+
return _mm512_mask_blend_ps(0xC0C, a.values, b.values);
|
| 140 |
+
case 35:
|
| 141 |
+
return _mm512_mask_blend_ps(0xC0F, a.values, b.values);
|
| 142 |
+
case 36:
|
| 143 |
+
return _mm512_mask_blend_ps(0xC30, a.values, b.values);
|
| 144 |
+
case 37:
|
| 145 |
+
return _mm512_mask_blend_ps(0xC33, a.values, b.values);
|
| 146 |
+
case 38:
|
| 147 |
+
return _mm512_mask_blend_ps(0xC3C, a.values, b.values);
|
| 148 |
+
case 39:
|
| 149 |
+
return _mm512_mask_blend_ps(0xC3F, a.values, b.values);
|
| 150 |
+
case 40:
|
| 151 |
+
return _mm512_mask_blend_ps(0xCC0, a.values, b.values);
|
| 152 |
+
case 41:
|
| 153 |
+
return _mm512_mask_blend_ps(0xCC3, a.values, b.values);
|
| 154 |
+
case 42:
|
| 155 |
+
return _mm512_mask_blend_ps(0xCCC, a.values, b.values);
|
| 156 |
+
case 43:
|
| 157 |
+
return _mm512_mask_blend_ps(0xCCF, a.values, b.values);
|
| 158 |
+
case 44:
|
| 159 |
+
return _mm512_mask_blend_ps(0xCF0, a.values, b.values);
|
| 160 |
+
case 45:
|
| 161 |
+
return _mm512_mask_blend_ps(0xCF3, a.values, b.values);
|
| 162 |
+
case 46:
|
| 163 |
+
return _mm512_mask_blend_ps(0xCFC, a.values, b.values);
|
| 164 |
+
case 47:
|
| 165 |
+
return _mm512_mask_blend_ps(0xCFF, a.values, b.values);
|
| 166 |
+
case 48:
|
| 167 |
+
return _mm512_mask_blend_ps(0xF00, a.values, b.values);
|
| 168 |
+
case 49:
|
| 169 |
+
return _mm512_mask_blend_ps(0xF03, a.values, b.values);
|
| 170 |
+
case 50:
|
| 171 |
+
return _mm512_mask_blend_ps(0xF0C, a.values, b.values);
|
| 172 |
+
case 51:
|
| 173 |
+
return _mm512_mask_blend_ps(0xF0F, a.values, b.values);
|
| 174 |
+
case 52:
|
| 175 |
+
return _mm512_mask_blend_ps(0xF30, a.values, b.values);
|
| 176 |
+
case 53:
|
| 177 |
+
return _mm512_mask_blend_ps(0xF33, a.values, b.values);
|
| 178 |
+
case 54:
|
| 179 |
+
return _mm512_mask_blend_ps(0xF3C, a.values, b.values);
|
| 180 |
+
case 55:
|
| 181 |
+
return _mm512_mask_blend_ps(0xF3F, a.values, b.values);
|
| 182 |
+
case 56:
|
| 183 |
+
return _mm512_mask_blend_ps(0xFC0, a.values, b.values);
|
| 184 |
+
case 57:
|
| 185 |
+
return _mm512_mask_blend_ps(0xFC3, a.values, b.values);
|
| 186 |
+
case 58:
|
| 187 |
+
return _mm512_mask_blend_ps(0xFCC, a.values, b.values);
|
| 188 |
+
case 59:
|
| 189 |
+
return _mm512_mask_blend_ps(0xFCF, a.values, b.values);
|
| 190 |
+
case 60:
|
| 191 |
+
return _mm512_mask_blend_ps(0xFF0, a.values, b.values);
|
| 192 |
+
case 61:
|
| 193 |
+
return _mm512_mask_blend_ps(0xFF3, a.values, b.values);
|
| 194 |
+
case 62:
|
| 195 |
+
return _mm512_mask_blend_ps(0xFFC, a.values, b.values);
|
| 196 |
+
case 63:
|
| 197 |
+
return _mm512_mask_blend_ps(0xFFF, a.values, b.values);
|
| 198 |
+
case 64:
|
| 199 |
+
return _mm512_mask_blend_ps(0x3000, a.values, b.values);
|
| 200 |
+
case 65:
|
| 201 |
+
return _mm512_mask_blend_ps(0x3003, a.values, b.values);
|
| 202 |
+
case 66:
|
| 203 |
+
return _mm512_mask_blend_ps(0x300C, a.values, b.values);
|
| 204 |
+
case 67:
|
| 205 |
+
return _mm512_mask_blend_ps(0x300F, a.values, b.values);
|
| 206 |
+
case 68:
|
| 207 |
+
return _mm512_mask_blend_ps(0x3030, a.values, b.values);
|
| 208 |
+
case 69:
|
| 209 |
+
return _mm512_mask_blend_ps(0x3033, a.values, b.values);
|
| 210 |
+
case 70:
|
| 211 |
+
return _mm512_mask_blend_ps(0x303C, a.values, b.values);
|
| 212 |
+
case 71:
|
| 213 |
+
return _mm512_mask_blend_ps(0x303F, a.values, b.values);
|
| 214 |
+
case 72:
|
| 215 |
+
return _mm512_mask_blend_ps(0x30C0, a.values, b.values);
|
| 216 |
+
case 73:
|
| 217 |
+
return _mm512_mask_blend_ps(0X30C3, a.values, b.values);
|
| 218 |
+
case 74:
|
| 219 |
+
return _mm512_mask_blend_ps(0x30CC, a.values, b.values);
|
| 220 |
+
case 75:
|
| 221 |
+
return _mm512_mask_blend_ps(0x30CF, a.values, b.values);
|
| 222 |
+
case 76:
|
| 223 |
+
return _mm512_mask_blend_ps(0x30F0, a.values, b.values);
|
| 224 |
+
case 77:
|
| 225 |
+
return _mm512_mask_blend_ps(0x30F3, a.values, b.values);
|
| 226 |
+
case 78:
|
| 227 |
+
return _mm512_mask_blend_ps(0x30FC, a.values, b.values);
|
| 228 |
+
case 79:
|
| 229 |
+
return _mm512_mask_blend_ps(0x30FF, a.values, b.values);
|
| 230 |
+
case 80:
|
| 231 |
+
return _mm512_mask_blend_ps(0x3300, a.values, b.values);
|
| 232 |
+
case 81:
|
| 233 |
+
return _mm512_mask_blend_ps(0X3303, a.values, b.values);
|
| 234 |
+
case 82:
|
| 235 |
+
return _mm512_mask_blend_ps(0x330C, a.values, b.values);
|
| 236 |
+
case 83:
|
| 237 |
+
return _mm512_mask_blend_ps(0x330F, a.values, b.values);
|
| 238 |
+
case 84:
|
| 239 |
+
return _mm512_mask_blend_ps(0x3330, a.values, b.values);
|
| 240 |
+
case 85:
|
| 241 |
+
return _mm512_mask_blend_ps(0x3333, a.values, b.values);
|
| 242 |
+
case 86:
|
| 243 |
+
return _mm512_mask_blend_ps(0x333C, a.values, b.values);
|
| 244 |
+
case 87:
|
| 245 |
+
return _mm512_mask_blend_ps(0X333F, a.values, b.values);
|
| 246 |
+
case 88:
|
| 247 |
+
return _mm512_mask_blend_ps(0x33C0, a.values, b.values);
|
| 248 |
+
case 89:
|
| 249 |
+
return _mm512_mask_blend_ps(0x33C3, a.values, b.values);
|
| 250 |
+
case 90:
|
| 251 |
+
return _mm512_mask_blend_ps(0x33CC, a.values, b.values);
|
| 252 |
+
case 91:
|
| 253 |
+
return _mm512_mask_blend_ps(0x33CF, a.values, b.values);
|
| 254 |
+
case 92:
|
| 255 |
+
return _mm512_mask_blend_ps(0x33F0, a.values, b.values);
|
| 256 |
+
case 93:
|
| 257 |
+
return _mm512_mask_blend_ps(0x33F3, a.values, b.values);
|
| 258 |
+
case 94:
|
| 259 |
+
return _mm512_mask_blend_ps(0x33FC, a.values, b.values);
|
| 260 |
+
case 95:
|
| 261 |
+
return _mm512_mask_blend_ps(0x33FF, a.values, b.values);
|
| 262 |
+
case 96:
|
| 263 |
+
return _mm512_mask_blend_ps(0X3C00, a.values, b.values);
|
| 264 |
+
case 97:
|
| 265 |
+
return _mm512_mask_blend_ps(0x3C03, a.values, b.values);
|
| 266 |
+
case 98:
|
| 267 |
+
return _mm512_mask_blend_ps(0x3C0C, a.values, b.values);
|
| 268 |
+
case 99:
|
| 269 |
+
return _mm512_mask_blend_ps(0x3C0F, a.values, b.values);
|
| 270 |
+
case 100:
|
| 271 |
+
return _mm512_mask_blend_ps(0x3C30, a.values, b.values);
|
| 272 |
+
case 101:
|
| 273 |
+
return _mm512_mask_blend_ps(0x3C33, a.values, b.values);
|
| 274 |
+
case 102:
|
| 275 |
+
return _mm512_mask_blend_ps(0x3C3C, a.values, b.values);
|
| 276 |
+
case 103:
|
| 277 |
+
return _mm512_mask_blend_ps(0x3C3F, a.values, b.values);
|
| 278 |
+
case 104:
|
| 279 |
+
return _mm512_mask_blend_ps(0x3CC0, a.values, b.values);
|
| 280 |
+
case 105:
|
| 281 |
+
return _mm512_mask_blend_ps(0x3CC3, a.values, b.values);
|
| 282 |
+
case 106:
|
| 283 |
+
return _mm512_mask_blend_ps(0x3CCC, a.values, b.values);
|
| 284 |
+
case 107:
|
| 285 |
+
return _mm512_mask_blend_ps(0x3CCF, a.values, b.values);
|
| 286 |
+
case 108:
|
| 287 |
+
return _mm512_mask_blend_ps(0x3CF0, a.values, b.values);
|
| 288 |
+
case 109:
|
| 289 |
+
return _mm512_mask_blend_ps(0x3CF3, a.values, b.values);
|
| 290 |
+
case 110:
|
| 291 |
+
return _mm512_mask_blend_ps(0x3CFC, a.values, b.values);
|
| 292 |
+
case 111:
|
| 293 |
+
return _mm512_mask_blend_ps(0x3CFF, a.values, b.values);
|
| 294 |
+
case 112:
|
| 295 |
+
return _mm512_mask_blend_ps(0x3F00, a.values, b.values);
|
| 296 |
+
case 113:
|
| 297 |
+
return _mm512_mask_blend_ps(0x3F03, a.values, b.values);
|
| 298 |
+
case 114:
|
| 299 |
+
return _mm512_mask_blend_ps(0x3F0C, a.values, b.values);
|
| 300 |
+
case 115:
|
| 301 |
+
return _mm512_mask_blend_ps(0x3F0F, a.values, b.values);
|
| 302 |
+
case 116:
|
| 303 |
+
return _mm512_mask_blend_ps(0x3F30, a.values, b.values);
|
| 304 |
+
case 117:
|
| 305 |
+
return _mm512_mask_blend_ps(0x3F33, a.values, b.values);
|
| 306 |
+
case 118:
|
| 307 |
+
return _mm512_mask_blend_ps(0x3F3C, a.values, b.values);
|
| 308 |
+
case 119:
|
| 309 |
+
return _mm512_mask_blend_ps(0x3F3F, a.values, b.values);
|
| 310 |
+
case 120:
|
| 311 |
+
return _mm512_mask_blend_ps(0x3FC0, a.values, b.values);
|
| 312 |
+
case 121:
|
| 313 |
+
return _mm512_mask_blend_ps(0x3FC3, a.values, b.values);
|
| 314 |
+
case 122:
|
| 315 |
+
return _mm512_mask_blend_ps(0x3FCC, a.values, b.values);
|
| 316 |
+
case 123:
|
| 317 |
+
return _mm512_mask_blend_ps(0x3FCF, a.values, b.values);
|
| 318 |
+
case 124:
|
| 319 |
+
return _mm512_mask_blend_ps(0x3FF0, a.values, b.values);
|
| 320 |
+
case 125:
|
| 321 |
+
return _mm512_mask_blend_ps(0x3FF3, a.values, b.values);
|
| 322 |
+
case 126:
|
| 323 |
+
return _mm512_mask_blend_ps(0x3FFC, a.values, b.values);
|
| 324 |
+
case 127:
|
| 325 |
+
return _mm512_mask_blend_ps(0x3FFF, a.values, b.values);
|
| 326 |
+
case 128:
|
| 327 |
+
return _mm512_mask_blend_ps(0xC000, a.values, b.values);
|
| 328 |
+
case 129:
|
| 329 |
+
return _mm512_mask_blend_ps(0xC003, a.values, b.values);
|
| 330 |
+
case 130:
|
| 331 |
+
return _mm512_mask_blend_ps(0xC00C, a.values, b.values);
|
| 332 |
+
case 131:
|
| 333 |
+
return _mm512_mask_blend_ps(0xC00F, a.values, b.values);
|
| 334 |
+
case 132:
|
| 335 |
+
return _mm512_mask_blend_ps(0xC030, a.values, b.values);
|
| 336 |
+
case 133:
|
| 337 |
+
return _mm512_mask_blend_ps(0xC033, a.values, b.values);
|
| 338 |
+
case 134:
|
| 339 |
+
return _mm512_mask_blend_ps(0xC03C, a.values, b.values);
|
| 340 |
+
case 135:
|
| 341 |
+
return _mm512_mask_blend_ps(0xC03F, a.values, b.values);
|
| 342 |
+
case 136:
|
| 343 |
+
return _mm512_mask_blend_ps(0xC0C0, a.values, b.values);
|
| 344 |
+
case 137:
|
| 345 |
+
return _mm512_mask_blend_ps(0xC0C3, a.values, b.values);
|
| 346 |
+
case 138:
|
| 347 |
+
return _mm512_mask_blend_ps(0xC0CC, a.values, b.values);
|
| 348 |
+
case 139:
|
| 349 |
+
return _mm512_mask_blend_ps(0xC0CF, a.values, b.values);
|
| 350 |
+
case 140:
|
| 351 |
+
return _mm512_mask_blend_ps(0xC0F0, a.values, b.values);
|
| 352 |
+
case 141:
|
| 353 |
+
return _mm512_mask_blend_ps(0xC0F3, a.values, b.values);
|
| 354 |
+
case 142:
|
| 355 |
+
return _mm512_mask_blend_ps(0xC0FC, a.values, b.values);
|
| 356 |
+
case 143:
|
| 357 |
+
return _mm512_mask_blend_ps(0xC0FF, a.values, b.values);
|
| 358 |
+
case 144:
|
| 359 |
+
return _mm512_mask_blend_ps(0xC300, a.values, b.values);
|
| 360 |
+
case 145:
|
| 361 |
+
return _mm512_mask_blend_ps(0xC303, a.values, b.values);
|
| 362 |
+
case 146:
|
| 363 |
+
return _mm512_mask_blend_ps(0xC30C, a.values, b.values);
|
| 364 |
+
case 147:
|
| 365 |
+
return _mm512_mask_blend_ps(0xC30F, a.values, b.values);
|
| 366 |
+
case 148:
|
| 367 |
+
return _mm512_mask_blend_ps(0xC330, a.values, b.values);
|
| 368 |
+
case 149:
|
| 369 |
+
return _mm512_mask_blend_ps(0xC333, a.values, b.values);
|
| 370 |
+
case 150:
|
| 371 |
+
return _mm512_mask_blend_ps(0xC33C, a.values, b.values);
|
| 372 |
+
case 151:
|
| 373 |
+
return _mm512_mask_blend_ps(0xC33F, a.values, b.values);
|
| 374 |
+
case 152:
|
| 375 |
+
return _mm512_mask_blend_ps(0xC3C0, a.values, b.values);
|
| 376 |
+
case 153:
|
| 377 |
+
return _mm512_mask_blend_ps(0xC3C3, a.values, b.values);
|
| 378 |
+
case 154:
|
| 379 |
+
return _mm512_mask_blend_ps(0xC3CC, a.values, b.values);
|
| 380 |
+
case 155:
|
| 381 |
+
return _mm512_mask_blend_ps(0xC3CF, a.values, b.values);
|
| 382 |
+
case 156:
|
| 383 |
+
return _mm512_mask_blend_ps(0xC3F0, a.values, b.values);
|
| 384 |
+
case 157:
|
| 385 |
+
return _mm512_mask_blend_ps(0xC3F3, a.values, b.values);
|
| 386 |
+
case 158:
|
| 387 |
+
return _mm512_mask_blend_ps(0xC3FC, a.values, b.values);
|
| 388 |
+
case 159:
|
| 389 |
+
return _mm512_mask_blend_ps(0xC3FF, a.values, b.values);
|
| 390 |
+
case 160:
|
| 391 |
+
return _mm512_mask_blend_ps(0xCC00, a.values, b.values);
|
| 392 |
+
case 161:
|
| 393 |
+
return _mm512_mask_blend_ps(0xCC03, a.values, b.values);
|
| 394 |
+
case 162:
|
| 395 |
+
return _mm512_mask_blend_ps(0xCC0C, a.values, b.values);
|
| 396 |
+
case 163:
|
| 397 |
+
return _mm512_mask_blend_ps(0xCC0F, a.values, b.values);
|
| 398 |
+
case 164:
|
| 399 |
+
return _mm512_mask_blend_ps(0xCC30, a.values, b.values);
|
| 400 |
+
case 165:
|
| 401 |
+
return _mm512_mask_blend_ps(0xCC33, a.values, b.values);
|
| 402 |
+
case 166:
|
| 403 |
+
return _mm512_mask_blend_ps(0xCC3C, a.values, b.values);
|
| 404 |
+
case 167:
|
| 405 |
+
return _mm512_mask_blend_ps(0xCC3F, a.values, b.values);
|
| 406 |
+
case 168:
|
| 407 |
+
return _mm512_mask_blend_ps(0xCCC0, a.values, b.values);
|
| 408 |
+
case 169:
|
| 409 |
+
return _mm512_mask_blend_ps(0xCCC3, a.values, b.values);
|
| 410 |
+
case 170:
|
| 411 |
+
return _mm512_mask_blend_ps(0xCCCC, a.values, b.values);
|
| 412 |
+
case 171:
|
| 413 |
+
return _mm512_mask_blend_ps(0xCCCF, a.values, b.values);
|
| 414 |
+
case 172:
|
| 415 |
+
return _mm512_mask_blend_ps(0xCCF0, a.values, b.values);
|
| 416 |
+
case 173:
|
| 417 |
+
return _mm512_mask_blend_ps(0xCCF3, a.values, b.values);
|
| 418 |
+
case 174:
|
| 419 |
+
return _mm512_mask_blend_ps(0xCCFC, a.values, b.values);
|
| 420 |
+
case 175:
|
| 421 |
+
return _mm512_mask_blend_ps(0xCCFF, a.values, b.values);
|
| 422 |
+
case 176:
|
| 423 |
+
return _mm512_mask_blend_ps(0xCF00, a.values, b.values);
|
| 424 |
+
case 177:
|
| 425 |
+
return _mm512_mask_blend_ps(0xCF03, a.values, b.values);
|
| 426 |
+
case 178:
|
| 427 |
+
return _mm512_mask_blend_ps(0xCF0C, a.values, b.values);
|
| 428 |
+
case 179:
|
| 429 |
+
return _mm512_mask_blend_ps(0xCF0F, a.values, b.values);
|
| 430 |
+
case 180:
|
| 431 |
+
return _mm512_mask_blend_ps(0xCF30, a.values, b.values);
|
| 432 |
+
case 181:
|
| 433 |
+
return _mm512_mask_blend_ps(0xCF33, a.values, b.values);
|
| 434 |
+
case 182:
|
| 435 |
+
return _mm512_mask_blend_ps(0xCF3C, a.values, b.values);
|
| 436 |
+
case 183:
|
| 437 |
+
return _mm512_mask_blend_ps(0xCF3F, a.values, b.values);
|
| 438 |
+
case 184:
|
| 439 |
+
return _mm512_mask_blend_ps(0xCFC0, a.values, b.values);
|
| 440 |
+
case 185:
|
| 441 |
+
return _mm512_mask_blend_ps(0xCFC3, a.values, b.values);
|
| 442 |
+
case 186:
|
| 443 |
+
return _mm512_mask_blend_ps(0xCFCC, a.values, b.values);
|
| 444 |
+
case 187:
|
| 445 |
+
return _mm512_mask_blend_ps(0xCFCF, a.values, b.values);
|
| 446 |
+
case 188:
|
| 447 |
+
return _mm512_mask_blend_ps(0xCFF0, a.values, b.values);
|
| 448 |
+
case 189:
|
| 449 |
+
return _mm512_mask_blend_ps(0xCFF3, a.values, b.values);
|
| 450 |
+
case 190:
|
| 451 |
+
return _mm512_mask_blend_ps(0xCFFC, a.values, b.values);
|
| 452 |
+
case 191:
|
| 453 |
+
return _mm512_mask_blend_ps(0xCFFF, a.values, b.values);
|
| 454 |
+
case 192:
|
| 455 |
+
return _mm512_mask_blend_ps(0xF000, a.values, b.values);
|
| 456 |
+
case 193:
|
| 457 |
+
return _mm512_mask_blend_ps(0xF003, a.values, b.values);
|
| 458 |
+
case 194:
|
| 459 |
+
return _mm512_mask_blend_ps(0xF00C, a.values, b.values);
|
| 460 |
+
case 195:
|
| 461 |
+
return _mm512_mask_blend_ps(0xF00F, a.values, b.values);
|
| 462 |
+
case 196:
|
| 463 |
+
return _mm512_mask_blend_ps(0xF030, a.values, b.values);
|
| 464 |
+
case 197:
|
| 465 |
+
return _mm512_mask_blend_ps(0xF033, a.values, b.values);
|
| 466 |
+
case 198:
|
| 467 |
+
return _mm512_mask_blend_ps(0xF03C, a.values, b.values);
|
| 468 |
+
case 199:
|
| 469 |
+
return _mm512_mask_blend_ps(0xF03F, a.values, b.values);
|
| 470 |
+
case 200:
|
| 471 |
+
return _mm512_mask_blend_ps(0XF0C0, a.values, b.values);
|
| 472 |
+
case 201:
|
| 473 |
+
return _mm512_mask_blend_ps(0xF0C3, a.values, b.values);
|
| 474 |
+
case 202:
|
| 475 |
+
return _mm512_mask_blend_ps(0xF0CC, a.values, b.values);
|
| 476 |
+
case 203:
|
| 477 |
+
return _mm512_mask_blend_ps(0xF0CF, a.values, b.values);
|
| 478 |
+
case 204:
|
| 479 |
+
return _mm512_mask_blend_ps(0xF0F0, a.values, b.values);
|
| 480 |
+
case 205:
|
| 481 |
+
return _mm512_mask_blend_ps(0xF0F3, a.values, b.values);
|
| 482 |
+
case 206:
|
| 483 |
+
return _mm512_mask_blend_ps(0xF0FC, a.values, b.values);
|
| 484 |
+
case 207:
|
| 485 |
+
return _mm512_mask_blend_ps(0xF0FF, a.values, b.values);
|
| 486 |
+
case 208:
|
| 487 |
+
return _mm512_mask_blend_ps(0XF300, a.values, b.values);
|
| 488 |
+
case 209:
|
| 489 |
+
return _mm512_mask_blend_ps(0xF303, a.values, b.values);
|
| 490 |
+
case 210:
|
| 491 |
+
return _mm512_mask_blend_ps(0xF30C, a.values, b.values);
|
| 492 |
+
case 211:
|
| 493 |
+
return _mm512_mask_blend_ps(0xF30F, a.values, b.values);
|
| 494 |
+
case 212:
|
| 495 |
+
return _mm512_mask_blend_ps(0xF330, a.values, b.values);
|
| 496 |
+
case 213:
|
| 497 |
+
return _mm512_mask_blend_ps(0xF333, a.values, b.values);
|
| 498 |
+
case 214:
|
| 499 |
+
return _mm512_mask_blend_ps(0XF33C, a.values, b.values);
|
| 500 |
+
case 215:
|
| 501 |
+
return _mm512_mask_blend_ps(0xF33F, a.values, b.values);
|
| 502 |
+
case 216:
|
| 503 |
+
return _mm512_mask_blend_ps(0xF3C0, a.values, b.values);
|
| 504 |
+
case 217:
|
| 505 |
+
return _mm512_mask_blend_ps(0xF3C3, a.values, b.values);
|
| 506 |
+
case 218:
|
| 507 |
+
return _mm512_mask_blend_ps(0xF3CC, a.values, b.values);
|
| 508 |
+
case 219:
|
| 509 |
+
return _mm512_mask_blend_ps(0xF3CF, a.values, b.values);
|
| 510 |
+
case 220:
|
| 511 |
+
return _mm512_mask_blend_ps(0xF3F0, a.values, b.values);
|
| 512 |
+
case 221:
|
| 513 |
+
return _mm512_mask_blend_ps(0xF3F3, a.values, b.values);
|
| 514 |
+
case 222:
|
| 515 |
+
return _mm512_mask_blend_ps(0xF3FC, a.values, b.values);
|
| 516 |
+
case 223:
|
| 517 |
+
return _mm512_mask_blend_ps(0XF3FF, a.values, b.values);
|
| 518 |
+
case 224:
|
| 519 |
+
return _mm512_mask_blend_ps(0xFC00, a.values, b.values);
|
| 520 |
+
case 225:
|
| 521 |
+
return _mm512_mask_blend_ps(0xFC03, a.values, b.values);
|
| 522 |
+
case 226:
|
| 523 |
+
return _mm512_mask_blend_ps(0xFC0C, a.values, b.values);
|
| 524 |
+
case 227:
|
| 525 |
+
return _mm512_mask_blend_ps(0xFC0F, a.values, b.values);
|
| 526 |
+
case 228:
|
| 527 |
+
return _mm512_mask_blend_ps(0xFC30, a.values, b.values);
|
| 528 |
+
case 229:
|
| 529 |
+
return _mm512_mask_blend_ps(0xFC33, a.values, b.values);
|
| 530 |
+
case 230:
|
| 531 |
+
return _mm512_mask_blend_ps(0xFC3C, a.values, b.values);
|
| 532 |
+
case 231:
|
| 533 |
+
return _mm512_mask_blend_ps(0xFC3F, a.values, b.values);
|
| 534 |
+
case 232:
|
| 535 |
+
return _mm512_mask_blend_ps(0xFCC0, a.values, b.values);
|
| 536 |
+
case 233:
|
| 537 |
+
return _mm512_mask_blend_ps(0xFCC3, a.values, b.values);
|
| 538 |
+
case 234:
|
| 539 |
+
return _mm512_mask_blend_ps(0xFCCC, a.values, b.values);
|
| 540 |
+
case 235:
|
| 541 |
+
return _mm512_mask_blend_ps(0xFCCF, a.values, b.values);
|
| 542 |
+
case 236:
|
| 543 |
+
return _mm512_mask_blend_ps(0xFCF0, a.values, b.values);
|
| 544 |
+
case 237:
|
| 545 |
+
return _mm512_mask_blend_ps(0xFCF3, a.values, b.values);
|
| 546 |
+
case 238:
|
| 547 |
+
return _mm512_mask_blend_ps(0xFCFC, a.values, b.values);
|
| 548 |
+
case 239:
|
| 549 |
+
return _mm512_mask_blend_ps(0xFCFF, a.values, b.values);
|
| 550 |
+
case 240:
|
| 551 |
+
return _mm512_mask_blend_ps(0xFF00, a.values, b.values);
|
| 552 |
+
case 241:
|
| 553 |
+
return _mm512_mask_blend_ps(0xFF03, a.values, b.values);
|
| 554 |
+
case 242:
|
| 555 |
+
return _mm512_mask_blend_ps(0xFF0C, a.values, b.values);
|
| 556 |
+
case 243:
|
| 557 |
+
return _mm512_mask_blend_ps(0xFF0F, a.values, b.values);
|
| 558 |
+
case 244:
|
| 559 |
+
return _mm512_mask_blend_ps(0xFF30, a.values, b.values);
|
| 560 |
+
case 245:
|
| 561 |
+
return _mm512_mask_blend_ps(0xFF33, a.values, b.values);
|
| 562 |
+
case 246:
|
| 563 |
+
return _mm512_mask_blend_ps(0xFF3C, a.values, b.values);
|
| 564 |
+
case 247:
|
| 565 |
+
return _mm512_mask_blend_ps(0xFF3F, a.values, b.values);
|
| 566 |
+
case 248:
|
| 567 |
+
return _mm512_mask_blend_ps(0xFFC0, a.values, b.values);
|
| 568 |
+
case 249:
|
| 569 |
+
return _mm512_mask_blend_ps(0xFFC3, a.values, b.values);
|
| 570 |
+
case 250:
|
| 571 |
+
return _mm512_mask_blend_ps(0xFFCC, a.values, b.values);
|
| 572 |
+
case 251:
|
| 573 |
+
return _mm512_mask_blend_ps(0xFFCF, a.values, b.values);
|
| 574 |
+
case 252:
|
| 575 |
+
return _mm512_mask_blend_ps(0xFFF0, a.values, b.values);
|
| 576 |
+
case 253:
|
| 577 |
+
return _mm512_mask_blend_ps(0xFFF3, a.values, b.values);
|
| 578 |
+
case 254:
|
| 579 |
+
return _mm512_mask_blend_ps(0xFFFC, a.values, b.values);
|
| 580 |
+
default: break;
|
| 581 |
+
}
|
| 582 |
+
return b;
|
| 583 |
+
}
|
| 584 |
+
static Vectorized<c10::complex<float>> blendv(const Vectorized<c10::complex<float>>& a,
|
| 585 |
+
const Vectorized<c10::complex<float>>& b,
|
| 586 |
+
const Vectorized<c10::complex<float>>& mask) {
|
| 587 |
+
// convert c10::complex<V> index mask to V index mask: xy -> xxyy
|
| 588 |
+
auto mask_ = _mm512_unpacklo_ps(mask.values, mask.values);
|
| 589 |
+
auto all_ones = _mm512_set1_epi32(0xFFFFFFFF);
|
| 590 |
+
auto mmask = _mm512_cmp_epi32_mask(_mm512_castps_si512(mask_), all_ones, _MM_CMPINT_EQ);
|
| 591 |
+
return _mm512_mask_blend_ps(mmask, a.values, b.values);
|
| 592 |
+
}
|
| 593 |
+
template<typename step_t>
|
| 594 |
+
static Vectorized<c10::complex<float>> arange(c10::complex<float> base = 0.,
|
| 595 |
+
step_t step = static_cast<step_t>(1)) {
|
| 596 |
+
return Vectorized<c10::complex<float>>(base,
|
| 597 |
+
base + step,
|
| 598 |
+
base + c10::complex<float>(2)*step,
|
| 599 |
+
base + c10::complex<float>(3)*step,
|
| 600 |
+
base + c10::complex<float>(4)*step,
|
| 601 |
+
base + c10::complex<float>(5)*step,
|
| 602 |
+
base + c10::complex<float>(6)*step,
|
| 603 |
+
base + c10::complex<float>(7)*step);
|
| 604 |
+
}
|
| 605 |
+
static Vectorized<c10::complex<float>> set(const Vectorized<c10::complex<float>>& a,
|
| 606 |
+
const Vectorized<c10::complex<float>>& b,
|
| 607 |
+
int64_t count = size()) {
|
| 608 |
+
switch (count) {
|
| 609 |
+
case 0:
|
| 610 |
+
return a;
|
| 611 |
+
case 1:
|
| 612 |
+
return blend<1>(a, b);
|
| 613 |
+
case 2:
|
| 614 |
+
return blend<3>(a, b);
|
| 615 |
+
case 3:
|
| 616 |
+
return blend<7>(a, b);
|
| 617 |
+
case 4:
|
| 618 |
+
return blend<15>(a, b);
|
| 619 |
+
case 5:
|
| 620 |
+
return blend<31>(a, b);
|
| 621 |
+
case 6:
|
| 622 |
+
return blend<63>(a, b);
|
| 623 |
+
case 7:
|
| 624 |
+
return blend<127>(a, b);
|
| 625 |
+
}
|
| 626 |
+
return b;
|
| 627 |
+
}
|
| 628 |
+
static Vectorized<c10::complex<float>> loadu(const void* ptr, int64_t count = size()) {
|
| 629 |
+
if (count == size())
|
| 630 |
+
return _mm512_loadu_ps(reinterpret_cast<const float*>(ptr));
|
| 631 |
+
|
| 632 |
+
__at_align__ float tmp_values[2*size()];
|
| 633 |
+
// Ensure uninitialized memory does not change the output value See https://github.com/pytorch/pytorch/issues/32502
|
| 634 |
+
// for more details. We do not initialize arrays to zero using "={0}" because gcc would compile it to two
|
| 635 |
+
// instructions while a loop would be compiled to one instruction.
|
| 636 |
+
for (const auto i : c10::irange(2*size())) {
|
| 637 |
+
tmp_values[i] = 0.0;
|
| 638 |
+
}
|
| 639 |
+
std::memcpy(
|
| 640 |
+
tmp_values,
|
| 641 |
+
reinterpret_cast<const float*>(ptr),
|
| 642 |
+
count * sizeof(c10::complex<float>));
|
| 643 |
+
return _mm512_load_ps(tmp_values);
|
| 644 |
+
}
|
| 645 |
+
void store(void* ptr, int count = size()) const {
|
| 646 |
+
if (count == size()) {
|
| 647 |
+
_mm512_storeu_ps(reinterpret_cast<float*>(ptr), values);
|
| 648 |
+
} else if (count > 0) {
|
| 649 |
+
float tmp_values[2*size()];
|
| 650 |
+
_mm512_storeu_ps(reinterpret_cast<float*>(tmp_values), values);
|
| 651 |
+
std::memcpy(ptr, tmp_values, count * sizeof(c10::complex<float>));
|
| 652 |
+
}
|
| 653 |
+
}
|
| 654 |
+
// AVX512 doesn't have horizontal add & horizontal sub instructions.
|
| 655 |
+
// TODO: hadd_pd() & hsub_pd() may have scope for improvement.
|
| 656 |
+
static inline __m512 hadd_ps(__m512 a, __m512 b) {
|
| 657 |
+
__m512i idx1 = _mm512_set_epi32(30, 14, 28, 12, 26, 10, 24, 8, 22, 6, 20, 4, 18, 2, 16, 0);
|
| 658 |
+
__m512i idx2 = _mm512_set_epi32(31, 15, 29, 13, 27, 11, 25, 9, 23, 7, 21, 5, 19, 3, 17, 1);
|
| 659 |
+
return _mm512_add_ps(_mm512_mask_permutex2var_ps(a, 0xffff, idx1, b),
|
| 660 |
+
_mm512_mask_permutex2var_ps(a, 0xffff, idx2, b));
|
| 661 |
+
}
|
| 662 |
+
static inline __m512 hsub_ps(__m512 a, __m512 b) {
|
| 663 |
+
__m512i idx1 = _mm512_set_epi32(30, 14, 28, 12, 26, 10, 24, 8, 22, 6, 20, 4, 18, 2, 16, 0);
|
| 664 |
+
__m512i idx2 = _mm512_set_epi32(31, 15, 29, 13, 27, 11, 25, 9, 23, 7, 21, 5, 19, 3, 17, 1);
|
| 665 |
+
return _mm512_sub_ps(_mm512_mask_permutex2var_ps(a, 0xffff, idx1, b),
|
| 666 |
+
_mm512_mask_permutex2var_ps(a, 0xffff, idx2, b));
|
| 667 |
+
}
|
| 668 |
+
const c10::complex<float>& operator[](int idx) const = delete;
|
| 669 |
+
c10::complex<float>& operator[](int idx) = delete;
|
| 670 |
+
Vectorized<c10::complex<float>> map(c10::complex<float> (*const f)(const c10::complex<float> &)) const {
|
| 671 |
+
__at_align__ c10::complex<float> tmp[size()];
|
| 672 |
+
store(tmp);
|
| 673 |
+
for (const auto i : c10::irange(size())) {
|
| 674 |
+
tmp[i] = f(tmp[i]);
|
| 675 |
+
}
|
| 676 |
+
return loadu(tmp);
|
| 677 |
+
}
|
| 678 |
+
__m512 abs_2_() const {
|
| 679 |
+
auto val_2 = _mm512_mul_ps(values, values); // a*a b*b
|
| 680 |
+
auto ret = hadd_ps(val_2, val_2); // a*a+b*b a*a+b*b
|
| 681 |
+
return ret;
|
| 682 |
+
}
|
| 683 |
+
__m512 abs_() const {
|
| 684 |
+
auto real = _mm512_moveldup_ps(values); // real real
|
| 685 |
+
auto imag = _mm512_movehdup_ps(values); // imag imag
|
| 686 |
+
return Sleef_hypotf16_u05(real, imag); // abs abs
|
| 687 |
+
}
|
| 688 |
+
Vectorized<c10::complex<float>> abs() const {
|
| 689 |
+
const __m512 real_mask = _mm512_castsi512_ps(_mm512_setr_epi32(0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000,
|
| 690 |
+
0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000,
|
| 691 |
+
0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000,
|
| 692 |
+
0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000));
|
| 693 |
+
return _mm512_and_ps(abs_(), real_mask); // abs 0
|
| 694 |
+
}
|
| 695 |
+
__m512 angle_() const {
|
| 696 |
+
//angle = atan2(b/a)
|
| 697 |
+
auto b_a = _mm512_permute_ps(values, 0xB1); // b a
|
| 698 |
+
return Sleef_atan2f16_u10(values, b_a); // 90-angle angle
|
| 699 |
+
}
|
| 700 |
+
Vectorized<c10::complex<float>> angle() const {
|
| 701 |
+
const __m512 real_mask = _mm512_castsi512_ps(_mm512_setr_epi32(0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000,
|
| 702 |
+
0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000,
|
| 703 |
+
0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000,
|
| 704 |
+
0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000));
|
| 705 |
+
auto angle = _mm512_permute_ps(angle_(), 0xB1); // angle 90-angle
|
| 706 |
+
return _mm512_and_ps(angle, real_mask); // angle 0
|
| 707 |
+
}
|
| 708 |
+
Vectorized<c10::complex<float>> sgn() const {
|
| 709 |
+
auto abs = abs_();
|
| 710 |
+
auto zero = _mm512_setzero_ps();
|
| 711 |
+
auto mask = _mm512_cmp_ps_mask(abs, zero, _CMP_EQ_OQ);
|
| 712 |
+
auto div = _mm512_div_ps(values, abs);
|
| 713 |
+
return _mm512_mask_blend_ps(mask, div, zero);
|
| 714 |
+
}
|
| 715 |
+
__m512 real_() const {
|
| 716 |
+
const __m512 real_mask = _mm512_castsi512_ps(_mm512_setr_epi32(0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000,
|
| 717 |
+
0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000,
|
| 718 |
+
0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000,
|
| 719 |
+
0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000));
|
| 720 |
+
return _mm512_and_ps(values, real_mask);
|
| 721 |
+
}
|
| 722 |
+
Vectorized<c10::complex<float>> real() const {
|
| 723 |
+
return real_();
|
| 724 |
+
}
|
| 725 |
+
__m512 imag_() const {
|
| 726 |
+
const __m512 imag_mask = _mm512_castsi512_ps(_mm512_setr_epi32(0x00000000, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF,
|
| 727 |
+
0x00000000, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF,
|
| 728 |
+
0x00000000, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF,
|
| 729 |
+
0x00000000, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF));
|
| 730 |
+
return _mm512_and_ps(values, imag_mask);
|
| 731 |
+
}
|
| 732 |
+
Vectorized<c10::complex<float>> imag() const {
|
| 733 |
+
return _mm512_permute_ps(imag_(), 0xB1); //b a
|
| 734 |
+
}
|
| 735 |
+
__m512 conj_() const {
|
| 736 |
+
const __m512 sign_mask = _mm512_setr_ps(0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0,
|
| 737 |
+
0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0);
|
| 738 |
+
return _mm512_xor_ps(values, sign_mask); // a -b
|
| 739 |
+
}
|
| 740 |
+
Vectorized<c10::complex<float>> conj() const {
|
| 741 |
+
return conj_();
|
| 742 |
+
}
|
| 743 |
+
Vectorized<c10::complex<float>> log() const {
|
| 744 |
+
// Most trigonomic ops use the log() op to improve complex number performance.
|
| 745 |
+
return map(std::log);
|
| 746 |
+
}
|
| 747 |
+
Vectorized<c10::complex<float>> log2() const {
|
| 748 |
+
const __m512 log2_ = _mm512_set1_ps(std::log(2));
|
| 749 |
+
return _mm512_div_ps(log(), log2_);
|
| 750 |
+
}
|
| 751 |
+
Vectorized<c10::complex<float>> log10() const {
|
| 752 |
+
const __m512 log10_ = _mm512_set1_ps(std::log(10));
|
| 753 |
+
return _mm512_div_ps(log(), log10_);
|
| 754 |
+
}
|
| 755 |
+
Vectorized<c10::complex<float>> log1p() const {
|
| 756 |
+
return map(std::log1p);
|
| 757 |
+
}
|
| 758 |
+
Vectorized<c10::complex<float>> asin() const {
|
| 759 |
+
// asin(x)
|
| 760 |
+
// = -i*ln(iz + sqrt(1 -z^2))
|
| 761 |
+
// = -i*ln((ai - b) + sqrt(1 - (a + bi)*(a + bi)))
|
| 762 |
+
// = -i*ln((-b + ai) + sqrt(1 - (a**2 - b**2) - 2*abi))
|
| 763 |
+
const __m512 one = _mm512_set1_ps(1);
|
| 764 |
+
|
| 765 |
+
auto conj = conj_();
|
| 766 |
+
auto b_a = _mm512_permute_ps(conj, 0xB1); //-b a
|
| 767 |
+
auto ab = _mm512_mul_ps(conj, b_a); //-ab -ab
|
| 768 |
+
auto im = _mm512_add_ps(ab, ab); //-2ab -2ab
|
| 769 |
+
|
| 770 |
+
auto val_2 = _mm512_mul_ps(values, values); // a*a b*b
|
| 771 |
+
auto re = hsub_ps(val_2, _mm512_permute_ps(val_2, 0xB1)); // a*a-b*b b*b-a*a
|
| 772 |
+
re = _mm512_sub_ps(one, re);
|
| 773 |
+
|
| 774 |
+
auto root = Vectorized(_mm512_mask_blend_ps(0xAAAA, re, im)).sqrt(); //sqrt(re + i*im)
|
| 775 |
+
auto ln = Vectorized(_mm512_add_ps(b_a, root)).log(); //ln(iz + sqrt())
|
| 776 |
+
return Vectorized(_mm512_permute_ps(ln.values, 0xB1)).conj(); //-i*ln()
|
| 777 |
+
}
|
| 778 |
+
Vectorized<c10::complex<float>> acos() const {
|
| 779 |
+
return map(std::acos);
|
| 780 |
+
}
|
| 781 |
+
Vectorized<c10::complex<float>> atan() const;
|
| 782 |
+
Vectorized<c10::complex<float>> atanh() const {
|
| 783 |
+
return map(std::atanh);
|
| 784 |
+
}
|
| 785 |
+
Vectorized<c10::complex<float>> exp() const {
|
| 786 |
+
//exp(a + bi)
|
| 787 |
+
// = exp(a)*(cos(b) + sin(b)i)
|
| 788 |
+
auto exp = Sleef_expf16_u10(values); //exp(a) exp(b)
|
| 789 |
+
exp = _mm512_mask_blend_ps(0xAAAA, exp, _mm512_permute_ps(exp, 0xB1)); //exp(a) exp(a)
|
| 790 |
+
|
| 791 |
+
auto sin_cos = Sleef_sincosf16_u10(values); //[sin(a), cos(a)] [sin(b), cos(b)]
|
| 792 |
+
auto cos_sin = _mm512_mask_blend_ps(0xAAAA, _mm512_permute_ps(sin_cos.y, 0xB1),
|
| 793 |
+
sin_cos.x); //cos(b) sin(b)
|
| 794 |
+
return _mm512_mul_ps(exp, cos_sin);
|
| 795 |
+
}
|
| 796 |
+
Vectorized<c10::complex<float>> exp2() const {
|
| 797 |
+
// Use identity 2**x = exp(log(2) * x)
|
| 798 |
+
const __m512 ln_2 = _mm512_set1_ps(c10::ln_2<float>);
|
| 799 |
+
Vectorized<c10::complex<float>> scaled_values = _mm512_mul_ps(values, ln_2);
|
| 800 |
+
return scaled_values.exp();
|
| 801 |
+
}
|
| 802 |
+
Vectorized<c10::complex<float>> expm1() const {
|
| 803 |
+
return map(std::expm1);
|
| 804 |
+
}
|
| 805 |
+
Vectorized<c10::complex<float>> sin() const {
|
| 806 |
+
return map(std::sin);
|
| 807 |
+
}
|
| 808 |
+
Vectorized<c10::complex<float>> sinh() const {
|
| 809 |
+
return map(std::sinh);
|
| 810 |
+
}
|
| 811 |
+
Vectorized<c10::complex<float>> cos() const {
|
| 812 |
+
return map(std::cos);
|
| 813 |
+
}
|
| 814 |
+
Vectorized<c10::complex<float>> cosh() const {
|
| 815 |
+
return map(std::cosh);
|
| 816 |
+
}
|
| 817 |
+
Vectorized<c10::complex<float>> ceil() const {
|
| 818 |
+
return _mm512_ceil_ps(values);
|
| 819 |
+
}
|
| 820 |
+
Vectorized<c10::complex<float>> floor() const {
|
| 821 |
+
return _mm512_floor_ps(values);
|
| 822 |
+
}
|
| 823 |
+
Vectorized<c10::complex<float>> neg() const {
|
| 824 |
+
auto zero = _mm512_setzero_ps();
|
| 825 |
+
return _mm512_sub_ps(zero, values);
|
| 826 |
+
}
|
| 827 |
+
Vectorized<c10::complex<float>> round() const {
|
| 828 |
+
return _mm512_roundscale_ps(values, (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
|
| 829 |
+
}
|
| 830 |
+
Vectorized<c10::complex<float>> tan() const {
|
| 831 |
+
return map(std::tan);
|
| 832 |
+
}
|
| 833 |
+
Vectorized<c10::complex<float>> tanh() const {
|
| 834 |
+
return map(std::tanh);
|
| 835 |
+
}
|
| 836 |
+
Vectorized<c10::complex<float>> trunc() const {
|
| 837 |
+
return _mm512_roundscale_ps(values, (_MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC));
|
| 838 |
+
}
|
| 839 |
+
Vectorized<c10::complex<float>> sqrt() const {
|
| 840 |
+
return map(std::sqrt);
|
| 841 |
+
}
|
| 842 |
+
Vectorized<c10::complex<float>> reciprocal() const;
|
| 843 |
+
Vectorized<c10::complex<float>> rsqrt() const {
|
| 844 |
+
return sqrt().reciprocal();
|
| 845 |
+
}
|
| 846 |
+
Vectorized<c10::complex<float>> pow(const Vectorized<c10::complex<float>> &exp) const {
|
| 847 |
+
__at_align__ c10::complex<float> x_tmp[size()];
|
| 848 |
+
__at_align__ c10::complex<float> y_tmp[size()];
|
| 849 |
+
store(x_tmp);
|
| 850 |
+
exp.store(y_tmp);
|
| 851 |
+
for (const auto i : c10::irange(size())) {
|
| 852 |
+
x_tmp[i] = std::pow(x_tmp[i], y_tmp[i]);
|
| 853 |
+
}
|
| 854 |
+
return loadu(x_tmp);
|
| 855 |
+
}
|
| 856 |
+
// Comparison using the _CMP_**_OQ predicate.
|
| 857 |
+
// `O`: get false if an operand is NaN
|
| 858 |
+
// `Q`: do not raise if an operand is NaN
|
| 859 |
+
Vectorized<c10::complex<float>> operator==(const Vectorized<c10::complex<float>>& other) const {
|
| 860 |
+
auto mask = _mm512_cmp_ps_mask(values, other.values, _CMP_EQ_OQ);
|
| 861 |
+
return _mm512_castsi512_ps(_mm512_mask_set1_epi32(zero_vector, mask, 0xFFFFFFFF));
|
| 862 |
+
}
|
| 863 |
+
Vectorized<c10::complex<float>> operator!=(const Vectorized<c10::complex<float>>& other) const {
|
| 864 |
+
auto mask = _mm512_cmp_ps_mask(values, other.values, _CMP_NEQ_UQ);
|
| 865 |
+
return _mm512_castsi512_ps(_mm512_mask_set1_epi32(zero_vector, mask, 0xFFFFFFFF));
|
| 866 |
+
}
|
| 867 |
+
Vectorized<c10::complex<float>> operator<(const Vectorized<c10::complex<float>>& other [[maybe_unused]]) const {
|
| 868 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 869 |
+
}
|
| 870 |
+
Vectorized<c10::complex<float>> operator<=(const Vectorized<c10::complex<float>>& other [[maybe_unused]]) const {
|
| 871 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 872 |
+
}
|
| 873 |
+
Vectorized<c10::complex<float>> operator>(const Vectorized<c10::complex<float>>& other [[maybe_unused]]) const {
|
| 874 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 875 |
+
}
|
| 876 |
+
Vectorized<c10::complex<float>> operator>=(const Vectorized<c10::complex<float>>& other [[maybe_unused]]) const {
|
| 877 |
+
TORCH_CHECK(false, "not supported for complex numbers");
|
| 878 |
+
}
|
| 879 |
+
|
| 880 |
+
Vectorized<c10::complex<float>> eq(const Vectorized<c10::complex<float>>& other) const;
|
| 881 |
+
Vectorized<c10::complex<float>> ne(const Vectorized<c10::complex<float>>& other) const;
|
| 882 |
+
};
|
| 883 |
+
|
| 884 |
+
template <> Vectorized<c10::complex<float>> inline operator+(const Vectorized<c10::complex<float>> &a,
|
| 885 |
+
const Vectorized<c10::complex<float>> &b) {
|
| 886 |
+
return _mm512_add_ps(a, b);
|
| 887 |
+
}
|
| 888 |
+
|
| 889 |
+
template <> Vectorized<c10::complex<float>> inline operator-(const Vectorized<c10::complex<float>> &a,
|
| 890 |
+
const Vectorized<c10::complex<float>> &b) {
|
| 891 |
+
return _mm512_sub_ps(a, b);
|
| 892 |
+
}
|
| 893 |
+
|
| 894 |
+
template <> Vectorized<c10::complex<float>> inline operator*(const Vectorized<c10::complex<float>> &a,
|
| 895 |
+
const Vectorized<c10::complex<float>> &b) {
|
| 896 |
+
//(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
|
| 897 |
+
const __m512 sign_mask = _mm512_setr_ps(0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0,
|
| 898 |
+
0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0);
|
| 899 |
+
auto ac_bd = _mm512_mul_ps(a, b); //ac bd
|
| 900 |
+
|
| 901 |
+
auto d_c = _mm512_permute_ps(b, 0xB1); //d c
|
| 902 |
+
d_c = _mm512_xor_ps(sign_mask, d_c); //d -c
|
| 903 |
+
auto ad_bc = _mm512_mul_ps(a, d_c); //ad -bc
|
| 904 |
+
|
| 905 |
+
auto ret = Vectorized<c10::complex<float>>::hsub_ps(ac_bd, ad_bc); //ac - bd ad + bc
|
| 906 |
+
return ret;
|
| 907 |
+
}
|
| 908 |
+
|
| 909 |
+
template <> Vectorized<c10::complex<float>> inline operator/(const Vectorized<c10::complex<float>> &a,
|
| 910 |
+
const Vectorized<c10::complex<float>> &b) {
|
| 911 |
+
//re + im*i = (a + bi) / (c + di)
|
| 912 |
+
auto mask = _mm512_set1_ps(-0.f);
|
| 913 |
+
auto fabs_cd = _mm512_andnot_ps(mask, b); // |c| |d|
|
| 914 |
+
auto fabs_dc = _mm512_permute_ps(fabs_cd, 0xB1); // |d| |c|
|
| 915 |
+
auto scale = _mm512_rcp14_ps(_mm512_max_ps(fabs_cd, fabs_dc)); // 1/sc 1/sc
|
| 916 |
+
auto a2 = _mm512_mul_ps(a, scale); // a/sc b/sc
|
| 917 |
+
auto b2 = _mm512_mul_ps(b, scale); // c/sc d/sc
|
| 918 |
+
auto acbd2 = _mm512_mul_ps(a2, b2);
|
| 919 |
+
|
| 920 |
+
const __m512 sign_mask = _mm512_setr_ps(-0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0,
|
| 921 |
+
-0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0);
|
| 922 |
+
auto dc2 = _mm512_permute_ps(b2, 0xB1); // d/sc c/sc
|
| 923 |
+
dc2 = _mm512_xor_ps(sign_mask, dc2); // -d/|c,d| c/sc
|
| 924 |
+
auto adbc2 = _mm512_mul_ps(a2, dc2); //-ad/sc^2 bc/sc^2
|
| 925 |
+
auto res2 = Vectorized<c10::complex<float>>::hadd_ps(acbd2, adbc2); //(ac+bd)/sc^2 (bc-ad)/sc^2
|
| 926 |
+
|
| 927 |
+
// get the denominator
|
| 928 |
+
auto denom2 = Vectorized<c10::complex<float>>(b2).abs_2_(); // (c^2+d^2)/sc^2 (c^2+d^2)/sc^2
|
| 929 |
+
res2 = _mm512_div_ps(res2, denom2);
|
| 930 |
+
return res2;
|
| 931 |
+
}
|
| 932 |
+
|
| 933 |
+
// reciprocal. Implement this here so we can use multiplication.
|
| 934 |
+
inline Vectorized<c10::complex<float>> Vectorized<c10::complex<float>>::reciprocal() const {
|
| 935 |
+
//re + im*i = (a + bi) / (c + di)
|
| 936 |
+
//re = (ac + bd)/abs_2() = c/abs_2()
|
| 937 |
+
//im = (bc - ad)/abs_2() = d/abs_2()
|
| 938 |
+
const __m512 sign_mask = _mm512_setr_ps(0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0,
|
| 939 |
+
0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0);
|
| 940 |
+
auto c_d = _mm512_xor_ps(sign_mask, values); //c -d
|
| 941 |
+
return _mm512_div_ps(c_d, abs_2_());
|
| 942 |
+
}
|
| 943 |
+
|
| 944 |
+
inline Vectorized<c10::complex<float>> Vectorized<c10::complex<float>>::atan() const {
|
| 945 |
+
// atan(x) = i/2 * ln((i + z)/(i - z))
|
| 946 |
+
const __m512 i = _mm512_setr_ps(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0,
|
| 947 |
+
0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
|
| 948 |
+
const Vectorized i_half = _mm512_setr_ps(0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5,
|
| 949 |
+
0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5);
|
| 950 |
+
|
| 951 |
+
auto sum = Vectorized(_mm512_add_ps(i, values)); // a 1+b
|
| 952 |
+
auto sub = Vectorized(_mm512_sub_ps(i, values)); // -a 1-b
|
| 953 |
+
auto ln = (sum/sub).log(); // ln((i + z)/(i - z))
|
| 954 |
+
return i_half*ln; // i/2*ln()
|
| 955 |
+
}
|
| 956 |
+
|
| 957 |
+
template <>
|
| 958 |
+
Vectorized<c10::complex<float>> inline maximum(const Vectorized<c10::complex<float>>& a,
|
| 959 |
+
const Vectorized<c10::complex<float>>& b) {
|
| 960 |
+
auto zero_vector = _mm512_set1_epi32(0);
|
| 961 |
+
auto abs_a = a.abs_2_();
|
| 962 |
+
auto abs_b = b.abs_2_();
|
| 963 |
+
auto mask = _mm512_cmp_ps_mask(abs_a, abs_b, _CMP_LT_OQ);
|
| 964 |
+
auto max = _mm512_mask_blend_ps(mask, a, b);
|
| 965 |
+
// Exploit the fact that all-ones is a NaN.
|
| 966 |
+
auto isnan_mask = _mm512_cmp_ps_mask(abs_a, abs_b, _CMP_UNORD_Q);
|
| 967 |
+
auto isnan = _mm512_mask_set1_epi32(zero_vector, isnan_mask, 0xFFFFFFFF);
|
| 968 |
+
return _mm512_or_ps(max, _mm512_castsi512_ps(isnan));
|
| 969 |
+
}
|
| 970 |
+
|
| 971 |
+
template <>
|
| 972 |
+
Vectorized<c10::complex<float>> inline minimum(const Vectorized<c10::complex<float>>& a,
|
| 973 |
+
const Vectorized<c10::complex<float>>& b) {
|
| 974 |
+
auto zero_vector = _mm512_set1_epi32(0);
|
| 975 |
+
auto abs_a = a.abs_2_();
|
| 976 |
+
auto abs_b = b.abs_2_();
|
| 977 |
+
auto mask = _mm512_cmp_ps_mask(abs_a, abs_b, _CMP_GT_OQ);
|
| 978 |
+
auto min = _mm512_mask_blend_ps(mask, a, b);
|
| 979 |
+
// Exploit the fact that all-ones is a NaN.
|
| 980 |
+
auto isnan_mask = _mm512_cmp_ps_mask(abs_a, abs_b, _CMP_UNORD_Q);
|
| 981 |
+
auto isnan = _mm512_mask_set1_epi32(zero_vector, isnan_mask, 0xFFFFFFFF);
|
| 982 |
+
return _mm512_or_ps(min, _mm512_castsi512_ps(isnan));
|
| 983 |
+
}
|
| 984 |
+
|
| 985 |
+
template <>
|
| 986 |
+
Vectorized<c10::complex<float>> inline operator&(const Vectorized<c10::complex<float>>& a,
|
| 987 |
+
const Vectorized<c10::complex<float>>& b) {
|
| 988 |
+
return _mm512_and_ps(a, b);
|
| 989 |
+
}
|
| 990 |
+
|
| 991 |
+
template <>
|
| 992 |
+
Vectorized<c10::complex<float>> inline operator|(const Vectorized<c10::complex<float>>& a,
|
| 993 |
+
const Vectorized<c10::complex<float>>& b) {
|
| 994 |
+
return _mm512_or_ps(a, b);
|
| 995 |
+
}
|
| 996 |
+
|
| 997 |
+
template <>
|
| 998 |
+
Vectorized<c10::complex<float>> inline operator^(const Vectorized<c10::complex<float>>& a,
|
| 999 |
+
const Vectorized<c10::complex<float>>& b) {
|
| 1000 |
+
return _mm512_xor_ps(a, b);
|
| 1001 |
+
}
|
| 1002 |
+
|
| 1003 |
+
inline Vectorized<c10::complex<float>> Vectorized<c10::complex<float>>::eq(
|
| 1004 |
+
const Vectorized<c10::complex<float>>& other) const {
|
| 1005 |
+
auto eq = (*this == other); // compares real and imag individually
|
| 1006 |
+
// If both real numbers and imag numbers are equal, then the complex numbers are equal
|
| 1007 |
+
return (eq.real() & eq.imag()) & Vectorized<c10::complex<float>>(_mm512_set1_ps(1.0f));
|
| 1008 |
+
}
|
| 1009 |
+
|
| 1010 |
+
inline Vectorized<c10::complex<float>> Vectorized<c10::complex<float>>::ne(
|
| 1011 |
+
const Vectorized<c10::complex<float>>& other) const {
|
| 1012 |
+
auto ne = (*this != other); // compares real and imag individually
|
| 1013 |
+
// If either real numbers or imag numbers are not equal, then the complex numbers are not equal
|
| 1014 |
+
return (ne.real() | ne.imag()) & Vectorized<c10::complex<float>>(_mm512_set1_ps(1.0f));
|
| 1015 |
+
}
|
| 1016 |
+
|
| 1017 |
+
#endif
|
| 1018 |
+
|
| 1019 |
+
}}}
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec512/vec512_convert.h
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 4 |
+
#include <ATen/cpu/vec/vec512/vec512_bfloat16.h>
|
| 5 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 6 |
+
#include <ATen/cpu/vec/vec_convert.h>
|
| 7 |
+
|
| 8 |
+
namespace at::vec {
|
| 9 |
+
inline namespace CPU_CAPABILITY {
|
| 10 |
+
|
| 11 |
+
#if defined(CPU_CAPABILITY_AVX512) && !defined(_MSC_VER)
|
| 12 |
+
|
| 13 |
+
template <>
|
| 14 |
+
struct VecConvert<float, 1, BFloat16, 1> {
|
| 15 |
+
static inline VectorizedN<float, 1> apply(
|
| 16 |
+
const VectorizedN<BFloat16, 1>& src) {
|
| 17 |
+
VectorizedN<float, 1> result;
|
| 18 |
+
__m512 value;
|
| 19 |
+
cvtbf16_fp32(_mm512_castsi512_si256(src[0]), value);
|
| 20 |
+
result[0] = value;
|
| 21 |
+
return result;
|
| 22 |
+
}
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
template <>
|
| 26 |
+
struct VecConvert<float, 1, Half, 1> {
|
| 27 |
+
static inline VectorizedN<float, 1> apply(const VectorizedN<Half, 1>& src) {
|
| 28 |
+
VectorizedN<float, 1> result;
|
| 29 |
+
__m512 value;
|
| 30 |
+
cvtfp16_fp32(_mm512_castsi512_si256(src[0]), value);
|
| 31 |
+
result[0] = value;
|
| 32 |
+
return result;
|
| 33 |
+
}
|
| 34 |
+
};
|
| 35 |
+
|
| 36 |
+
template <>
|
| 37 |
+
struct VecConvert<BFloat16, 1, float, 1> {
|
| 38 |
+
static inline VectorizedN<BFloat16, 1> apply(
|
| 39 |
+
const VectorizedN<float, 1>& src) {
|
| 40 |
+
VectorizedN<BFloat16, 1> result;
|
| 41 |
+
result[0] = _mm512_castsi256_si512(cvtfp32_bf16(src[0]));
|
| 42 |
+
return result;
|
| 43 |
+
}
|
| 44 |
+
};
|
| 45 |
+
|
| 46 |
+
template <>
|
| 47 |
+
struct VecConvert<BFloat16, 1, float, 2> {
|
| 48 |
+
static inline VectorizedN<BFloat16, 1> apply(
|
| 49 |
+
const VectorizedN<float, 2>& src) {
|
| 50 |
+
VectorizedN<BFloat16, 1> result;
|
| 51 |
+
result[0] = convert_float_bfloat16(src[0], src[1]);
|
| 52 |
+
return result;
|
| 53 |
+
}
|
| 54 |
+
};
|
| 55 |
+
|
| 56 |
+
template <>
|
| 57 |
+
struct VecConvert<float, 2, BFloat16, 1> {
|
| 58 |
+
static inline VectorizedN<float, 2> apply(
|
| 59 |
+
const VectorizedN<BFloat16, 1>& src) {
|
| 60 |
+
VectorizedN<float, 2> result;
|
| 61 |
+
std::tie(result[0], result[1]) = convert_bfloat16_float(src[0]);
|
| 62 |
+
return result;
|
| 63 |
+
}
|
| 64 |
+
};
|
| 65 |
+
|
| 66 |
+
template <>
|
| 67 |
+
struct VecConvert<Half, 1, float, 1> {
|
| 68 |
+
static inline VectorizedN<Half, 1> apply(const VectorizedN<float, 1>& src) {
|
| 69 |
+
VectorizedN<Half, 1> result;
|
| 70 |
+
result[0] = _mm512_castsi256_si512(cvtfp32_fp16(src[0]));
|
| 71 |
+
return result;
|
| 72 |
+
}
|
| 73 |
+
};
|
| 74 |
+
|
| 75 |
+
template <>
|
| 76 |
+
struct VecConvert<Half, 1, float, 2> {
|
| 77 |
+
static inline VectorizedN<Half, 1> apply(const VectorizedN<float, 2>& src) {
|
| 78 |
+
VectorizedN<Half, 1> result;
|
| 79 |
+
result[0] = convert_float_half(src[0], src[1]);
|
| 80 |
+
return result;
|
| 81 |
+
}
|
| 82 |
+
};
|
| 83 |
+
|
| 84 |
+
template <>
|
| 85 |
+
struct VecConvert<float, 2, Half, 1> {
|
| 86 |
+
static inline VectorizedN<float, 2> apply(const VectorizedN<Half, 1>& src) {
|
| 87 |
+
VectorizedN<float, 2> result;
|
| 88 |
+
std::tie(result[0], result[1]) = convert_half_float(src[0]);
|
| 89 |
+
return result;
|
| 90 |
+
}
|
| 91 |
+
};
|
| 92 |
+
|
| 93 |
+
template <>
|
| 94 |
+
struct VecConvert<float, 1, int64_t, 2> {
|
| 95 |
+
static inline VectorizedN<float, 1> apply(
|
| 96 |
+
const VectorizedN<int64_t, 2>& src) {
|
| 97 |
+
auto low = _mm512_cvtepi64_ps(src[0]);
|
| 98 |
+
auto high = _mm512_cvtepi64_ps(src[1]);
|
| 99 |
+
return Vectorized<float>(
|
| 100 |
+
_mm512_insertf32x8(_mm512_castps256_ps512(low), high, 1));
|
| 101 |
+
}
|
| 102 |
+
};
|
| 103 |
+
|
| 104 |
+
template <>
|
| 105 |
+
struct VecConvert<int64_t, 2, float, 1> {
|
| 106 |
+
static inline VectorizedN<int64_t, 2> apply(
|
| 107 |
+
const VectorizedN<float, 1>& src) {
|
| 108 |
+
at::vec::VectorizedN<int64_t, 2> result;
|
| 109 |
+
result[0] = _mm512_cvt_roundps_epi64(
|
| 110 |
+
_mm512_castps512_ps256(src[0]), _MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC);
|
| 111 |
+
result[1] = _mm512_cvt_roundps_epi64(
|
| 112 |
+
_mm512_extractf32x8_ps(src[0], 1),
|
| 113 |
+
_MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC);
|
| 114 |
+
return result;
|
| 115 |
+
}
|
| 116 |
+
};
|
| 117 |
+
|
| 118 |
+
template <>
|
| 119 |
+
struct VecConvert<int32_t, 1, int64_t, 2> {
|
| 120 |
+
static inline VectorizedN<int32_t, 1> apply(
|
| 121 |
+
const VectorizedN<int64_t, 2>& src) {
|
| 122 |
+
auto low = _mm512_cvtepi64_epi32(src[0]);
|
| 123 |
+
auto high = _mm512_cvtepi64_epi32(src[1]);
|
| 124 |
+
return Vectorized<int32_t>(
|
| 125 |
+
_mm512_inserti32x8(_mm512_castsi256_si512(low), high, 1));
|
| 126 |
+
}
|
| 127 |
+
};
|
| 128 |
+
|
| 129 |
+
template <>
|
| 130 |
+
struct VecConvert<int64_t, 2, int32_t, 1> {
|
| 131 |
+
static inline VectorizedN<int64_t, 2> apply(
|
| 132 |
+
const VectorizedN<int32_t, 1>& src) {
|
| 133 |
+
at::vec::VectorizedN<int64_t, 2> result;
|
| 134 |
+
result[0] = _mm512_cvtepi32_epi64(_mm512_castsi512_si256(src[0]));
|
| 135 |
+
result[1] = _mm512_cvtepi32_epi64(_mm512_extracti32x8_epi32(src[0], 1));
|
| 136 |
+
return result;
|
| 137 |
+
}
|
| 138 |
+
};
|
| 139 |
+
|
| 140 |
+
template <>
|
| 141 |
+
struct VecConvert<int32_t, 1, int8_t, 1> {
|
| 142 |
+
static inline VectorizedN<int32_t, 1> apply(
|
| 143 |
+
const VectorizedN<int8_t, 1>& src) {
|
| 144 |
+
auto src128 = _mm512_castsi512_si128(src[0]);
|
| 145 |
+
return Vectorized<int32_t>(_mm512_cvtepi8_epi32(src128));
|
| 146 |
+
}
|
| 147 |
+
};
|
| 148 |
+
|
| 149 |
+
template <>
|
| 150 |
+
struct VecConvert<int32_t, 1, uint8_t, 1> {
|
| 151 |
+
static inline VectorizedN<int32_t, 1> apply(
|
| 152 |
+
const VectorizedN<uint8_t, 1>& src) {
|
| 153 |
+
auto src128 = _mm512_castsi512_si128(src[0]);
|
| 154 |
+
return Vectorized<int32_t>(_mm512_cvtepu8_epi32(src128));
|
| 155 |
+
}
|
| 156 |
+
};
|
| 157 |
+
|
| 158 |
+
template <>
|
| 159 |
+
struct VecConvert<int32_t, 1, float, 1> {
|
| 160 |
+
static inline VectorizedN<int32_t, 1> apply(
|
| 161 |
+
const VectorizedN<float, 1>& src) {
|
| 162 |
+
return Vectorized<int32_t>(_mm512_cvttps_epi32(src[0]));
|
| 163 |
+
}
|
| 164 |
+
};
|
| 165 |
+
|
| 166 |
+
template <>
|
| 167 |
+
struct VecConvert<float, 1, int32_t, 1> {
|
| 168 |
+
static inline VectorizedN<float, 1> apply(
|
| 169 |
+
const VectorizedN<int32_t, 1>& src) {
|
| 170 |
+
return Vectorized<float>(_mm512_cvtepi32_ps(src[0]));
|
| 171 |
+
}
|
| 172 |
+
};
|
| 173 |
+
|
| 174 |
+
template <>
|
| 175 |
+
struct VecConvert<int16_t, 1, uint8_t, 1> {
|
| 176 |
+
static inline VectorizedN<int16_t, 1> apply(
|
| 177 |
+
const VectorizedN<uint8_t, 1>& src) {
|
| 178 |
+
auto src256 = _mm512_castsi512_si256(src[0]);
|
| 179 |
+
return Vectorized<int16_t>(_mm512_cvtepu8_epi16(src256));
|
| 180 |
+
}
|
| 181 |
+
};
|
| 182 |
+
|
| 183 |
+
template <>
|
| 184 |
+
struct VecConvert<int8_t, 1, int32_t, 1> {
|
| 185 |
+
static inline VectorizedN<int8_t, 1> apply(
|
| 186 |
+
const VectorizedN<int32_t, 1>& src) {
|
| 187 |
+
auto src128 = _mm512_cvtepi32_epi8(src[0]);
|
| 188 |
+
return Vectorized<int8_t>(_mm512_castsi128_si512(src128));
|
| 189 |
+
}
|
| 190 |
+
};
|
| 191 |
+
|
| 192 |
+
template <>
|
| 193 |
+
struct VecConvert<int8_t, 1, int16_t, 1> {
|
| 194 |
+
static inline VectorizedN<int8_t, 1> apply(
|
| 195 |
+
const VectorizedN<int16_t, 1>& src) {
|
| 196 |
+
auto src256 = _mm512_cvtepi16_epi8(src[0]);
|
| 197 |
+
return Vectorized<int8_t>(_mm512_castsi256_si512(src256));
|
| 198 |
+
}
|
| 199 |
+
};
|
| 200 |
+
|
| 201 |
+
template <typename dst_t, typename src_t>
|
| 202 |
+
struct VecConvert<
|
| 203 |
+
dst_t,
|
| 204 |
+
1,
|
| 205 |
+
src_t,
|
| 206 |
+
1,
|
| 207 |
+
typename std::enable_if_t<
|
| 208 |
+
(is_reduced_floating_point_v<dst_t> && is_8bit_integer_v<src_t>) ||
|
| 209 |
+
(is_reduced_floating_point_v<src_t> && is_8bit_integer_v<dst_t>),
|
| 210 |
+
void>> {
|
| 211 |
+
static inline VectorizedN<dst_t, 1> apply(const VectorizedN<src_t, 1>& src) {
|
| 212 |
+
VectorizedN<float, 1> tmp_fp32 = VecConvert<float, 1, src_t, 1>::apply(src);
|
| 213 |
+
return VecConvert<dst_t, 1, float, 1>::apply(tmp_fp32);
|
| 214 |
+
}
|
| 215 |
+
};
|
| 216 |
+
|
| 217 |
+
template <typename dst_t>
|
| 218 |
+
struct VecConvert<
|
| 219 |
+
dst_t,
|
| 220 |
+
1,
|
| 221 |
+
float,
|
| 222 |
+
1,
|
| 223 |
+
typename std::enable_if_t<is_8bit_integer_v<dst_t>,
|
| 224 |
+
void>> {
|
| 225 |
+
static inline VectorizedN<dst_t, 1> apply(const VectorizedN<float, 1>& src) {
|
| 226 |
+
return convert_float_to_int8<dst_t>(src[0]);
|
| 227 |
+
}
|
| 228 |
+
};
|
| 229 |
+
|
| 230 |
+
template <typename src_t>
|
| 231 |
+
struct VecConvert<
|
| 232 |
+
float,
|
| 233 |
+
1,
|
| 234 |
+
src_t,
|
| 235 |
+
1,
|
| 236 |
+
typename std::enable_if_t<is_8bit_integer_v<src_t>,
|
| 237 |
+
void>> {
|
| 238 |
+
static inline VectorizedN<float, 1> apply(const VectorizedN<src_t, 1>& src) {
|
| 239 |
+
return convert_int8_to_float<src_t>(src[0]);
|
| 240 |
+
}
|
| 241 |
+
};
|
| 242 |
+
|
| 243 |
+
template <typename dst_t>
|
| 244 |
+
struct VecConvert<
|
| 245 |
+
dst_t,
|
| 246 |
+
1,
|
| 247 |
+
int64_t,
|
| 248 |
+
2,
|
| 249 |
+
typename std::enable_if<
|
| 250 |
+
std::is_same_v<dst_t, int8_t> ||
|
| 251 |
+
std::is_same_v<dst_t, uint8_t>>::type> {
|
| 252 |
+
static inline VectorizedN<dst_t, 1> apply(
|
| 253 |
+
const VectorizedN<int64_t, 2>& src) {
|
| 254 |
+
return VecConvert<dst_t, 1, int32_t, 1>::apply(
|
| 255 |
+
VecConvert<int32_t, 1, int64_t, 2>::apply(src));
|
| 256 |
+
}
|
| 257 |
+
};
|
| 258 |
+
|
| 259 |
+
#endif
|
| 260 |
+
|
| 261 |
+
} // namespace CPU_CAPABILITY
|
| 262 |
+
} // namespace at::vec
|
vllm/lib/python3.10/site-packages/torch/include/ATen/cpu/vec/vec512/vec512_double.h
ADDED
|
@@ -0,0 +1,472 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
+
#pragma once
|
| 2 |
+
|
| 3 |
+
// DO NOT DEFINE STATIC DATA IN THIS HEADER!
|
| 4 |
+
// See Note [Do not compile initializers with AVX]
|
| 5 |
+
|
| 6 |
+
#include <ATen/cpu/vec/intrinsics.h>
|
| 7 |
+
#include <ATen/cpu/vec/vec_base.h>
|
| 8 |
+
#include <c10/util/irange.h>
|
| 9 |
+
#if (defined(CPU_CAPABILITY_AVX512))
|
| 10 |
+
#define SLEEF_STATIC_LIBS
|
| 11 |
+
#include <sleef.h>
|
| 12 |
+
#endif
|
| 13 |
+
|
| 14 |
+
namespace at {
|
| 15 |
+
namespace vec {
|
| 16 |
+
// See Note [CPU_CAPABILITY namespace]
|
| 17 |
+
inline namespace CPU_CAPABILITY {
|
| 18 |
+
|
| 19 |
+
#if defined(CPU_CAPABILITY_AVX512)
|
| 20 |
+
|
| 21 |
+
template <> class Vectorized<double> {
|
| 22 |
+
private:
|
| 23 |
+
static constexpr __m512i zero_vector {0, 0, 0, 0, 0, 0, 0, 0};
|
| 24 |
+
public:
|
| 25 |
+
// values needs to be public for compilation with clang
|
| 26 |
+
// as vec512.h uses it
|
| 27 |
+
__m512d values;
|
| 28 |
+
using value_type = double;
|
| 29 |
+
using size_type = int;
|
| 30 |
+
static constexpr size_type size() {
|
| 31 |
+
return 8;
|
| 32 |
+
}
|
| 33 |
+
Vectorized() {}
|
| 34 |
+
Vectorized(__m512d v) : values(v) {}
|
| 35 |
+
Vectorized(double val) {
|
| 36 |
+
values = _mm512_set1_pd(val);
|
| 37 |
+
}
|
| 38 |
+
Vectorized(double val1, double val2, double val3, double val4,
|
| 39 |
+
double val5, double val6, double val7, double val8) {
|
| 40 |
+
values = _mm512_setr_pd(val1, val2, val3, val4, val5, val6, val7, val8);
|
| 41 |
+
}
|
| 42 |
+
operator __m512d() const {
|
| 43 |
+
return values;
|
| 44 |
+
}
|
| 45 |
+
template <int64_t mask>
|
| 46 |
+
static Vectorized<double> blend(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 47 |
+
return _mm512_mask_blend_pd(mask, a.values, b.values);
|
| 48 |
+
}
|
| 49 |
+
static Vectorized<double> blendv(const Vectorized<double>& a, const Vectorized<double>& b,
|
| 50 |
+
const Vectorized<double>& mask) {
|
| 51 |
+
auto all_ones = _mm512_set1_epi64(0xFFFFFFFFFFFFFFFF);
|
| 52 |
+
auto mmask = _mm512_cmp_epi64_mask(_mm512_castpd_si512(mask.values), all_ones, _MM_CMPINT_EQ);
|
| 53 |
+
return _mm512_mask_blend_pd(mmask, a.values, b.values);
|
| 54 |
+
}
|
| 55 |
+
template<typename step_t>
|
| 56 |
+
static Vectorized<double> arange(double base = 0., step_t step = static_cast<step_t>(1)) {
|
| 57 |
+
return Vectorized<double>(base, base + step, base + 2 * step, base + 3 * step,
|
| 58 |
+
base + 4 * step, base + 5 * step, base + 6 * step,
|
| 59 |
+
base + 7 * step);
|
| 60 |
+
}
|
| 61 |
+
static Vectorized<double> set(const Vectorized<double>& a, const Vectorized<double>& b,
|
| 62 |
+
int64_t count = size()) {
|
| 63 |
+
switch (count) {
|
| 64 |
+
case 0:
|
| 65 |
+
return a;
|
| 66 |
+
case 1:
|
| 67 |
+
return blend<1>(a, b);
|
| 68 |
+
case 2:
|
| 69 |
+
return blend<3>(a, b);
|
| 70 |
+
case 3:
|
| 71 |
+
return blend<7>(a, b);
|
| 72 |
+
case 4:
|
| 73 |
+
return blend<15>(a, b);
|
| 74 |
+
case 5:
|
| 75 |
+
return blend<31>(a, b);
|
| 76 |
+
case 6:
|
| 77 |
+
return blend<63>(a, b);
|
| 78 |
+
case 7:
|
| 79 |
+
return blend<127>(a, b);
|
| 80 |
+
}
|
| 81 |
+
return b;
|
| 82 |
+
}
|
| 83 |
+
static Vectorized<double> loadu(const void* ptr, int64_t count = size()) {
|
| 84 |
+
if (count == size())
|
| 85 |
+
return _mm512_loadu_pd(reinterpret_cast<const double*>(ptr));
|
| 86 |
+
|
| 87 |
+
__mmask8 mask = (1ULL << count) - 1;
|
| 88 |
+
return _mm512_maskz_loadu_pd(mask, ptr);
|
| 89 |
+
}
|
| 90 |
+
void store(void* ptr, int count = size()) const {
|
| 91 |
+
if (count == size()) {
|
| 92 |
+
_mm512_storeu_pd(reinterpret_cast<double*>(ptr), values);
|
| 93 |
+
} else if (count > 0) {
|
| 94 |
+
__mmask8 mask = (1ULL << count) - 1;
|
| 95 |
+
_mm512_mask_storeu_pd(reinterpret_cast<double*>(ptr), mask, values);
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
const double& operator[](int idx) const = delete;
|
| 99 |
+
double& operator[](int idx) = delete;
|
| 100 |
+
int zero_mask() const {
|
| 101 |
+
// returns an integer mask where all zero elements are translated to 1-bit and others are translated to 0-bit
|
| 102 |
+
__mmask8 cmp = _mm512_cmp_pd_mask(values, _mm512_set1_pd(0.0), _CMP_EQ_OQ);
|
| 103 |
+
return static_cast<int32_t>(cmp);
|
| 104 |
+
}
|
| 105 |
+
Vectorized<double> isnan() const {
|
| 106 |
+
auto cmp_mask = _mm512_cmp_pd_mask(values, _mm512_set1_pd(0.0), _CMP_UNORD_Q);
|
| 107 |
+
return _mm512_castsi512_pd(_mm512_mask_set1_epi64(zero_vector, cmp_mask,
|
| 108 |
+
0xFFFFFFFFFFFFFFFF));
|
| 109 |
+
}
|
| 110 |
+
bool has_inf_nan() const {
|
| 111 |
+
__m512d self_sub = _mm512_sub_pd(values, values);
|
| 112 |
+
return (_mm512_movepi8_mask(_mm512_castpd_si512(self_sub)) & 0x7777777777777777) != 0;
|
| 113 |
+
}
|
| 114 |
+
Vectorized<double> map(double (*const f)(double)) const {
|
| 115 |
+
__at_align__ double tmp[size()];
|
| 116 |
+
store(tmp);
|
| 117 |
+
for (const auto i : c10::irange(size())) {
|
| 118 |
+
tmp[i] = f(tmp[i]);
|
| 119 |
+
}
|
| 120 |
+
return loadu(tmp);
|
| 121 |
+
}
|
| 122 |
+
Vectorized<double> abs() const {
|
| 123 |
+
auto mask = _mm512_set1_pd(-0.f);
|
| 124 |
+
return _mm512_andnot_pd(mask, values);
|
| 125 |
+
}
|
| 126 |
+
Vectorized<double> angle() const {
|
| 127 |
+
const auto zero_vec = _mm512_castsi512_pd(zero_vector);
|
| 128 |
+
const auto nan_vec = _mm512_set1_pd(NAN);
|
| 129 |
+
const auto not_nan_mask = _mm512_cmp_pd_mask(values, values, _CMP_EQ_OQ);
|
| 130 |
+
const auto not_nan = _mm512_mask_set1_epi64(zero_vector, not_nan_mask,
|
| 131 |
+
0xFFFFFFFFFFFFFFFF);
|
| 132 |
+
const auto nan_mask = _mm512_cmp_pd_mask(_mm512_castsi512_pd(not_nan),
|
| 133 |
+
zero_vec, _CMP_EQ_OQ);
|
| 134 |
+
const auto pi = _mm512_set1_pd(c10::pi<double>);
|
| 135 |
+
|
| 136 |
+
const auto neg_mask = _mm512_cmp_pd_mask(values, zero_vec, _CMP_LT_OQ);
|
| 137 |
+
auto angle = _mm512_mask_blend_pd(neg_mask, zero_vec, pi);
|
| 138 |
+
angle = _mm512_mask_blend_pd(nan_mask, angle, nan_vec);
|
| 139 |
+
return angle;
|
| 140 |
+
}
|
| 141 |
+
Vectorized<double> real() const {
|
| 142 |
+
return *this;
|
| 143 |
+
}
|
| 144 |
+
Vectorized<double> imag() const {
|
| 145 |
+
return _mm512_set1_pd(0);
|
| 146 |
+
}
|
| 147 |
+
Vectorized<double> conj() const {
|
| 148 |
+
return *this;
|
| 149 |
+
}
|
| 150 |
+
Vectorized<double> acos() const {
|
| 151 |
+
return Vectorized<double>(Sleef_acosd8_u10(values));
|
| 152 |
+
}
|
| 153 |
+
Vectorized<double> acosh() const {
|
| 154 |
+
return Vectorized<double>(Sleef_acoshd8_u10(values));
|
| 155 |
+
}
|
| 156 |
+
Vectorized<double> asin() const {
|
| 157 |
+
return Vectorized<double>(Sleef_asind8_u10(values));
|
| 158 |
+
}
|
| 159 |
+
Vectorized<double> atan() const {
|
| 160 |
+
return Vectorized<double>(Sleef_atand8_u10(values));
|
| 161 |
+
}
|
| 162 |
+
Vectorized<double> atanh() const {
|
| 163 |
+
return Vectorized<double>(Sleef_atanhd8_u10(values));
|
| 164 |
+
}
|
| 165 |
+
Vectorized<double> atan2(const Vectorized<double> &b) const {
|
| 166 |
+
return Vectorized<double>(Sleef_atan2d8_u10(values, b));
|
| 167 |
+
}
|
| 168 |
+
Vectorized<double> copysign(const Vectorized<double> &sign) const {
|
| 169 |
+
return Vectorized<double>(Sleef_copysignd8(values, sign));
|
| 170 |
+
}
|
| 171 |
+
Vectorized<double> erf() const {
|
| 172 |
+
return Vectorized<double>(Sleef_erfd8_u10(values));
|
| 173 |
+
}
|
| 174 |
+
Vectorized<double> erfc() const {
|
| 175 |
+
return Vectorized<double>(Sleef_erfcd8_u15(values));
|
| 176 |
+
}
|
| 177 |
+
Vectorized<double> erfinv() const {
|
| 178 |
+
return map(calc_erfinv);
|
| 179 |
+
}
|
| 180 |
+
Vectorized<double> exp() const {
|
| 181 |
+
return Vectorized<double>(Sleef_expd8_u10(values));
|
| 182 |
+
}
|
| 183 |
+
Vectorized<double> exp2() const {
|
| 184 |
+
return Vectorized<double>(Sleef_exp2d8_u10(values));
|
| 185 |
+
}
|
| 186 |
+
Vectorized<double> expm1() const {
|
| 187 |
+
return Vectorized<double>(Sleef_expm1d8_u10(values));
|
| 188 |
+
}
|
| 189 |
+
Vectorized<double> exp_u20() const {
|
| 190 |
+
return exp();
|
| 191 |
+
}
|
| 192 |
+
Vectorized<double> fmod(const Vectorized<double>& q) const {
|
| 193 |
+
return Vectorized<double>(Sleef_fmodd8(values, q));
|
| 194 |
+
}
|
| 195 |
+
Vectorized<double> hypot(const Vectorized<double> &b) const {
|
| 196 |
+
return Vectorized<double>(Sleef_hypotd8_u05(values, b));
|
| 197 |
+
}
|
| 198 |
+
Vectorized<double> i0() const {
|
| 199 |
+
return map(calc_i0);
|
| 200 |
+
}
|
| 201 |
+
Vectorized<double> i0e() const {
|
| 202 |
+
return map(calc_i0e);
|
| 203 |
+
}
|
| 204 |
+
Vectorized<double> digamma() const {
|
| 205 |
+
return map(calc_digamma);
|
| 206 |
+
}
|
| 207 |
+
Vectorized<double> igamma(const Vectorized<double> &x) const {
|
| 208 |
+
__at_align__ double tmp[size()];
|
| 209 |
+
__at_align__ double tmp_x[size()];
|
| 210 |
+
store(tmp);
|
| 211 |
+
x.store(tmp_x);
|
| 212 |
+
for (const auto i : c10::irange(size())) {
|
| 213 |
+
tmp[i] = calc_igamma(tmp[i], tmp_x[i]);
|
| 214 |
+
}
|
| 215 |
+
return loadu(tmp);
|
| 216 |
+
}
|
| 217 |
+
Vectorized<double> igammac(const Vectorized<double> &x) const {
|
| 218 |
+
__at_align__ double tmp[size()];
|
| 219 |
+
__at_align__ double tmp_x[size()];
|
| 220 |
+
store(tmp);
|
| 221 |
+
x.store(tmp_x);
|
| 222 |
+
for (const auto i : c10::irange(size())) {
|
| 223 |
+
tmp[i] = calc_igammac(tmp[i], tmp_x[i]);
|
| 224 |
+
}
|
| 225 |
+
return loadu(tmp);
|
| 226 |
+
}
|
| 227 |
+
Vectorized<double> log() const {
|
| 228 |
+
return Vectorized<double>(Sleef_logd8_u10(values));
|
| 229 |
+
}
|
| 230 |
+
Vectorized<double> log2() const {
|
| 231 |
+
return Vectorized<double>(Sleef_log2d8_u10(values));
|
| 232 |
+
}
|
| 233 |
+
Vectorized<double> log10() const {
|
| 234 |
+
return Vectorized<double>(Sleef_log10d8_u10(values));
|
| 235 |
+
}
|
| 236 |
+
Vectorized<double> log1p() const {
|
| 237 |
+
return Vectorized<double>(Sleef_log1pd8_u10(values));
|
| 238 |
+
}
|
| 239 |
+
Vectorized<double> sin() const {
|
| 240 |
+
return Vectorized<double>(Sleef_sind8_u10(values));
|
| 241 |
+
}
|
| 242 |
+
Vectorized<double> sinh() const {
|
| 243 |
+
return Vectorized<double>(Sleef_sinhd8_u10(values));
|
| 244 |
+
}
|
| 245 |
+
Vectorized<double> cos() const {
|
| 246 |
+
return Vectorized<double>(Sleef_cosd8_u10(values));
|
| 247 |
+
}
|
| 248 |
+
Vectorized<double> cosh() const {
|
| 249 |
+
return Vectorized<double>(Sleef_coshd8_u10(values));
|
| 250 |
+
}
|
| 251 |
+
Vectorized<double> ceil() const {
|
| 252 |
+
return _mm512_ceil_pd(values);
|
| 253 |
+
}
|
| 254 |
+
Vectorized<double> floor() const {
|
| 255 |
+
return _mm512_floor_pd(values);
|
| 256 |
+
}
|
| 257 |
+
Vectorized<double> frac() const;
|
| 258 |
+
Vectorized<double> neg() const {
|
| 259 |
+
return _mm512_xor_pd(_mm512_set1_pd(-0.), values);
|
| 260 |
+
}
|
| 261 |
+
Vectorized<double> nextafter(const Vectorized<double> &b) const {
|
| 262 |
+
return Vectorized<double>(Sleef_nextafterd8(values, b));
|
| 263 |
+
}
|
| 264 |
+
Vectorized<double> round() const {
|
| 265 |
+
return _mm512_roundscale_pd(values, (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
|
| 266 |
+
}
|
| 267 |
+
Vectorized<double> tan() const {
|
| 268 |
+
return Vectorized<double>(Sleef_tand8_u10(values));
|
| 269 |
+
}
|
| 270 |
+
Vectorized<double> tanh() const {
|
| 271 |
+
return Vectorized<double>(Sleef_tanhd8_u10(values));
|
| 272 |
+
}
|
| 273 |
+
Vectorized<double> trunc() const {
|
| 274 |
+
return _mm512_roundscale_pd(values, (_MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC));
|
| 275 |
+
}
|
| 276 |
+
Vectorized<double> lgamma() const {
|
| 277 |
+
return Vectorized<double>(Sleef_lgammad8_u10(values));
|
| 278 |
+
}
|
| 279 |
+
Vectorized<double> sqrt() const {
|
| 280 |
+
return _mm512_sqrt_pd(values);
|
| 281 |
+
}
|
| 282 |
+
Vectorized<double> reciprocal() const {
|
| 283 |
+
return _mm512_div_pd(_mm512_set1_pd(1), values);
|
| 284 |
+
}
|
| 285 |
+
Vectorized<double> rsqrt() const {
|
| 286 |
+
return _mm512_div_pd(_mm512_set1_pd(1), _mm512_sqrt_pd(values));
|
| 287 |
+
}
|
| 288 |
+
Vectorized<double> pow(const Vectorized<double> &b) const {
|
| 289 |
+
return Vectorized<double>(Sleef_powd8_u10(values, b));
|
| 290 |
+
}
|
| 291 |
+
// Comparison using the _CMP_**_OQ predicate.
|
| 292 |
+
// `O`: get false if an operand is NaN
|
| 293 |
+
// `Q`: do not raise if an operand is NaN
|
| 294 |
+
Vectorized<double> operator==(const Vectorized<double>& other) const {
|
| 295 |
+
auto cmp_mask = _mm512_cmp_pd_mask(values, other.values, _CMP_EQ_OQ);
|
| 296 |
+
return _mm512_castsi512_pd(_mm512_mask_set1_epi64(zero_vector, cmp_mask,
|
| 297 |
+
0xFFFFFFFFFFFFFFFF));
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
Vectorized<double> operator!=(const Vectorized<double>& other) const {
|
| 301 |
+
auto cmp_mask = _mm512_cmp_pd_mask(values, other.values, _CMP_NEQ_UQ);
|
| 302 |
+
return _mm512_castsi512_pd(_mm512_mask_set1_epi64(zero_vector, cmp_mask,
|
| 303 |
+
0xFFFFFFFFFFFFFFFF));
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
Vectorized<double> operator<(const Vectorized<double>& other) const {
|
| 307 |
+
auto cmp_mask = _mm512_cmp_pd_mask(values, other.values, _CMP_LT_OQ);
|
| 308 |
+
return _mm512_castsi512_pd(_mm512_mask_set1_epi64(zero_vector, cmp_mask,
|
| 309 |
+
0xFFFFFFFFFFFFFFFF));
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
Vectorized<double> operator<=(const Vectorized<double>& other) const {
|
| 313 |
+
auto cmp_mask = _mm512_cmp_pd_mask(values, other.values, _CMP_LE_OQ);
|
| 314 |
+
return _mm512_castsi512_pd(_mm512_mask_set1_epi64(zero_vector, cmp_mask,
|
| 315 |
+
0xFFFFFFFFFFFFFFFF));
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
Vectorized<double> operator>(const Vectorized<double>& other) const {
|
| 319 |
+
auto cmp_mask = _mm512_cmp_pd_mask(values, other.values, _CMP_GT_OQ);
|
| 320 |
+
return _mm512_castsi512_pd(_mm512_mask_set1_epi64(zero_vector, cmp_mask,
|
| 321 |
+
0xFFFFFFFFFFFFFFFF));
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
Vectorized<double> operator>=(const Vectorized<double>& other) const {
|
| 325 |
+
auto cmp_mask = _mm512_cmp_pd_mask(values, other.values, _CMP_GE_OQ);
|
| 326 |
+
return _mm512_castsi512_pd(_mm512_mask_set1_epi64(zero_vector, cmp_mask,
|
| 327 |
+
0xFFFFFFFFFFFFFFFF));
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
Vectorized<double> eq(const Vectorized<double>& other) const;
|
| 331 |
+
Vectorized<double> ne(const Vectorized<double>& other) const;
|
| 332 |
+
Vectorized<double> lt(const Vectorized<double>& other) const;
|
| 333 |
+
Vectorized<double> le(const Vectorized<double>& other) const;
|
| 334 |
+
Vectorized<double> gt(const Vectorized<double>& other) const;
|
| 335 |
+
Vectorized<double> ge(const Vectorized<double>& other) const;
|
| 336 |
+
};
|
| 337 |
+
|
| 338 |
+
template <>
|
| 339 |
+
Vectorized<double> inline operator+(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 340 |
+
return _mm512_add_pd(a, b);
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
template <>
|
| 344 |
+
Vectorized<double> inline operator-(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 345 |
+
return _mm512_sub_pd(a, b);
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
template <>
|
| 349 |
+
Vectorized<double> inline operator*(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 350 |
+
return _mm512_mul_pd(a, b);
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
template <>
|
| 354 |
+
Vectorized<double> inline operator/(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 355 |
+
return _mm512_div_pd(a, b);
|
| 356 |
+
}
|
| 357 |
+
|
| 358 |
+
// frac. Implement this here so we can use subtraction.
|
| 359 |
+
inline Vectorized<double> Vectorized<double>::frac() const {
|
| 360 |
+
return *this - this->trunc();
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
// Implements the IEEE 754 201X `maximum` operation, which propagates NaN if
|
| 364 |
+
// either input is a NaN.
|
| 365 |
+
template <>
|
| 366 |
+
Vectorized<double> inline maximum(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 367 |
+
auto zero_vec = _mm512_set1_epi64(0);
|
| 368 |
+
Vectorized<double> max = _mm512_max_pd(a, b);
|
| 369 |
+
auto isnan_mask = _mm512_cmp_pd_mask(a, b, _CMP_UNORD_Q);
|
| 370 |
+
auto isnan = _mm512_castsi512_pd(_mm512_mask_set1_epi64(zero_vec, isnan_mask,
|
| 371 |
+
0xFFFFFFFFFFFFFFFF));
|
| 372 |
+
// Exploit the fact that all-ones is a NaN.
|
| 373 |
+
return _mm512_or_pd(max, isnan);
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
// Implements the IEEE 754 201X `minimum` operation, which propagates NaN if
|
| 377 |
+
// either input is a NaN.
|
| 378 |
+
template <>
|
| 379 |
+
Vectorized<double> inline minimum(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 380 |
+
auto zero_vec = _mm512_set1_epi64(0);
|
| 381 |
+
Vectorized<double> min = _mm512_min_pd(a, b);
|
| 382 |
+
auto isnan_mask = _mm512_cmp_pd_mask(a, b, _CMP_UNORD_Q);
|
| 383 |
+
auto isnan = _mm512_castsi512_pd(_mm512_mask_set1_epi64(zero_vec, isnan_mask,
|
| 384 |
+
0xFFFFFFFFFFFFFFFF));
|
| 385 |
+
// Exploit the fact that all-ones is a NaN.
|
| 386 |
+
return _mm512_or_pd(min, isnan);
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
template <>
|
| 390 |
+
Vectorized<double> inline clamp(const Vectorized<double>& a, const Vectorized<double>& min, const Vectorized<double>& max) {
|
| 391 |
+
return _mm512_min_pd(max, _mm512_max_pd(min, a));
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
template <>
|
| 395 |
+
Vectorized<double> inline clamp_min(const Vectorized<double>& a, const Vectorized<double>& min) {
|
| 396 |
+
return _mm512_max_pd(min, a);
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
template <>
|
| 400 |
+
Vectorized<double> inline clamp_max(const Vectorized<double>& a, const Vectorized<double>& max) {
|
| 401 |
+
return _mm512_min_pd(max, a);
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
template <>
|
| 405 |
+
Vectorized<double> inline operator&(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 406 |
+
return _mm512_and_pd(a, b);
|
| 407 |
+
}
|
| 408 |
+
|
| 409 |
+
template <>
|
| 410 |
+
Vectorized<double> inline operator|(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 411 |
+
return _mm512_or_pd(a, b);
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
template <>
|
| 415 |
+
Vectorized<double> inline operator^(const Vectorized<double>& a, const Vectorized<double>& b) {
|
| 416 |
+
return _mm512_xor_pd(a, b);
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
inline Vectorized<double> Vectorized<double>::eq(const Vectorized<double>& other) const {
|
| 420 |
+
return (*this == other) & Vectorized<double>(1.0);
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
inline Vectorized<double> Vectorized<double>::ne(const Vectorized<double>& other) const {
|
| 424 |
+
return (*this != other) & Vectorized<double>(1.0);
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
inline Vectorized<double> Vectorized<double>::gt(const Vectorized<double>& other) const {
|
| 428 |
+
return (*this > other) & Vectorized<double>(1.0);
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
inline Vectorized<double> Vectorized<double>::ge(const Vectorized<double>& other) const {
|
| 432 |
+
return (*this >= other) & Vectorized<double>(1.0);
|
| 433 |
+
}
|
| 434 |
+
|
| 435 |
+
inline Vectorized<double> Vectorized<double>::lt(const Vectorized<double>& other) const {
|
| 436 |
+
return (*this < other) & Vectorized<double>(1.0);
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
inline Vectorized<double> Vectorized<double>::le(const Vectorized<double>& other) const {
|
| 440 |
+
return (*this <= other) & Vectorized<double>(1.0);
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
template <>
|
| 444 |
+
inline void convert(const double* src, double* dst, int64_t n) {
|
| 445 |
+
int64_t i;
|
| 446 |
+
#ifndef __msvc_cl__
|
| 447 |
+
#pragma unroll
|
| 448 |
+
#endif
|
| 449 |
+
for (i = 0; i <= (n - Vectorized<double>::size()); i += Vectorized<double>::size()) {
|
| 450 |
+
_mm512_storeu_pd(dst + i, _mm512_loadu_pd(src + i));
|
| 451 |
+
}
|
| 452 |
+
#ifndef __msvc_cl__
|
| 453 |
+
#pragma unroll
|
| 454 |
+
#endif
|
| 455 |
+
for (; i < n; i++) {
|
| 456 |
+
dst[i] = src[i];
|
| 457 |
+
}
|
| 458 |
+
}
|
| 459 |
+
|
| 460 |
+
template <>
|
| 461 |
+
Vectorized<double> inline fmadd(const Vectorized<double>& a, const Vectorized<double>& b, const Vectorized<double>& c) {
|
| 462 |
+
return _mm512_fmadd_pd(a, b, c);
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
template <>
|
| 466 |
+
Vectorized<double> inline fmsub(const Vectorized<double>& a, const Vectorized<double>& b, const Vectorized<double>& c) {
|
| 467 |
+
return _mm512_fmsub_pd(a, b, c);
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
#endif
|
| 471 |
+
|
| 472 |
+
}}}
|