1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_COMMON_CONVERT_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_CONVERT_H_ 18 19 #include <stdint.h> 20 21 #include <vector> 22 23 #include "absl/types/span.h" 24 #include "tensorflow/lite/delegates/gpu/common/data_type.h" 25 #include "tensorflow/lite/delegates/gpu/common/shape.h" 26 #include "tensorflow/lite/delegates/gpu/common/status.h" 27 #include "tensorflow/lite/delegates/gpu/common/tensor.h" 28 #include "tensorflow/lite/delegates/gpu/common/types.h" 29 30 namespace tflite { 31 namespace gpu { 32 33 // PHWC4 layout is where channels are grouped by 4 in a row and P stands for 34 // a plane that was derived by dividing channels by 4. 35 absl::Status ConvertToPHWC4(absl::Span<const float> in, const BHWC& shape, 36 absl::Span<float> out); 37 absl::Status ConvertToPHWC4Half(absl::Span<const float> in, const BHWC& shape, 38 absl::Span<HalfBits> out); 39 40 // @return number of elements when shape is converted into PHWC4. 41 uint32_t GetElementsSizeForPHWC4(const BHWC& shape); 42 43 // Operation is opposite to ConvertToPHWC4. 44 absl::Status ConvertFromPHWC4(absl::Span<const float> in, const BHWC& shape, 45 absl::Span<float> out); 46 absl::Status ConvertFromPHWC4Half(absl::Span<const HalfBits> in, 47 const BHWC& shape, absl::Span<float> out); 48 49 // Convenience wrapper around a method above. 50 std::vector<float> ConvertToPHWC4( 51 const Tensor<BHWC, DataType::FLOAT32>& tensor); 52 std::vector<float> ConvertToPHWC4(const Tensor<HWC, DataType::FLOAT32>& tensor); 53 54 // @return number of elements when shape is converted into PIOHW4. 55 uint32_t GetElementsSizeForPIOHW4(const OHWI& shape); 56 57 // PIOHW4 layout re-arranges weights in groups by 4, where outer dimension is 58 // P which is OxI/4. 59 absl::Status ConvertToPIOHW4(absl::Span<const float> in, const OHWI& shape, 60 absl::Span<float> out); 61 62 // Convenience wrapper around a method above. 63 std::vector<float> ConvertToPIOHW4( 64 const Tensor<OHWI, DataType::FLOAT32>& tensor); 65 66 // @return number of elements when shape is converted into PHWO4I4. 67 uint32_t GetElementsSizeForPHWO4I4(const OHWI& shape); 68 69 // Convenience wrapper around a method above. 70 std::vector<float> ConvertToPHWO4I4( 71 const Tensor<OHWI, DataType::FLOAT32>& tensor); 72 73 // Convenience wrapper around a method above, for Transposed Convolution. 74 std::vector<float> ConvertToPHWO4I4Transposed( 75 const Tensor<OHWI, DataType::FLOAT32>& tensor); 76 77 // @return (x,y,z) size for PHWO4I4 to access elements where each element 78 // consists of 4 values. 79 uint3 Get3DSizeForPHWO4I4(const OHWI& shape); 80 81 // @return number of elements when shape is converted into PHWO4I4. 82 uint32_t GetElementsSizeForPHWO4I4(const IHWO& shape); 83 84 // Layout is Po,H,W,OI4x4. 85 absl::Status ConvertToPHWO4I4(absl::Span<const float> in, const IHWO& shape, 86 absl::Span<float> out); 87 88 // Convenience wrapper around a method above. 89 std::vector<float> ConvertToPHWO4I4( 90 const Tensor<IHWO, DataType::FLOAT32>& tensor); 91 92 } // namespace gpu 93 } // namespace tflite 94 95 #endif // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_CONVERT_H_ 96