xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/tensorflow/utils/device_util.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_COMPILER_MLIR_TENSORFLOW_UTILS_DEVICE_UTIL_H_
17 #define TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_DEVICE_UTIL_H_
18 
19 #include "llvm/ADT/SmallVector.h"
20 #include "mlir/IR/Location.h"  // from @llvm-project
21 #include "mlir/IR/Operation.h"  // from @llvm-project
22 #include "mlir/Support/LogicalResult.h"  // from @llvm-project
23 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_structs.h"
24 #include "tensorflow/core/common_runtime/device_set.h"
25 #include "tensorflow/core/util/device_name_utils.h"
26 
27 namespace tensorflow {
28 
29 // Collects all devices known to the system by name and adds them as a
30 // `tf.devices` dictionary attribute with a full device name as a key, and
31 // device metadata as a value.
32 //
33 // Device names added in full parsed device form:
34 //   /job:<name>/replica:<replica>/task:<task>/device:<type>:<device_num>
35 //
36 // Supported device metadata types:
37 // (1) GpuDeviceMetadata: GPU device compute capability.
38 void AddDevicesToOp(mlir::Operation* op, const DeviceSet* device_set);
39 
40 // Collects devices information from an op `tf.devices` attributes. Returns
41 // failure if can't parse device metadata from the attribute.
42 mlir::LogicalResult GetDevicesFromOp(mlir::Operation* op,
43                                      mlir::TF::RuntimeDevices* devices);
44 
45 // Parses a device string and returns its ordinal (id). This will return an
46 // error if the device string is invalid or has no id.
47 mlir::LogicalResult GetDeviceOrdinalFromDeviceString(mlir::Location loc,
48                                                      llvm::StringRef device,
49                                                      int64_t* device_ordinal);
50 
51 }  // namespace tensorflow
52 
53 #endif  // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_DEVICE_UTIL_H_
54