File size: 8,200 Bytes
d1d4335 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
// Metal helper functions
#pragma once
#include <c10/metal/common.h>
#include <metal_stdlib>
namespace c10 {
namespace metal {
namespace detail {
template <typename T>
struct vectypes {};
template <>
struct vectypes<float> {
using type4 = float4;
using type3 = float3;
using type2 = float2;
};
template <>
struct vectypes<half> {
using type4 = half4;
using type3 = half3;
using type2 = half2;
};
#if __METAL_VERSION__ >= 310
template <>
struct vectypes<bfloat> {
using type4 = bfloat4;
using type3 = bfloat3;
using type2 = bfloat2;
};
#endif
template <>
struct vectypes<short> {
using type4 = short4;
using type3 = short3;
using type2 = short2;
};
template <>
struct vectypes<int> {
using type4 = int4;
using type3 = int3;
using type2 = int2;
};
template <>
struct vectypes<long> {
using type4 = short4;
using type3 = short3;
using type2 = short2;
};
template <typename T>
struct OpMathType {
using type = T;
};
template <>
struct OpMathType<half> {
using type = float;
};
template <>
struct OpMathType<short> {
using type = int;
};
template <>
struct OpMathType<char> {
using type = int;
};
template <>
struct OpMathType<uchar> {
using type = int;
};
#if __METAL_VERSION__ >= 310
template <>
struct OpMathType<bfloat> {
using type = float;
};
#endif
// Type promotion structure for higher precision accumulation
template <typename T>
struct AccumulationType {
using type = T;
};
// Specialization for half - promote to float for accumulation
template <>
struct AccumulationType<half> {
using type = float;
};
#if __METAL_VERSION__ >= 310
// Specialization for bfloat - promote to float for accumulation
template <>
struct AccumulationType<bfloat> {
using type = float;
};
#endif
} // namespace detail
template <typename T>
::metal::enable_if_t<::metal::is_floating_point_v<T>, T> max(T a, T b) {
return ::metal::isunordered(a, b) ? NAN : ::metal::max(a, b);
}
template <typename T, typename U>
::metal::enable_if_t<::metal::is_integral_v<T>&& ::metal::is_integral_v<U>, T>
max(T a, U b) {
return ::metal::max(a, static_cast<T>(b));
}
template <typename T>
::metal::enable_if_t<::metal::is_floating_point_v<T>, T> min(T a, T b) {
return ::metal::isunordered(a, b) ? NAN : ::metal::min(a, b);
}
template <typename T, typename U>
::metal::enable_if_t<::metal::is_integral_v<T>&& ::metal::is_integral_v<U>, T>
min(T a, U b) {
return ::metal::min(a, static_cast<T>(b));
}
#if __METAL_VERSION__ >= 310
template <>
inline bfloat min(bfloat a, bfloat b) {
return bfloat(
::metal::isunordered(a, b) ? NAN : ::metal::min(float(a), float(b)));
}
template <>
inline bfloat max(bfloat a, bfloat b) {
return bfloat(
::metal::isunordered(a, b) ? NAN : ::metal::max(float(a), float(b)));
}
#endif
template <typename T>
using vec2type_t = typename detail::vectypes<T>::type2;
template <typename T>
using vec4type_t = typename detail::vectypes<T>::type4;
template <typename T>
using opmath_t = typename detail::OpMathType<T>::type;
template <typename T>
using accum_t = typename detail::AccumulationType<T>::type;
// TODO: Move it to type_traits header may be
template <typename F, typename... Args>
using result_of = decltype(::metal::declval<F>()(::metal::declval<Args>()...));
template <typename T>
constexpr constant bool is_complex_v =
::metal::is_same_v<T, float2> || ::metal::is_same_v<T, half2>;
template <typename T>
constexpr constant bool is_scalar_floating_point_v =
::metal::is_floating_point_v<T> && ::metal::is_scalar_v<T>;
template <typename T>
constexpr constant bool is_scalar_integral_v =
::metal::is_integral_v<T> && ::metal::is_scalar_v<T>;
template <typename U, typename V>
using common_dtype = decltype(U(0) + V(0));
// floor_divide
template <
typename T,
typename U,
::metal::enable_if_t<
is_scalar_integral_v<T> && is_scalar_integral_v<U>,
bool> = true>
inline common_dtype<T, U> floor_divide(T x, U y) {
const auto quot = x / y;
return (x < 0) == (y < 0) ? quot : (x % y != 0) ? quot - 1 : quot;
}
template <
typename T,
typename U,
::metal::enable_if_t<
is_scalar_floating_point_v<T> && is_scalar_floating_point_v<U>,
bool> = true>
inline common_dtype<T, U> floor_divide(T x, U y) {
return ::metal::floor(x / y);
}
// fmod
template <
typename T,
typename U,
::metal::enable_if_t<
is_scalar_integral_v<T> && is_scalar_integral_v<U>,
bool> = true>
inline common_dtype<T, U> fmod(T x, U y) {
return x % y;
}
template <
typename T,
typename U,
::metal::enable_if_t<
is_scalar_floating_point_v<T> && is_scalar_floating_point_v<U>,
bool> = true>
inline common_dtype<T, U> fmod(T x, U y) {
return ::metal::fmod(x, y);
}
// cast_to primitives
// - No-op if types as the same
template <
typename T,
typename U,
::metal::enable_if_t<::metal::is_same_v<U, T>, bool> = true>
inline T cast_to(const U from) {
return from;
}
// - Simple cast between scalar and complex dtypes
template <
typename T,
typename U,
::metal::enable_if_t<
!::metal::is_same_v<U, T> && (is_complex_v<T> == is_complex_v<U>),
bool> = true>
inline T cast_to(const U from) {
return static_cast<T>(from);
}
// - Scalar to complex
template <
typename T,
typename U,
::metal::enable_if_t<is_complex_v<T> && !is_complex_v<U>, bool> = true>
inline T cast_to(const U from) {
return T(float(from), 0.0);
}
// - Complex to scalar (should not really be used, but exists for compliteness)
template <
typename T,
typename U,
::metal::enable_if_t<!is_complex_v<T> && is_complex_v<U>, bool> = true>
inline T cast_to(const U from) {
return static_cast<T>(from.x);
}
// Generalizable math operators (used for both scalar and complex)
template <
typename T,
typename U,
::metal::enable_if_t<!is_complex_v<T>, bool> = true>
inline common_dtype<T, U> mul(const T x, const U y) {
return x * y;
}
template <
typename T,
typename U,
::metal::enable_if_t<is_complex_v<T> && is_complex_v<U>, bool> = true>
inline common_dtype<T, U> mul(const T x, const U y) {
return T(x.x * y.x - x.y * y.y, x.x * y.y + x.y * y.x);
}
template <
typename T,
typename U,
::metal::enable_if_t<!is_complex_v<T>, bool> = true>
inline common_dtype<T, U> div(const T x, const U y) {
return x / y;
}
template <
typename T,
typename U,
::metal::enable_if_t<is_complex_v<T> && is_complex_v<U>, bool> = true>
inline common_dtype<T, U> div(const T x, const U y) {
return T(::metal::dot(x, y), x.y * y.x - x.x * y.y) / ::metal::dot(y, y);
}
// Remainder operator
template <
typename T,
typename U,
::metal::enable_if_t<
is_scalar_floating_point_v<T> || is_scalar_floating_point_v<U>,
bool> = true>
inline float remainder(const T x, const U y) {
const auto x_f = static_cast<float>(x);
const auto y_f = static_cast<float>(y);
return x_f - y_f * floor_divide(x_f, y_f);
}
template <
typename T,
typename U,
::metal::enable_if_t<
is_scalar_integral_v<T> && is_scalar_integral_v<U>,
bool> = true>
inline common_dtype<T, U> remainder(const T x, const U y) {
auto rc = x % y;
return rc == 0 || (x ^ y) > 0 ? rc : rc + y;
}
// Based on algorithm described in
// https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html#1202
inline float log1p(float x) {
const auto xp1 = 1.0f + x;
// First two elements of Taylor series for log(1+x) in Horner's form are:
// log(1+x) = x * (1 - x * (.5 ...)), but if 1 + x == x, then it's just x
if (xp1 == 1.0f) {
return x;
}
auto rc = ::metal::precise::log(xp1);
if (x > -.5 && x < .5) {
// Order of operations is important here for higher precision
rc *= x / (xp1 - 1.0f);
}
return rc;
}
} // namespace metal
} // namespace c10
|