| 1 |
/******************************************************************************* |
| 2 |
* |
| 3 |
* MIT License |
| 4 |
* |
| 5 |
* Copyright (C) 2022-2025 Advanced Micro Devices, Inc. All rights reserved. |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 |
* of this software and associated documentation files (the "Software"), to deal |
| 9 |
* in the Software without restriction, including without limitation the rights |
| 10 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
* copies of the Software, and to permit persons to whom the Software is |
| 12 |
* furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included in |
| 15 |
* all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
* SOFTWARE. |
| 24 |
* |
| 25 |
*******************************************************************************/ |
| 26 |
|
| 27 |
#pragma once |
| 28 |
|
| 29 |
#include <Tensile/ContractionLibrary.hpp> |
| 30 |
#include <Tensile/Serialization.hpp> |
| 31 |
|
| 32 |
#include <llvm/ObjectYAML/YAML.h> |
| 33 |
|
| 34 |
#include <cstddef> |
| 35 |
|
| 36 |
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(rocisa::DataType) |
| 37 |
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(TensileLite::ActivationType) |
| 38 |
|
| 39 |
namespace llvm |
| 40 |
{ |
| 41 |
namespace yaml |
| 42 |
{ |
| 43 |
namespace sn = TensileLite::Serialization; |
| 44 |
|
| 45 |
template <typename T> |
| 46 |
struct Hide |
| 47 |
{ |
| 48 |
T& _value; |
| 49 |
|
| 50 |
Hide(T& value) |
| 51 |
: _value(value) |
| 52 |
{ |
| 53 |
} |
| 54 |
|
| 55 |
T& operator*() |
| 56 |
{ |
| 57 |
return _value; |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
template <typename T> |
| 62 |
struct has_SequenceTraits<Hide<T>> |
| 63 |
{ |
| 64 |
const static bool value = sn::has_SequenceTraits<T, IO>::value; |
| 65 |
}; |
| 66 |
|
| 67 |
template <typename T> |
| 68 |
struct has_ScalarEnumerationTraits<Hide<T>> |
| 69 |
{ |
| 70 |
const static bool value = sn::has_EnumTraits<T, IO>::value; |
| 71 |
}; |
| 72 |
|
| 73 |
template <typename T> |
| 74 |
struct has_MappingTraits<Hide<T>, EmptyContext> |
| 75 |
{ |
| 76 |
const static bool value = sn::has_EmptyMappingTraits<T, IO>::value; |
| 77 |
}; |
| 78 |
|
| 79 |
template <typename T> |
| 80 |
struct has_CustomMappingTraits<Hide<T>> |
| 81 |
{ |
| 82 |
const static bool value = sn::has_CustomMappingTraits<T, IO>::value; |
| 83 |
}; |
| 84 |
|
| 85 |
template <typename T> |
| 86 |
struct missingTraits<T, EmptyContext> |
| 87 |
: public std::integral_constant< |
| 88 |
bool, |
| 89 |
!has_ScalarEnumerationTraits<T>::value && !has_ScalarBitSetTraits<T>::value |
| 90 |
&& !has_ScalarTraits<T>::value && !has_BlockScalarTraits<T>::value |
| 91 |
&& !has_MappingTraits<T, EmptyContext>::value && !has_SequenceTraits<T>::value |
| 92 |
&& !has_CustomMappingTraits<T>::value && !has_DocumentListTraits<T>::value |
| 93 |
&& !sn::has_SerializationTraits<T, IO>::value> |
| 94 |
{ |
| 95 |
}; |
| 96 |
|
| 97 |
template <typename T> |
| 98 |
typename std::enable_if<sn::has_SerializationTraits<T, IO>::value, void>::type |
| 99 |
yamlize(IO& io, T& Val, bool b, EmptyContext& ctx) |
| 100 |
{ |
| 101 |
Hide<T> hide(Val); |
| 102 |
|
| 103 |
yamlize(io, hide, b, ctx); |
| 104 |
} |
| 105 |
|
| 106 |
template <typename T> |
| 107 |
typename std::enable_if<sn::has_SerializationTraits<T, IO>::value, Input&>::type |
| 108 |
operator>>(Input& input, T& Val) |
| 109 |
{ |
| 110 |
Hide<T> hide(Val); |
| 111 |
|
| 112 |
return input >> hide; |
| 113 |
} |
| 114 |
|
| 115 |
template <typename T> |
| 116 |
typename std::enable_if<sn::has_SerializationTraits<T, IO>::value, Output&>::type |
| 117 |
operator<<(Output& output, T& Val) |
| 118 |
{ |
| 119 |
Hide<T> hide(Val); |
| 120 |
|
| 121 |
return output << hide; |
| 122 |
} |
| 123 |
} // namespace yaml |
| 124 |
} // namespace llvm |
| 125 |
|
| 126 |
namespace TensileLite |
| 127 |
{ |
| 128 |
namespace Serialization |
| 129 |
{ |
| 130 |
template <> |
| 131 |
struct IOTraits<llvm::yaml::IO> |
| 132 |
{ |
| 133 |
using IO = llvm::yaml::IO; |
| 134 |
|
| 135 |
template <typename T> |
| 136 |
static void mapRequired(IO& io, const char* key, T& obj) |
| 137 |
{ |
| 138 |
io.mapRequired(key, obj); |
| 139 |
} |
| 140 |
|
| 141 |
template <typename T, typename Context> |
| 142 |
static void mapRequired(IO& io, const char* key, T& obj, Context& ctx) |
| 143 |
{ |
| 144 |
io.mapRequired(key, obj, ctx); |
| 145 |
} |
| 146 |
|
| 147 |
template <typename T> |
| 148 |
static void mapOptional(IO& io, const char* key, T& obj) |
| 149 |
{ |
| 150 |
io.mapOptional(key, obj); |
| 151 |
} |
| 152 |
|
| 153 |
template <typename T, typename Context> |
| 154 |
static void mapOptional(IO& io, const char* key, T& obj, Context& ctx) |
| 155 |
{ |
| 156 |
io.mapOptional(key, obj, ctx); |
| 157 |
} |
| 158 |
|
| 159 |
static bool outputting(IO& io) |
| 160 |
{ |
| 161 |
return io.outputting(); |
| 162 |
} |
| 163 |
|
| 164 |
static void setError(IO& io, std::string const& msg) |
| 165 |
{ |
| 166 |
// throw std::runtime_error(msg); |
| 167 |
return io.setError(msg); |
| 168 |
} |
| 169 |
|
| 170 |
static void setContext(IO& io, void* ctx) |
| 171 |
{ |
| 172 |
io.setContext(ctx); |
| 173 |
} |
| 174 |
|
| 175 |
static void* getContext(IO& io) |
| 176 |
{ |
| 177 |
return io.getContext(); |
| 178 |
} |
| 179 |
|
| 180 |
template <typename T> |
| 181 |
static void enumCase(IO& io, T& member, const char* key, T value) |
| 182 |
{ |
| 183 |
io.enumCase(member, key, value); |
| 184 |
} |
| 185 |
}; |
| 186 |
|
| 187 |
} // namespace Serialization |
| 188 |
} // namespace TensileLite |
| 189 |
|
| 190 |
namespace llvm |
| 191 |
{ |
| 192 |
namespace yaml |
| 193 |
{ |
| 194 |
LLVM_YAML_STRONG_TYPEDEF(size_t, FooType); |
| 195 |
|
| 196 |
using mysize_t |
| 197 |
= std::conditional<std::is_same<size_t, uint64_t>::value, FooType, size_t>::type; |
| 198 |
|
| 199 |
template <> |
| 200 |
struct ScalarTraits<mysize_t> |
| 201 |
{ |
| 202 |
static void output(const mysize_t& value, void* ctx, raw_ostream& stream) |
| 203 |
{ |
| 204 |
uint64_t tmp = value; |
| 205 |
ScalarTraits<uint64_t>::output(tmp, ctx, stream); |
| 206 |
} |
| 207 |
|
| 208 |
static StringRef input(StringRef str, void* ctx, mysize_t& value) |
| 209 |
{ |
| 210 |
uint64_t tmp; |
| 211 |
auto rv = ScalarTraits<uint64_t>::input(str, ctx, tmp); |
| 212 |
value = tmp; |
| 213 |
return rv; |
| 214 |
} |
| 215 |
|
| 216 |
static bool mustQuote(StringRef) |
| 217 |
{ |
| 218 |
return false; |
| 219 |
} |
| 220 |
}; |
| 221 |
|
| 222 |
template <typename T> |
| 223 |
struct MappingTraits<Hide<T>> |
| 224 |
{ |
| 225 |
static void mapping(IO& io, Hide<T>& value) |
| 226 |
{ |
| 227 |
sn::MappingTraits<T, IO>::mapping(io, *value); |
| 228 |
} |
| 229 |
|
| 230 |
static const bool flow = sn::MappingTraits<T, IO>::flow; |
| 231 |
}; |
| 232 |
|
| 233 |
template <typename T> |
| 234 |
struct SequenceTraits<Hide<T>> |
| 235 |
{ |
| 236 |
using Impl = sn::SequenceTraits<T, IO>; |
| 237 |
using Value = typename Impl::Value; |
| 238 |
|
| 239 |
static size_t size(IO& io, Hide<T>& t) |
| 240 |
{ |
| 241 |
return Impl::size(io, *t); |
| 242 |
} |
| 243 |
static Value& element(IO& io, Hide<T>& t, size_t index) |
| 244 |
{ |
| 245 |
return Impl::element(io, *t, index); |
| 246 |
} |
| 247 |
|
| 248 |
static const bool flow = Impl::flow; |
| 249 |
}; |
| 250 |
|
| 251 |
template <typename T> |
| 252 |
struct ScalarEnumerationTraits<Hide<T>> |
| 253 |
{ |
| 254 |
static void enumeration(IO& io, Hide<T>& value) |
| 255 |
{ |
| 256 |
sn::EnumTraits<T, IO>::enumeration(io, *value); |
| 257 |
} |
| 258 |
}; |
| 259 |
|
| 260 |
template <typename T> |
| 261 |
struct CustomMappingTraits<Hide<T>> |
| 262 |
{ |
| 263 |
using Impl = sn::CustomMappingTraits<T, IO>; |
| 264 |
|
| 265 |
static void inputOne(IO& io, StringRef key, Hide<T>& value) |
| 266 |
{ |
| 267 |
Impl::inputOne(io, key.str(), *value); |
| 268 |
} |
| 269 |
|
| 270 |
static void output(IO& io, Hide<T>& value) |
| 271 |
{ |
| 272 |
Impl::output(io, *value); |
| 273 |
} |
| 274 |
}; |
| 275 |
|
| 276 |
template <> |
| 277 |
struct MappingTraits<std::shared_ptr<TensileLite::MasterContractionLibrary>> |
| 278 |
{ |
| 279 |
using obj = TensileLite::MasterContractionLibrary; |
| 280 |
|
| 281 |
static void mapping(IO& io, std::shared_ptr<obj>& o) |
| 282 |
{ |
| 283 |
sn::PointerMappingTraits<obj, IO>::mapping(io, o); |
| 284 |
} |
| 285 |
}; |
| 286 |
|
| 287 |
static_assert( |
| 288 |
sn::has_EmptyMappingTraits< |
| 289 |
std::shared_ptr<TensileLite::SolutionLibrary<TensileLite::ContractionProblemGemm>>, |
| 290 |
IO>::value, |
| 291 |
"asdf2"); |
| 292 |
|
| 293 |
static_assert( |
| 294 |
sn::has_SerializationTraits< |
| 295 |
std::shared_ptr<TensileLite::SolutionLibrary<TensileLite::ContractionProblemGemm>>, |
| 296 |
IO>::value, |
| 297 |
"asdf"); |
| 298 |
|
| 299 |
static_assert( |
| 300 |
!has_SequenceTraits<Hide<std::shared_ptr< |
| 301 |
TensileLite::SolutionLibrary<TensileLite::ContractionProblemGemm>>>>::value, |
| 302 |
"fdsa"); |
| 303 |
static_assert(has_MappingTraits<Hide<std::shared_ptr<TensileLite::SolutionLibrary< |
| 304 |
TensileLite::ContractionProblemGemm>>>, |
| 305 |
EmptyContext>::value, |
| 306 |
"fdsa"); |
| 307 |
|
| 308 |
static_assert( |
| 309 |
!missingTraits<Hide<std::shared_ptr< |
| 310 |
TensileLite::SolutionLibrary<TensileLite::ContractionProblemGemm>>>, |
| 311 |
EmptyContext>::value, |
| 312 |
"fdsa"); |
| 313 |
} // namespace yaml |
| 314 |
} // namespace llvm |