1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef FCP_AGGREGATION_CORE_TENSOR_AGGREGATOR_H_ 18 #define FCP_AGGREGATION_CORE_TENSOR_AGGREGATOR_H_ 19 20 #include <vector> 21 22 #include "fcp/aggregation/core/aggregator.h" 23 #include "fcp/aggregation/core/input_tensor_list.h" 24 #include "fcp/aggregation/core/tensor.h" 25 #include "fcp/base/monitoring.h" 26 27 namespace fcp { 28 namespace aggregation { 29 30 using OutputTensorList = std::vector<Tensor>; 31 32 // TensorAggregator is a base class for implementing Aggregation intrinsics 33 // with Tensor being an input and output type for the aggregation. 34 class TensorAggregator 35 : public Aggregator<InputTensorList, OutputTensorList, TensorAggregator> { 36 public: 37 ~TensorAggregator() override = default; 38 39 // Implementation of the base Aggregator class methods. 40 Status Accumulate(InputTensorList tensors) override; 41 bool CanReport() const override; 42 StatusOr<OutputTensorList> Report() && override; 43 44 // Returns the number of aggregated inputs. 45 virtual int GetNumInputs() const = 0; 46 47 protected: 48 // Construct TensorAggregator TensorAggregator()49 explicit TensorAggregator() {} 50 51 // The actual implementation of the tensor aggregation to be provided by 52 // a derived class. 53 virtual Status AggregateTensors(InputTensorList tensors) = 0; 54 55 // Checks if the current TensorAggregator is valid e.g. the resulting output 56 // hasn't been consumed. 57 virtual Status CheckValid() const = 0; 58 59 // Consumes the output of this TensorAggregator. 60 virtual OutputTensorList TakeOutputs() && = 0; 61 62 private: 63 // Extracts the aggregated tensor and makes the current aggregator "consumed". 64 OutputTensorList TakeTensors() &&; 65 }; 66 67 } // namespace aggregation 68 } // namespace fcp 69 70 #endif // FCP_AGGREGATION_CORE_TENSOR_AGGREGATOR_H_ 71