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 #ifndef TENSORFLOW_LITE_EXPERIMENTAL_DELEGATES_COREML_BUILDERS_CONVOLUTION_OP_BUILDER_H_ 16 #define TENSORFLOW_LITE_EXPERIMENTAL_DELEGATES_COREML_BUILDERS_CONVOLUTION_OP_BUILDER_H_ 17 18 #include "tensorflow/lite/builtin_ops.h" 19 #include "tensorflow/lite/c/builtin_op_data.h" 20 #include "tensorflow/lite/c/common.h" 21 #include "tensorflow/lite/delegates/coreml/builders/op_builder.h" 22 23 namespace tflite { 24 namespace delegates { 25 namespace coreml { 26 27 enum class ConvolutionType { kConv, kDepthwiseConv, kTransposeConv }; 28 29 // Layer that provides convolution and depthwise convolution. 30 class ConvolutionOpBuilder : public OpBuilder { 31 public: ConvolutionOpBuilder(GraphBuilder * graph_builder,ConvolutionType conv_type)32 explicit ConvolutionOpBuilder(GraphBuilder* graph_builder, 33 ConvolutionType conv_type) 34 : OpBuilder(graph_builder), conv_type_(conv_type) {} 35 36 const std::string& DebugName() override; 37 38 CoreML::Specification::NeuralNetworkLayer* Build() override; 39 40 TfLiteStatus PopulateSubgraph(TfLiteContext* context) override; 41 42 void SetOutputChannels(uint64_t output_channels); 43 44 void SetNGroups(uint64_t n_groups); 45 46 void SetWeights(TfLiteTensor* weights); 47 48 void SetBias(TfLiteTensor* bias); 49 50 void SetOutputShape(TfLiteTensor* output_shape); 51 52 void SetParams(void* builtin_data); 53 54 TfLiteStatus RegisterInputs(const TfLiteIntArray* inputs, 55 TfLiteContext* context) override; 56 57 TfLiteStatus RegisterOutputs(const TfLiteIntArray* outputs, 58 TfLiteContext* context) override; 59 60 private: 61 void FillCoreMLWeights(); 62 void FillCoreMLBias(); 63 64 // Transpose TFLite kernel weights to CoreML kernel weights. 65 // Should be called after setting CoreML's kernel shapes. 66 void TransposeKernelWeights(); 67 68 uint64_t output_channels_; 69 uint64_t n_groups_ = 1; 70 71 ConvolutionType conv_type_; 72 73 // using default dilation_factor (1, 1) 74 // CoreML ConvolutionLayerParams.isDeconvolution == false 75 TfLiteTensor* weights_ = nullptr; 76 TfLiteTensor* bias_ = nullptr; 77 // Only used for TransposeConv. 78 TfLiteTensor* output_shape_ = nullptr; 79 }; 80 81 } // namespace coreml 82 } // namespace delegates 83 } // namespace tflite 84 85 #endif // TENSORFLOW_LITE_EXPERIMENTAL_DELEGATES_COREML_BUILDERS_CONVOLUTION_OP_BUILDER_H_ 86