|
| 1 | +#include <ATen/ATen.h> |
| 2 | +#include <ATen/native/TensorFactories.h> |
| 3 | +#include <ATen/quantized/QTensorImpl.h> |
| 4 | +#include <ATen/quantized/Quantizer.h> |
| 5 | +#include <c10/core/QScheme.h> |
| 6 | +#include <c10/core/TensorOptions.h> |
| 7 | +#include <c10/util/accumulate.h> |
| 8 | +#include <torch/custom_class.h> |
| 9 | +#include <torch/custom_class_detail.h> |
| 10 | + |
| 11 | +#include <oneapi/dnnl/dnnl.hpp> |
| 12 | +#include <quantized/QUtils.h> |
| 13 | + |
| 14 | +#ifdef BUILD_JIT_QUANTIZATION_SAVE |
| 15 | +// Following code is not in any namespace. This is due to |
| 16 | +// we align to PyTorch side. If any code is need added in this |
| 17 | +// file except packedparam serialization, please write it in a |
| 18 | +// proper namespace. |
| 19 | +// QConv prepack pickling method hacking |
| 20 | +template <int kSpatialDim = 2> |
| 21 | +torch::class_<ConvPackedParamsBase<kSpatialDim>> register_conv_params(); |
| 22 | + |
| 23 | +extern template torch::class_<ConvPackedParamsBase<2>> register_conv_params< |
| 24 | + 2>(); |
| 25 | +extern template torch::class_<ConvPackedParamsBase<3>> register_conv_params< |
| 26 | + 3>(); |
| 27 | + |
| 28 | +template <int kSpatialDim = 2> |
| 29 | +ConvParamsSerializationTypeV2 serialize_conv( |
| 30 | + const c10::intrusive_ptr<ConvPackedParamsBase<kSpatialDim>>& params); |
| 31 | +extern template ConvParamsSerializationTypeV2 serialize_conv( |
| 32 | + const c10::intrusive_ptr<ConvPackedParamsBase<2>>& params); |
| 33 | +extern template ConvParamsSerializationTypeV2 serialize_conv( |
| 34 | + const c10::intrusive_ptr<ConvPackedParamsBase<3>>& params); |
| 35 | + |
| 36 | +template <uint32_t kSpatialDim> |
| 37 | +ConvParamsSerializationTypeV3 parse_conv_serialized_state(c10::IValue v); |
| 38 | + |
| 39 | +template <int kSpatialDim> |
| 40 | +int redefine_prepack() { |
| 41 | + auto conv_prepack_class = register_conv_params<kSpatialDim>(); |
| 42 | + auto clsptr = torch::getCustomClass( |
| 43 | + "__torch__.torch.classes.quantized.Conv" + c10::to_string(kSpatialDim) + |
| 44 | + "dPackedParamsBase"); |
| 45 | + clsptr->unsafeRemoveMethod("__getstate__"); |
| 46 | + clsptr->unsafeRemoveMethod("__setstate__"); |
| 47 | + conv_prepack_class.def_pickle( |
| 48 | + [](const c10::intrusive_ptr<ConvPackedParamsBase<kSpatialDim>>& params) |
| 49 | + -> ConvParamsSerializationType { // __getstate__ |
| 50 | + return serialize_conv<kSpatialDim>(params); |
| 51 | + }, |
| 52 | + // __setstate__ takes c10::IValue because we support parsing historical |
| 53 | + // serialization versions. |
| 54 | + [](c10::IValue v) -> c10::intrusive_ptr< |
| 55 | + ConvPackedParamsBase<kSpatialDim>> { // __setstate__ |
| 56 | + ConvParamsSerializationTypeV3 state = |
| 57 | + parse_conv_serialized_state<kSpatialDim>(v); |
| 58 | + return deserialize_conv_dpcpp<kSpatialDim>(state); |
| 59 | + }); |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | +template int redefine_prepack<2>(); |
| 64 | +template int redefine_prepack<3>(); |
| 65 | + |
| 66 | +// QLinear prepack pickling method hacking |
| 67 | +torch::class_<LinearPackedParamsBase> register_linear_params(); |
| 68 | + |
| 69 | +int redefine_linear_prepack() { |
| 70 | + auto linear_prepack_class = register_linear_params(); |
| 71 | + auto clsptr = torch::getCustomClass( |
| 72 | + "__torch__.torch.classes.quantized.LinearPackedParamsBase"); |
| 73 | + clsptr->unsafeRemoveMethod("__getstate__"); |
| 74 | + clsptr->unsafeRemoveMethod("__setstate__"); |
| 75 | + using SerializationType = std::tuple<at::Tensor, c10::optional<at::Tensor>>; |
| 76 | + linear_prepack_class.def_pickle( |
| 77 | + [](const c10::intrusive_ptr<LinearPackedParamsBase>& params) |
| 78 | + -> SerializationType { // __getstate__ |
| 79 | + at::Tensor weight; |
| 80 | + c10::optional<at::Tensor> bias; |
| 81 | + std::tie(weight, bias) = params->unpack(); |
| 82 | + return std::make_tuple(std::move(weight), std::move(bias)); |
| 83 | + }, |
| 84 | + [](SerializationType state) |
| 85 | + -> c10::intrusive_ptr<LinearPackedParamsBase> { // __setstate__ |
| 86 | + at::Tensor weight; |
| 87 | + c10::optional<at::Tensor> bias; |
| 88 | + weight = std::move(std::get<0>(state)); |
| 89 | + bias = std::move(std::get<1>(state)); |
| 90 | + |
| 91 | + return at::AtenIpexTypeQuantizedXPU::PackedLinearWeightQDPCPP::prepack( |
| 92 | + std::move(weight), std::move(bias)); |
| 93 | + }); |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +namespace { |
| 98 | +static auto conv2d_params = redefine_prepack<2>(); |
| 99 | +static auto conv3d_params = redefine_prepack<3>(); |
| 100 | +static auto linear_params = redefine_linear_prepack(); |
| 101 | +} // namespace |
| 102 | +#endif |
0 commit comments