xref: /aosp_15_r20/external/tensorflow/tensorflow/core/kernels/data/model_dataset_op.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 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_CORE_KERNELS_DATA_MODEL_DATASET_OP_H_
16 #define TENSORFLOW_CORE_KERNELS_DATA_MODEL_DATASET_OP_H_
17 
18 #include "tensorflow/core/platform/platform.h"
19 
20 // On mobile we do not provide model dataset op because not all of its
21 // dependencies are available there. The op is replaced with a no-op.
22 #if !defined(IS_MOBILE_PLATFORM)
23 #include "tensorflow/core/framework/dataset.h"
24 #include "tensorflow/core/framework/model.h"
25 
26 namespace tensorflow {
27 namespace data {
28 
29 class ModelDatasetOp : public UnaryDatasetOpKernel {
30  public:
31   static constexpr const char* const kDatasetType = "ModelDataset";
32   static constexpr const char* const kDatasetOp = "ModelDatasetOp";
33   static constexpr const char* const kAlgorithm = "algorithm";
34   static constexpr const char* const kCpuBudget = "cpu_budget";
35   static constexpr const char* const kRamBudget = "ram_budget";
36 
37   // Executes the logic of the ModelDatasetOp directly (as opposed to through
38   // executing the ModelDatasetOp op kernel).
39   static void MakeDatasetFromOptions(OpKernelContext* ctx, DatasetBase* input,
40                                      model::AutotuneAlgorithm algorithm,
41                                      int64_t cpu_budget, int64_t ram_budget,
42                                      DatasetBase** output);
43 
44   explicit ModelDatasetOp(OpKernelConstruction* ctx);
45 
46  protected:
47   void MakeDataset(OpKernelContext* ctx, DatasetBase* input,
48                    DatasetBase** output) override;
49 
50  private:
51   class Dataset;
52 
53   model::AutotuneAlgorithm algorithm_;
54   int64_t cpu_budget_;
55   int64_t ram_budget_;
56 };
57 
58 }  // namespace data
59 }  // namespace tensorflow
60 #else  // !IS_MOBILE_PLATFORM
61 #include "tensorflow/core/framework/dataset.h"
62 
63 namespace tensorflow {
64 namespace data {
65 
66 class ModelDatasetOp : public UnaryDatasetOpKernel {
67  public:
68   // Creates and returns a ModelDatasetOp::Dataset in output, given the
69   // input, algorithm, cpu_budget and ram_budget parameters. This method is used
70   // to create the dataset without explicitly using the ModelDatasetOp.
71   static void MakeDatasetFromOptions(OpKernelContext* ctx, DatasetBase* input,
72                                      model::AutotuneAlgorithm algorithm,
73                                      bool cpu_budget, bool ram_budget,
74                                      DatasetBase** output);
75 
76   explicit ModelDatasetOp(OpKernelConstruction* ctx);
77 
78  protected:
79   void MakeDataset(OpKernelContext* ctx, DatasetBase* input,
80                    DatasetBase** output) override;
81 };
82 
83 }  // namespace data
84 }  // namespace tensorflow
85 #endif  // !IS_MOBILE_PLATFORM
86 
87 #endif  // TENSORFLOW_CORE_KERNELS_DATA_MODEL_DATASET_OP_H_
88