├── REPO.bazel ├── BUILD.bazel ├── README.md ├── tsl ├── platform │ ├── notification.h │ ├── env.h │ ├── test.h │ ├── types.h │ ├── errors.h │ ├── macros.h │ ├── status.h │ ├── logging.h │ ├── env_time.h │ ├── statusor.h │ ├── threadpool.h │ ├── file_system.h │ ├── test_benchmark.h │ ├── file_statistics.h │ ├── status_matchers.h │ ├── file_system_helper.h │ ├── threadpool_options.h │ ├── status_to_from_proto.h │ ├── threadpool_interface.h │ ├── init_main.h │ ├── bfloat16.h │ ├── abi.h │ ├── regexp.h │ ├── cord.h │ ├── error_logging.h │ ├── tensor_float_32_utils.h │ ├── tensor_float_32_utils.cc │ ├── stacktrace_handler_test.cc │ ├── random_test.cc │ ├── demangle.h │ ├── stacktrace_handler.h │ ├── casts.h │ ├── random.h │ ├── cpu_info_test.cc │ ├── load_library.h │ ├── dso_loader.h │ ├── stacktrace_test.cc │ ├── abi_test.cc │ ├── stacktrace.h │ ├── setround.cc │ ├── unbounded_work_queue.h │ ├── stringpiece.h │ ├── net_test.cc │ ├── context.h │ ├── ml_dtypes.h │ ├── random.cc │ ├── scanner.cc │ ├── net.h │ ├── tracing.cc │ ├── snappy.h │ ├── setround.h │ ├── cuda_root_path.h │ ├── abi.cc │ ├── host_info.h │ ├── stringpiece_test.cc │ ├── protobuf_util.cc │ ├── human_readable_json.h │ ├── protobuf.cc │ ├── integral_types_test.cc │ ├── numa_test.cc │ ├── numa.h │ ├── raw_coding.h │ ├── blocking_counter.h │ ├── base64.h │ ├── platform.h │ ├── coding.h │ ├── fingerprint_test.cc │ ├── retrying_utils.h │ ├── denormal.h │ ├── setround_test.cc │ ├── hash.cc │ ├── unbounded_work_queue_test.cc │ ├── stringprintf_test.cc │ ├── null_file_system.h │ ├── mem.h │ ├── mutex_test.cc │ └── hash.h └── profiler │ ├── protobuf │ ├── profiled_instructions.proto │ ├── profiler_service_monitor_result.proto │ ├── profile.proto │ ├── trace_events.proto │ ├── profiler_analysis.proto │ └── BUILD │ └── lib │ ├── scoped_memory_debug_annotation.cc │ ├── nvtx_utils_stub.cc │ ├── profiler_collection.h │ ├── profiler_interface.h │ ├── profiler_factory.h │ ├── profiler_collection.cc │ ├── context_types.h │ ├── context_types.cc │ ├── profiler_factory.cc │ ├── profiler_controller.h │ ├── profiler_lock.h │ ├── profiler_lock.cc │ ├── profiler_lock_test.cc │ ├── profiler_controller.cc │ ├── nvtx_utils.h │ ├── profiler_session.h │ ├── profiler_factory_test.cc │ ├── scoped_annotation.h │ ├── traceme_encode_test.cc │ └── scoped_annotation_test.cc └── .github └── workflows └── scorecards-analysis.yml /REPO.bazel: -------------------------------------------------------------------------------- 1 | # Mark the repo as a bazel repo. 2 | -------------------------------------------------------------------------------- /BUILD.bazel: -------------------------------------------------------------------------------- 1 | load("@rules_license//rules:license.bzl", "license") 2 | 3 | package( 4 | default_applicable_licenses = [":license"], 5 | default_visibility = ["//visibility:public"], 6 | ) 7 | 8 | licenses(["notice"]) 9 | 10 | license( 11 | name = "license", 12 | package_name = "tsl", 13 | license_kinds = ["@rules_license//licenses/spdx:Apache-2.0"], 14 | ) 15 | 16 | cc_library( 17 | name = "empty", 18 | visibility = ["//visibility:public"], 19 | ) 20 | 21 | # Needed to workaround https://github.com/bazelbuild/bazel/issues/21519 22 | alias( 23 | name = "bazel_issue_21519", 24 | actual = ":empty", 25 | visibility = ["//visibility:public"], 26 | ) 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TSL: Tensor Standard Libraries 2 | 3 | This repo contains base utilities and cross-platform support for projects like 4 | [XLA](https://github.com/openxla/xla/) and 5 | [TensorFlow](https://github.com/tensorflow/tensorflow). 6 | 7 | > **Warning** This repo is currently being migrated from TensorFlow. Until the 8 | > migration is complete, this repo will not be accepting PRs or issues. Please 9 | > go to the 10 | > [upstream location](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tsl) 11 | > to make any contributions or report any issues. 12 | 13 | ## Code of Conduct 14 | 15 | While under TensorFlow governance, all community spaces are subject to the 16 | [TensorFlow Code of Conduct](https://github.com/tensorflow/tensorflow/blob/master/CODE_OF_CONDUCT.md). 17 | 18 | ## License 19 | 20 | [Apache License 2.0](https://github.com/google/tsl/blob/master/LICENSE) 21 | 22 | -------------------------------------------------------------------------------- /tsl/platform/notification.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2016 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_TSL_PLATFORM_NOTIFICATION_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_NOTIFICATION_H_ 18 | 19 | #endif // TENSORFLOW_TSL_PLATFORM_NOTIFICATION_H_ 20 | -------------------------------------------------------------------------------- /tsl/platform/env.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_ENV_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_ENV_H_ 18 | 19 | #include "xla/tsl/platform/env.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_ENV_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/test.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_TEST_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_TEST_H_ 18 | 19 | #include "xla/tsl/platform/test.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_TEST_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/types.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_TYPES_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_TYPES_H_ 18 | 19 | #include "xla/tsl/platform/types.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_TYPES_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/errors.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_ERRORS_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_ERRORS_H_ 18 | 19 | #include "xla/tsl/platform/errors.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_ERRORS_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/macros.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_MACROS_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_MACROS_H_ 18 | 19 | #include "xla/tsl/platform/macros.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_MACROS_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/status.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_STATUS_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_STATUS_H_ 18 | 19 | #include "xla/tsl/platform/status.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_STATUS_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/logging.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_LOGGING_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_LOGGING_H_ 18 | 19 | #include "xla/tsl/platform/logging.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_LOGGING_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/env_time.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_ENV_TIME_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_ENV_TIME_H_ 18 | 19 | #include "xla/tsl/platform/env_time.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_ENV_TIME_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/statusor.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_STATUSOR_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_STATUSOR_H_ 18 | 19 | #include "xla/tsl/platform/statusor.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_STATUSOR_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/threadpool.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_THREADPOOL_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_THREADPOOL_H_ 18 | 19 | #include "xla/tsl/platform/threadpool.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_THREADPOOL_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/file_system.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_FILE_SYSTEM_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_FILE_SYSTEM_H_ 18 | 19 | #include "xla/tsl/platform/file_system.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_FILE_SYSTEM_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/test_benchmark.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_TEST_BENCHMARK_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_TEST_BENCHMARK_H_ 18 | 19 | #include "xla/tsl/platform/test_benchmark.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_TEST_BENCHMARK_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/file_statistics.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_FILE_STATISTICS_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_FILE_STATISTICS_H_ 18 | 19 | #include "xla/tsl/platform/file_statistics.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_FILE_STATISTICS_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/status_matchers.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_STATUS_MATCHERS_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_STATUS_MATCHERS_H_ 18 | 19 | #include "xla/tsl/platform/status_matchers.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_STATUS_MATCHERS_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/file_system_helper.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_FILE_SYSTEM_HELPER_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_FILE_SYSTEM_HELPER_H_ 18 | 19 | #include "xla/tsl/platform/file_system_helper.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_FILE_SYSTEM_HELPER_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/threadpool_options.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_THREADPOOL_OPTIONS_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_THREADPOOL_OPTIONS_H_ 18 | 19 | #include "xla/tsl/platform/threadpool_options.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_THREADPOOL_OPTIONS_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/status_to_from_proto.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_STATUS_TO_FROM_PROTO_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_STATUS_TO_FROM_PROTO_H_ 18 | 19 | #include "xla/tsl/platform/status_to_from_proto.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_STATUS_TO_FROM_PROTO_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/threadpool_interface.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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_TSL_PLATFORM_THREADPOOL_INTERFACE_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_THREADPOOL_INTERFACE_H_ 18 | 19 | #include "xla/tsl/platform/threadpool_interface.h" 20 | 21 | #endif // TENSORFLOW_TSL_PLATFORM_THREADPOOL_INTERFACE_H_ 22 | -------------------------------------------------------------------------------- /tsl/platform/init_main.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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_TSL_PLATFORM_INIT_MAIN_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_INIT_MAIN_H_ 18 | 19 | namespace tsl { 20 | namespace port { 21 | 22 | void InitMain(const char* usage, int* argc, char*** argv); 23 | 24 | } // namespace port 25 | } // namespace tsl 26 | 27 | #endif // TENSORFLOW_TSL_PLATFORM_INIT_MAIN_H_ 28 | -------------------------------------------------------------------------------- /tsl/platform/bfloat16.h: -------------------------------------------------------------------------------- 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 | 16 | #ifndef TENSORFLOW_TSL_PLATFORM_BFLOAT16_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_BFLOAT16_H_ 18 | 19 | // clang-format off 20 | #include "Eigen/Core" // from @eigen_archive 21 | // clang-format on 22 | 23 | namespace tsl { 24 | typedef Eigen::bfloat16 bfloat16; 25 | } // end namespace tsl 26 | 27 | #endif // TENSORFLOW_TSL_PLATFORM_BFLOAT16_H_ 28 | -------------------------------------------------------------------------------- /tsl/platform/abi.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 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_TSL_PLATFORM_ABI_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_ABI_H_ 18 | 19 | #include 20 | 21 | #include "xla/tsl/platform/types.h" 22 | 23 | namespace tsl { 24 | namespace port { 25 | 26 | std::string MaybeAbiDemangle(const char* name); 27 | 28 | } // namespace port 29 | } // namespace tsl 30 | 31 | #endif // TENSORFLOW_TSL_PLATFORM_ABI_H_ 32 | -------------------------------------------------------------------------------- /tsl/platform/regexp.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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_TSL_PLATFORM_REGEXP_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_REGEXP_H_ 18 | 19 | #include "tsl/platform/platform.h" 20 | 21 | #if TSL_IS_IN_OSS 22 | #include "re2/re2.h" // IWYU pragma: export 23 | #else 24 | #include "third_party/re2/re2.h" // IWYU pragma: export 25 | #endif // TSL_IS_IN_OSS 26 | 27 | #endif // TENSORFLOW_TSL_PLATFORM_REGEXP_H_ 28 | -------------------------------------------------------------------------------- /tsl/platform/cord.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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_TSL_PLATFORM_CORD_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_CORD_H_ 18 | 19 | // It seems CORD doesn't work well with CUDA <= 10.2 20 | #if !defined(__CUDACC__) || ((defined(__CUDACC__) && CUDA_VERSION > 10020)) 21 | #include "absl/strings/cord.h" // IWYU pragma: export 22 | #define TF_CORD_SUPPORT 1 23 | 24 | #endif // __CUDACC__ 25 | 26 | #endif // TENSORFLOW_TSL_PLATFORM_CORD_H_ 27 | -------------------------------------------------------------------------------- /tsl/platform/error_logging.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2023 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_TSL_PLATFORM_ERROR_LOGGING_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_ERROR_LOGGING_H_ 18 | 19 | #include "absl/status/status.h" 20 | #include "absl/strings/string_view.h" 21 | 22 | namespace tsl::error_logging { 23 | 24 | absl::Status Log(absl::string_view component, absl::string_view subcomponent, 25 | absl::string_view error_msg); 26 | 27 | } 28 | 29 | #endif // TENSORFLOW_TSL_PLATFORM_ERROR_LOGGING_H_ 30 | -------------------------------------------------------------------------------- /tsl/profiler/protobuf/profiled_instructions.proto: -------------------------------------------------------------------------------- 1 | /* Copyright 2023 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 | syntax = "proto3"; 17 | 18 | package tensorflow.profiler; 19 | 20 | // Next ID: 3 21 | message ProfiledInstructionsProto { 22 | message InstructionCost { 23 | string name = 1; 24 | double cost_us = 2; 25 | } 26 | message Latency { 27 | string source = 1; 28 | string target = 2; 29 | double latency_us = 3; 30 | } 31 | repeated InstructionCost costs = 1; 32 | repeated Latency latencies = 2; 33 | } 34 | -------------------------------------------------------------------------------- /tsl/platform/tensor_float_32_utils.h: -------------------------------------------------------------------------------- 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 | 16 | #ifndef TENSORFLOW_TSL_PLATFORM_TENSOR_FLOAT_32_UTILS_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_TENSOR_FLOAT_32_UTILS_H_ 18 | 19 | namespace tsl { 20 | 21 | // NOTE: The usage of this function is only supported through the Tensorflow 22 | // framework. 23 | void enable_tensor_float_32_execution(bool enabled); 24 | 25 | bool tensor_float_32_execution_enabled(); 26 | 27 | } // namespace tsl 28 | 29 | #endif // TENSORFLOW_TSL_PLATFORM_TENSOR_FLOAT_32_UTILS_H_ 30 | -------------------------------------------------------------------------------- /tsl/platform/tensor_float_32_utils.cc: -------------------------------------------------------------------------------- 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 | 16 | #include "tsl/platform/tensor_float_32_utils.h" 17 | 18 | #include 19 | 20 | namespace tsl { 21 | 22 | // Whether TensorFloat-32 should be used where supported. 23 | static std::atomic tensor_float_32_enabled{true}; 24 | 25 | void enable_tensor_float_32_execution(bool enabled) { 26 | tensor_float_32_enabled = enabled; 27 | } 28 | 29 | bool tensor_float_32_execution_enabled() { return tensor_float_32_enabled; } 30 | 31 | } // namespace tsl 32 | -------------------------------------------------------------------------------- /tsl/profiler/lib/scoped_memory_debug_annotation.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 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 | #include "tsl/profiler/lib/scoped_memory_debug_annotation.h" 16 | 17 | #include 18 | 19 | namespace tsl { 20 | namespace profiler { 21 | 22 | std::string DefaultPendingShapeFunc() { return ""; } 23 | 24 | /*static*/ MemoryDebugAnnotation* 25 | ScopedMemoryDebugAnnotation::ThreadMemoryDebugAnnotation() { 26 | static thread_local MemoryDebugAnnotation annotation; 27 | return &annotation; 28 | } 29 | 30 | } // namespace profiler 31 | } // namespace tsl 32 | -------------------------------------------------------------------------------- /tsl/platform/stacktrace_handler_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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 | // Testing proper operation of the stacktrace handler. 16 | 17 | #include 18 | 19 | #include "xla/tsl/platform/logging.h" 20 | #include "xla/tsl/platform/test.h" 21 | 22 | namespace tsl { 23 | namespace { 24 | 25 | TEST(StacktraceHandlerTest, GeneratesStacktrace) { 26 | // Just make sure we can detect one of the calls in testing stack. 27 | EXPECT_DEATH(raise(SIGABRT), "testing::internal::UnitTestImpl::RunAllTests"); 28 | } 29 | 30 | } // namespace 31 | } // namespace tsl 32 | -------------------------------------------------------------------------------- /tsl/platform/random_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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 | #include "tsl/platform/random.h" 17 | 18 | #include 19 | 20 | #include "xla/tsl/platform/test.h" 21 | #include "xla/tsl/platform/types.h" 22 | 23 | namespace tsl { 24 | namespace random { 25 | namespace { 26 | 27 | TEST(New64Test, SanityCheck) { 28 | std::set values; 29 | for (int i = 0; i < 1000000; i++) { 30 | uint64 x = New64(); 31 | EXPECT_TRUE(values.insert(x).second) << "duplicate " << x; 32 | } 33 | } 34 | 35 | } // namespace 36 | } // namespace random 37 | } // namespace tsl 38 | -------------------------------------------------------------------------------- /tsl/platform/demangle.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2016 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_TSL_PLATFORM_DEMANGLE_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_DEMANGLE_H_ 18 | 19 | #include "xla/tsl/platform/types.h" 20 | 21 | namespace tsl { 22 | namespace port { 23 | 24 | // If the compiler supports, demangle a mangled symbol name and return 25 | // the demangled name. Otherwise, returns 'mangled' as is. 26 | string Demangle(const char* mangled); 27 | inline string Demangle(const string mangled) { 28 | return Demangle(mangled.c_str()); 29 | } 30 | 31 | } // namespace port 32 | } // namespace tsl 33 | 34 | #endif // TENSORFLOW_TSL_PLATFORM_DEMANGLE_H_ 35 | -------------------------------------------------------------------------------- /tsl/platform/stacktrace_handler.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 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_TSL_PLATFORM_STACKTRACE_HANDLER_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_STACKTRACE_HANDLER_H_ 18 | 19 | namespace tsl { 20 | namespace testing { 21 | 22 | // Installs signal handlers to print out stack trace. 23 | // Although GoogleTest has support for generating stacktraces with abseil via 24 | // https://github.com/google/googletest/pull/1653, this doesn't cover our use 25 | // case of getting C++ stacktraces in our python tests. 26 | void InstallStacktraceHandler(); 27 | 28 | } // namespace testing 29 | } // namespace tsl 30 | 31 | #endif // TENSORFLOW_TSL_PLATFORM_STACKTRACE_HANDLER_H_ 32 | -------------------------------------------------------------------------------- /tsl/platform/casts.h: -------------------------------------------------------------------------------- 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 | 16 | #ifndef TENSORFLOW_TSL_PLATFORM_CASTS_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_CASTS_H_ 18 | 19 | #include "tsl/platform/platform.h" 20 | 21 | #if defined(PLATFORM_GOOGLE) 22 | #include "xla/tsl/platform/google/casts.h" // IWYU pragma: export 23 | #elif defined(PLATFORM_POSIX) || defined(PLATFORM_POSIX_ANDROID) || \ 24 | defined(PLATFORM_GOOGLE_ANDROID) || defined(PLATFORM_POSIX_IOS) || \ 25 | defined(PLATFORM_GOOGLE_IOS) || defined(PLATFORM_WINDOWS) 26 | #include "xla/tsl/platform/default/casts.h" // IWYU pragma: export 27 | #else 28 | #error Define the appropriate PLATFORM_ macro for this platform 29 | #endif 30 | 31 | #endif // TENSORFLOW_TSL_PLATFORM_CASTS_H_ 32 | -------------------------------------------------------------------------------- /tsl/platform/random.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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_TSL_PLATFORM_RANDOM_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_RANDOM_H_ 18 | 19 | #include "xla/tsl/platform/types.h" 20 | 21 | namespace tsl { 22 | namespace random { 23 | 24 | // Return a 64-bit random value. Different sequences are generated 25 | // in different processes. 26 | uint64 New64(); 27 | 28 | // Same as previous method, but uses a different RNG for each thread. 29 | uint64 ThreadLocalNew64(); 30 | 31 | // Return a 64-bit random value. Uses 32 | // std::mersenne_twister_engine::default_seed as seed value. 33 | uint64 New64DefaultSeed(); 34 | 35 | } // namespace random 36 | } // namespace tsl 37 | 38 | #endif // TENSORFLOW_TSL_PLATFORM_RANDOM_H_ 39 | -------------------------------------------------------------------------------- /tsl/profiler/protobuf/profiler_service_monitor_result.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package tensorflow; 4 | 5 | message ProfilerServiceMonitorResult { 6 | // Represents the different types of responses from the profiling service. 7 | enum ResponseType { 8 | // No result is returned from the profiling service. 9 | EMPTY_RESULT = 0; 10 | // Only device utilization is available. 11 | UTIL_ONLY = 1; 12 | // Both device utilization and device idle time are available. 13 | UTIL_IDLE = 2; 14 | // Device utilization, device idle time, step time, and infeed percentage 15 | // are all available. 16 | UTIL_IDLE_STEP = 3; 17 | } 18 | 19 | // Type of profiling responses. 20 | ResponseType response_type = 1; 21 | // Percentage of time when device is idle. 22 | double device_idle_time_percent = 2; 23 | // TPU matrix unit utilization percentage. 24 | double matrix_unit_utilization_percent = 3; 25 | // Average step time in millisecond. 26 | double step_time_ms_avg = 4; 27 | // Minimum step time in millisecond. 28 | double step_time_ms_min = 5; 29 | // Maximum step time in millisecond. 30 | double step_time_ms_max = 6; 31 | // Average infeed percentage. 32 | double infeed_percent_avg = 7; 33 | // Minimum infeed percentage. 34 | double infeed_percent_min = 8; 35 | // Maximum infeed percentage. 36 | double infeed_percent_max = 9; 37 | 38 | // next-field: 10 39 | } 40 | -------------------------------------------------------------------------------- /tsl/platform/cpu_info_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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 | 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 | #include "tsl/platform/cpu_info.h" 18 | 19 | #include "xla/tsl/platform/test.h" 20 | 21 | namespace tsl { 22 | 23 | TEST(CPUInfo, CommonX86CPU) { 24 | // CPUs from 1999 onwards support SSE. 25 | if (port::TestCPUFeature(port::CPUFeature::SSE)) { 26 | EXPECT_TRUE(port::IsX86CPU()); 27 | } 28 | } 29 | 30 | TEST(CPUInfo, Aarch64NeoverseV1CPU) { 31 | if (port::TestAarch64CPU(port::Aarch64CPU::ARM_NEOVERSE_V1)) { 32 | EXPECT_TRUE(port::IsAarch64CPU()); 33 | } 34 | } 35 | 36 | TEST(CPUInfo, Aarch64Bf16) { 37 | if (port::TestCPUFeature(port::CPUFeature::AARCH64_BF16)) { 38 | EXPECT_TRUE(port::IsAarch64CPU()); 39 | } 40 | } 41 | 42 | } // namespace tsl 43 | -------------------------------------------------------------------------------- /tsl/platform/load_library.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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_TSL_PLATFORM_LOAD_LIBRARY_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_LOAD_LIBRARY_H_ 18 | 19 | #include 20 | 21 | #include "absl/status/status.h" 22 | 23 | namespace tsl { 24 | 25 | namespace internal { 26 | 27 | absl::Status LoadDynamicLibrary(const char* library_filename, void** handle); 28 | absl::Status GetSymbolFromLibrary(void* handle, const char* symbol_name, 29 | void** symbol); 30 | std::string FormatLibraryFileName(const std::string& name, 31 | const std::string& version); 32 | 33 | } // namespace internal 34 | 35 | } // namespace tsl 36 | 37 | #endif // TENSORFLOW_TSL_PLATFORM_LOAD_LIBRARY_H_ 38 | -------------------------------------------------------------------------------- /tsl/platform/dso_loader.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 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_TSL_PLATFORM_DSO_LOADER_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_DSO_LOADER_H_ 18 | 19 | #include "tsl/platform/platform.h" 20 | 21 | // Include appropriate platform-dependent implementations 22 | #if defined(PLATFORM_GOOGLE) || \ 23 | (defined(PLATFORM_PORTABLE_GOOGLE) && !defined(__EMSCRIPTEN__)) 24 | #include "xla/tsl/platform/google/dso_loader.h" 25 | #elif defined(PLATFORM_POSIX) || defined(PLATFORM_POSIX_ANDROID) || \ 26 | defined(PLATFORM_GOOGLE_ANDROID) || defined(PLATFORM_WINDOWS) 27 | #include "xla/tsl/platform/default/dso_loader.h" 28 | #else 29 | #error Define the appropriate PLATFORM_ macro for this platform 30 | #endif 31 | 32 | #endif // TENSORFLOW_TSL_PLATFORM_DSO_LOADER_H_ 33 | -------------------------------------------------------------------------------- /tsl/platform/stacktrace_test.cc: -------------------------------------------------------------------------------- 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 | // Testing proper operation of the stacktrace handler. 16 | 17 | #include "tsl/platform/stacktrace.h" 18 | 19 | #include 20 | 21 | #include "xla/tsl/platform/logging.h" 22 | #include "xla/tsl/platform/test.h" 23 | 24 | namespace tsl { 25 | namespace { 26 | 27 | #if defined(TF_HAS_STACKTRACE) 28 | 29 | TEST(StacktraceTest, StacktraceWorks) { 30 | std::string stacktrace = CurrentStackTrace(); 31 | LOG(INFO) << "CurrentStackTrace():\n" << stacktrace; 32 | std::string expected_frame = "testing::internal::UnitTestImpl::RunAllTests"; 33 | EXPECT_NE(stacktrace.find(expected_frame), std::string::npos); 34 | } 35 | 36 | #endif // defined(TF_HAS_STACKTRACE) 37 | 38 | } // namespace 39 | } // namespace tsl 40 | -------------------------------------------------------------------------------- /tsl/platform/abi_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 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 | #include "tsl/platform/abi.h" 17 | 18 | #include 19 | 20 | #include "xla/tsl/platform/test.h" 21 | 22 | namespace tsl { 23 | 24 | struct MyRandomPODType {}; 25 | 26 | TEST(AbiTest, AbiDemangleTest) { 27 | EXPECT_EQ(port::MaybeAbiDemangle(typeid(int).name()), "int"); 28 | 29 | #ifdef PLATFORM_WINDOWS 30 | const char pod_type_name[] = "struct tsl::MyRandomPODType"; 31 | #else 32 | const char pod_type_name[] = "tsl::MyRandomPODType"; 33 | #endif 34 | EXPECT_EQ(port::MaybeAbiDemangle(typeid(MyRandomPODType).name()), 35 | pod_type_name); 36 | 37 | EXPECT_EQ( 38 | port::MaybeAbiDemangle("help! i'm caught in a C++ mangle factoryasdf"), 39 | "help! i'm caught in a C++ mangle factoryasdf"); 40 | } 41 | 42 | } // namespace tsl 43 | -------------------------------------------------------------------------------- /tsl/platform/stacktrace.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2016 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_TSL_PLATFORM_STACKTRACE_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_STACKTRACE_H_ 18 | 19 | #include "tsl/platform/platform.h" // IWYU pragma: export 20 | 21 | // Include appropriate platform-dependent implementation. 22 | #if defined(PLATFORM_GOOGLE) 23 | #include "xla/tsl/platform/google/stacktrace.h" // IWYU pragma: export 24 | #elif defined(PLATFORM_POSIX) || defined(PLATFORM_POSIX_ANDROID) || \ 25 | defined(PLATFORM_GOOGLE_ANDROID) || defined(PLATFORM_POSIX_IOS) || \ 26 | defined(PLATFORM_GOOGLE_IOS) 27 | #include "xla/tsl/platform/default/stacktrace.h" // IWYU pragma: export 28 | #elif defined(PLATFORM_WINDOWS) 29 | #include "xla/tsl/platform/windows/stacktrace.h" // IWYU pragma: export 30 | #else 31 | #error Define the appropriate PLATFORM_ macro for this platform 32 | #endif 33 | 34 | #endif // TENSORFLOW_TSL_PLATFORM_STACKTRACE_H_ 35 | -------------------------------------------------------------------------------- /tsl/platform/setround.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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 | #include "tsl/platform/setround.h" 17 | 18 | #include "xla/tsl/platform/logging.h" 19 | 20 | namespace tsl { 21 | namespace port { 22 | 23 | #if defined(TF_BROKEN_CFENV) 24 | 25 | ScopedSetRound::ScopedSetRound(const int mode) : original_mode_(mode) { 26 | // If cfenv usage is broken, assume support only for TONEAREST. 27 | DCHECK_EQ(mode, FE_TONEAREST); 28 | } 29 | 30 | ScopedSetRound::~ScopedSetRound() {} 31 | 32 | #else 33 | 34 | ScopedSetRound::ScopedSetRound(const int mode) { 35 | original_mode_ = std::fegetround(); 36 | if (original_mode_ < 0) { 37 | // Failed to get current mode, assume ROUND TO NEAREST. 38 | original_mode_ = FE_TONEAREST; 39 | } 40 | std::fesetround(mode); 41 | } 42 | 43 | ScopedSetRound::~ScopedSetRound() { std::fesetround(original_mode_); } 44 | 45 | #endif // TF_BROKEN_CFENV 46 | 47 | } // namespace port 48 | } // namespace tsl 49 | -------------------------------------------------------------------------------- /tsl/platform/unbounded_work_queue.h: -------------------------------------------------------------------------------- 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_TSL_PLATFORM_UNBOUNDED_WORK_QUEUE_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_UNBOUNDED_WORK_QUEUE_H_ 18 | 19 | #include "tsl/platform/platform.h" 20 | 21 | // An `UnboundedWorkQueue` feeds potentially-blocking work into a thread-pool 22 | // whose size automatically increases with demand. 23 | 24 | #if defined(PLATFORM_GOOGLE) 25 | #include "xla/tsl/platform/google/unbounded_work_queue.h" // IWYU pragma: export 26 | #elif defined(PLATFORM_POSIX) || defined(PLATFORM_POSIX_ANDROID) || \ 27 | defined(PLATFORM_GOOGLE_ANDROID) || defined(PLATFORM_POSIX_IOS) || \ 28 | defined(PLATFORM_GOOGLE_IOS) || defined(PLATFORM_WINDOWS) 29 | #include "xla/tsl/platform/default/unbounded_work_queue.h" // IWYU pragma: export 30 | #else 31 | #error Define the appropriate PLATFORM_ macro for this platform 32 | #endif 33 | 34 | #endif // TENSORFLOW_TSL_PLATFORM_UNBOUNDED_WORK_QUEUE_H_ 35 | -------------------------------------------------------------------------------- /tsl/profiler/lib/nvtx_utils_stub.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2024 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 | #include 16 | 17 | #include "tsl/profiler/lib/nvtx_utils.h" 18 | 19 | namespace tsl::profiler { 20 | ProfilerDomainHandle DefaultProfilerDomain() { return {}; } 21 | void NameCurrentThread(const std::string&) {} 22 | void NameDevice(int, const std::string&) {} 23 | void NameStream(StreamHandle, const std::string&) {} 24 | void RangePop(ProfilerDomainHandle) {} 25 | void RangePush(ProfilerDomainHandle, const char*) {} 26 | namespace detail { 27 | void RangePush(ProfilerDomainHandle, StringHandle, uint64_t, const void*, 28 | size_t) {} 29 | } // namespace detail 30 | uint64_t RegisterSchema(ProfilerDomainHandle, const void*) { return 0; } 31 | StringHandle RegisterString(ProfilerDomainHandle, const std::string&) { 32 | return {}; 33 | } 34 | void MarkMemoryInitialized(void const* address, size_t size, 35 | StreamHandle stream) {} 36 | } // namespace tsl::profiler 37 | -------------------------------------------------------------------------------- /tsl/platform/stringpiece.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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 | // StringPiece is a simple structure containing a pointer into some external 17 | // storage and a size. The user of a StringPiece must ensure that the slice 18 | // is not used after the corresponding external storage has been 19 | // deallocated. 20 | // 21 | // Multiple threads can invoke const methods on a StringPiece without 22 | // external synchronization, but if any of the threads may call a 23 | // non-const method, all threads accessing the same StringPiece must use 24 | // external synchronization. 25 | 26 | #ifndef TENSORFLOW_TSL_PLATFORM_STRINGPIECE_H_ 27 | #define TENSORFLOW_TSL_PLATFORM_STRINGPIECE_H_ 28 | 29 | // IWYU pragma: private, include "absl/strings/string_view.h" 30 | 31 | #include "absl/base/macros.h" 32 | #include "absl/strings/string_view.h" 33 | 34 | namespace tsl { 35 | 36 | using StringPiece ABSL_DEPRECATE_AND_INLINE() = absl::string_view; 37 | 38 | } // namespace tsl 39 | 40 | #endif // TENSORFLOW_TSL_PLATFORM_STRINGPIECE_H_ 41 | -------------------------------------------------------------------------------- /tsl/profiler/protobuf/profile.proto: -------------------------------------------------------------------------------- 1 | // This proto intends to match format expected by pprof tool. 2 | syntax = "proto3"; 3 | 4 | package tensorflow.tfprof.pprof; 5 | 6 | message Profile { 7 | repeated ValueType sample_type = 1; 8 | repeated Sample sample = 2; 9 | repeated Mapping mapping = 3; 10 | repeated Location location = 4; 11 | repeated Function function = 5; 12 | repeated string string_table = 6; 13 | int64 drop_frames = 7; 14 | int64 keep_frames = 8; 15 | int64 time_nanos = 9; 16 | int64 duration_nanos = 10; 17 | ValueType period_type = 11; 18 | int64 period = 12; 19 | repeated int64 comment = 13; 20 | int64 default_sample_type = 14; 21 | } 22 | 23 | message ValueType { 24 | int64 type = 1; 25 | int64 unit = 2; 26 | } 27 | 28 | message Sample { 29 | repeated uint64 location_id = 1; 30 | repeated int64 value = 2; 31 | repeated Label label = 3; 32 | } 33 | 34 | message Label { 35 | int64 key = 1; 36 | int64 str = 2; 37 | int64 num = 3; 38 | } 39 | 40 | message Mapping { 41 | uint64 id = 1; 42 | uint64 memory_start = 2; 43 | uint64 memory_limit = 3; 44 | uint64 file_offset = 4; 45 | int64 filename = 5; 46 | int64 build_id = 6; 47 | bool has_functions = 7; 48 | bool has_filenames = 8; 49 | bool has_line_numbers = 9; 50 | bool has_inline_frames = 10; 51 | } 52 | 53 | message Location { 54 | uint64 id = 1; 55 | uint64 mapping_id = 2; 56 | uint64 address = 3; 57 | repeated Line line = 4; 58 | } 59 | 60 | message Line { 61 | uint64 function_id = 1; 62 | int64 line = 2; 63 | } 64 | 65 | message Function { 66 | uint64 id = 1; 67 | int64 name = 2; 68 | int64 system_name = 3; 69 | int64 filename = 4; 70 | int64 start_line = 5; 71 | } 72 | -------------------------------------------------------------------------------- /tsl/platform/net_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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 | #include "tsl/platform/net.h" 17 | 18 | #include "xla/tsl/platform/logging.h" 19 | #include "xla/tsl/platform/test.h" 20 | 21 | namespace tsl { 22 | namespace internal { 23 | 24 | TEST(Net, PickUnusedPortOrDie) { 25 | int port0 = PickUnusedPortOrDie(); 26 | int port1 = PickUnusedPortOrDie(); 27 | CHECK_GE(port0, 0); 28 | CHECK_LT(port0, 65536); 29 | CHECK_GE(port1, 0); 30 | CHECK_LT(port1, 65536); 31 | CHECK_NE(port0, port1); 32 | } 33 | 34 | TEST(Net, RecycleUnusedPort) { 35 | for (int i = 0; i < 1000; ++i) { 36 | int port0 = PickUnusedPortOrDie(); 37 | CHECK_GE(port0, 0); 38 | CHECK_LT(port0, 65536); 39 | RecycleUnusedPort(port0); 40 | } 41 | } 42 | 43 | TEST(Net, RecycleUnusedPortTwiceShallFail) { 44 | int port0 = PickUnusedPortOrDie(); 45 | CHECK_GE(port0, 0); 46 | CHECK_LT(port0, 65536); 47 | RecycleUnusedPort(port0); 48 | 49 | EXPECT_DEATH(RecycleUnusedPort(port0), ""); 50 | } 51 | 52 | } // namespace internal 53 | } // namespace tsl 54 | -------------------------------------------------------------------------------- /tsl/platform/context.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2016 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_TSL_PLATFORM_CONTEXT_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_CONTEXT_H_ 18 | 19 | #include "tsl/platform/platform.h" 20 | 21 | namespace tsl { 22 | 23 | enum class ContextKind { 24 | // Initial state with default (empty) values. 25 | kDefault, 26 | // Initial state inherited from the creating or scheduling thread. 27 | kThread, 28 | }; 29 | 30 | // Context is a container for request-specific information that should be passed 31 | // to threads that perform related work. The default constructor should capture 32 | // all relevant context. 33 | class Context; 34 | 35 | // Scoped object that sets the current thread's context until the object is 36 | // destroyed. 37 | class WithContext; 38 | 39 | } // namespace tsl 40 | 41 | #if defined(PLATFORM_GOOGLE) 42 | #include "xla/tsl/platform/google/context.h" // IWYU pragma: export 43 | #else 44 | #include "xla/tsl/platform/default/context.h" // IWYU pragma: export 45 | #endif 46 | 47 | #endif // TENSORFLOW_TSL_PLATFORM_CONTEXT_H_ 48 | -------------------------------------------------------------------------------- /tsl/profiler/lib/profiler_collection.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 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_TSL_PROFILER_LIB_PROFILER_COLLECTION_H_ 16 | #define TENSORFLOW_TSL_PROFILER_LIB_PROFILER_COLLECTION_H_ 17 | 18 | #include 19 | #include 20 | 21 | #include "absl/status/status.h" 22 | #include "xla/tsl/platform/status.h" 23 | #include "tsl/profiler/lib/profiler_interface.h" 24 | #include "tsl/profiler/protobuf/xplane.pb.h" 25 | 26 | namespace tsl { 27 | namespace profiler { 28 | 29 | // ProfilerCollection multiplexes ProfilerInterface calls into a collection of 30 | // profilers. 31 | class ProfilerCollection : public ProfilerInterface { 32 | public: 33 | explicit ProfilerCollection( 34 | std::vector> profilers); 35 | 36 | absl::Status Start() override; 37 | 38 | absl::Status Stop() override; 39 | 40 | absl::Status CollectData(tensorflow::profiler::XSpace* space) override; 41 | 42 | private: 43 | std::vector> profilers_; 44 | }; 45 | 46 | } // namespace profiler 47 | } // namespace tsl 48 | 49 | #endif // TENSORFLOW_TSL_PROFILER_LIB_PROFILER_COLLECTION_H_ 50 | -------------------------------------------------------------------------------- /tsl/platform/ml_dtypes.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 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_TSL_PLATFORM_ML_DTYPES_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_ML_DTYPES_H_ 18 | 19 | #include "ml_dtypes/include/float8.h" // from @ml_dtypes_py 20 | #include "ml_dtypes/include/intn.h" // from @ml_dtypes_py 21 | #include "ml_dtypes/include/mxfloat.h" // from @ml_dtypes_py 22 | 23 | namespace tsl { 24 | using float4_e2m1fn = ::ml_dtypes::float4_e2m1fn; 25 | using float8_e3m4 = ::ml_dtypes::float8_e3m4; 26 | using float8_e4m3 = ::ml_dtypes::float8_e4m3; 27 | using float8_e4m3fn = ::ml_dtypes::float8_e4m3fn; 28 | using float8_e4m3fnuz = ::ml_dtypes::float8_e4m3fnuz; 29 | using float8_e4m3b11fnuz = ::ml_dtypes::float8_e4m3b11fnuz; 30 | using float8_e5m2 = ::ml_dtypes::float8_e5m2; 31 | using float8_e5m2fnuz = ::ml_dtypes::float8_e5m2fnuz; 32 | using float8_e8m0fnu = ::ml_dtypes::float8_e8m0fnu; 33 | 34 | using int1 = ::ml_dtypes::int1; 35 | using uint1 = ::ml_dtypes::uint1; 36 | using int2 = ::ml_dtypes::int2; 37 | using uint2 = ::ml_dtypes::uint2; 38 | using int4 = ::ml_dtypes::int4; 39 | using uint4 = ::ml_dtypes::uint4; 40 | } // namespace tsl 41 | 42 | #endif // TENSORFLOW_TSL_PLATFORM_ML_DTYPES_H_ 43 | -------------------------------------------------------------------------------- /tsl/platform/random.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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 | #include "tsl/platform/random.h" 17 | 18 | #include 19 | #include 20 | 21 | #include "xla/tsl/platform/types.h" 22 | #include "tsl/platform/mutex.h" 23 | 24 | namespace tsl { 25 | namespace random { 26 | 27 | namespace { 28 | std::mt19937_64* InitRngWithRandomSeed() { 29 | std::random_device device("/dev/urandom"); 30 | return new std::mt19937_64(device()); 31 | } 32 | std::mt19937_64 InitRngWithDefaultSeed() { return std::mt19937_64(); } 33 | 34 | } // anonymous namespace 35 | 36 | uint64 New64() { 37 | static std::mt19937_64* rng = InitRngWithRandomSeed(); 38 | static mutex mu(LINKER_INITIALIZED); 39 | mutex_lock l(mu); 40 | return (*rng)(); 41 | } 42 | 43 | uint64 ThreadLocalNew64() { 44 | static thread_local std::unique_ptr rng = 45 | std::unique_ptr(InitRngWithRandomSeed()); 46 | return (*rng)(); 47 | } 48 | 49 | uint64 New64DefaultSeed() { 50 | static std::mt19937_64 rng = InitRngWithDefaultSeed(); 51 | static mutex mu(LINKER_INITIALIZED); 52 | mutex_lock l(mu); 53 | return rng(); 54 | } 55 | 56 | } // namespace random 57 | } // namespace tsl 58 | -------------------------------------------------------------------------------- /tsl/platform/scanner.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2016 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 | #include "tsl/platform/scanner.h" 17 | 18 | namespace tsl { 19 | namespace strings { 20 | 21 | void Scanner::ScanUntilImpl(char end_ch, bool escaped) { 22 | for (;;) { 23 | if (cur_.empty()) { 24 | Error(); 25 | return; 26 | } 27 | const char ch = cur_[0]; 28 | if (ch == end_ch) { 29 | return; 30 | } 31 | 32 | cur_.remove_prefix(1); 33 | if (escaped && ch == '\\') { 34 | // Escape character, skip next character. 35 | if (cur_.empty()) { 36 | Error(); 37 | return; 38 | } 39 | cur_.remove_prefix(1); 40 | } 41 | } 42 | } 43 | 44 | bool Scanner::GetResult(absl::string_view* remaining, 45 | absl::string_view* capture) { 46 | if (error_) { 47 | return false; 48 | } 49 | if (remaining != nullptr) { 50 | *remaining = cur_; 51 | } 52 | if (capture != nullptr) { 53 | const char* end = capture_end_ == nullptr ? cur_.data() : capture_end_; 54 | *capture = absl::string_view(capture_start_, end - capture_start_); 55 | } 56 | return true; 57 | } 58 | 59 | } // namespace strings 60 | } // namespace tsl 61 | -------------------------------------------------------------------------------- /tsl/profiler/lib/profiler_interface.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2016 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_TSL_PROFILER_LIB_PROFILER_INTERFACE_H_ 16 | #define TENSORFLOW_TSL_PROFILER_LIB_PROFILER_INTERFACE_H_ 17 | 18 | #include "xla/tsl/platform/status.h" 19 | #include "tsl/profiler/protobuf/xplane.pb.h" 20 | 21 | namespace tsl { 22 | namespace profiler { 23 | 24 | // Interface for tensorflow profiler plugins. 25 | // 26 | // ProfileSession calls each of these methods at most once per instance, and 27 | // implementations can rely on that guarantee for simplicity. 28 | // 29 | // Thread-safety: Implementations are only required to be go/thread-compatible. 30 | // ProfileSession is go/thread-safe and synchronizes access to ProfilerInterface 31 | // instances. 32 | class ProfilerInterface { 33 | public: 34 | virtual ~ProfilerInterface() = default; 35 | 36 | // Starts profiling. 37 | virtual absl::Status Start() = 0; 38 | 39 | // Stops profiling. 40 | virtual absl::Status Stop() = 0; 41 | 42 | // Saves collected profile data into XSpace. 43 | virtual absl::Status CollectData(tensorflow::profiler::XSpace* space) = 0; 44 | }; 45 | 46 | } // namespace profiler 47 | } // namespace tsl 48 | 49 | #endif // TENSORFLOW_TSL_PROFILER_LIB_PROFILER_INTERFACE_H_ 50 | -------------------------------------------------------------------------------- /tsl/platform/net.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2016 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_TSL_PLATFORM_NET_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_NET_H_ 18 | 19 | namespace tsl { 20 | namespace internal { 21 | 22 | // Return a port number that is not currently bound to any TCP or UDP port. 23 | // On success returns the assigned port number. Otherwise returns -1. 24 | int PickUnusedPort(); 25 | 26 | // Same as PickUnusedPort(), but fails a CHECK() if a port can't be found. In 27 | // that case, the error message is logged to FATAL. 28 | int PickUnusedPortOrDie(); 29 | 30 | // Relinquish a claim on the given port which was previously returned by 31 | // PickUnusedPort[OrDie](). This allows PickUnusedPort[OrDie]() to return 32 | // the given port to another caller in the future. Since the number of 33 | // ports the portserver will give to a process is limited (typically 200), 34 | // recycling ports after they are no longer needed can help avoid 35 | // exhausting them. 'port' must be a positive number that was previously 36 | // returned by PickUnusedPort[OrDie](), and not yet recycled, otherwise an 37 | // abort may occur. 38 | void RecycleUnusedPort(int port); 39 | 40 | } // namespace internal 41 | } // namespace tsl 42 | 43 | #endif // TENSORFLOW_TSL_PLATFORM_NET_H_ 44 | -------------------------------------------------------------------------------- /tsl/platform/tracing.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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 | #include "tsl/platform/tracing.h" 17 | 18 | #include 19 | #include 20 | 21 | #include "tsl/platform/hash.h" 22 | 23 | namespace tsl { 24 | namespace tracing { 25 | namespace { 26 | std::atomic unique_arg{1}; 27 | } // namespace 28 | 29 | const char* GetEventCategoryName(EventCategory category) { 30 | switch (category) { 31 | case EventCategory::kScheduleClosure: 32 | return "ScheduleClosure"; 33 | case EventCategory::kRunClosure: 34 | return "RunClosure"; 35 | case EventCategory::kCompute: 36 | return "Compute"; 37 | default: 38 | return "Unknown"; 39 | } 40 | } 41 | 42 | std::array 43 | EventCollector::instances_; 44 | 45 | void SetEventCollector(EventCategory category, 46 | const EventCollector* collector) { 47 | EventCollector::instances_[static_cast(category)] = collector; 48 | } 49 | 50 | uint64 GetUniqueArg() { 51 | return unique_arg.fetch_add(1, std::memory_order_relaxed); 52 | } 53 | 54 | uint64 GetArgForName(absl::string_view name) { 55 | return Hash64(name.data(), name.size()); 56 | } 57 | 58 | } // namespace tracing 59 | } // namespace tsl 60 | -------------------------------------------------------------------------------- /tsl/platform/snappy.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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_TSL_PLATFORM_SNAPPY_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_SNAPPY_H_ 18 | 19 | #include "xla/tsl/platform/types.h" 20 | 21 | #if !defined(PLATFORM_WINDOWS) 22 | #include 23 | namespace tsl { 24 | using ::iovec; // NOLINT(misc-unused-using-decls) 25 | } // namespace tsl 26 | #else 27 | namespace tsl { 28 | struct iovec { 29 | void* iov_base; 30 | size_t iov_len; 31 | }; 32 | } // namespace tsl 33 | #endif 34 | 35 | namespace tsl { 36 | namespace port { 37 | 38 | // Snappy compression/decompression support 39 | bool Snappy_Compress(const char* input, size_t length, string* output); 40 | 41 | bool Snappy_CompressFromIOVec(const struct iovec* iov, 42 | size_t uncompressed_length, string* output); 43 | 44 | bool Snappy_GetUncompressedLength(const char* input, size_t length, 45 | size_t* result); 46 | bool Snappy_Uncompress(const char* input, size_t length, char* output); 47 | 48 | bool Snappy_UncompressToIOVec(const char* compressed, size_t compressed_length, 49 | const struct iovec* iov, size_t iov_cnt); 50 | 51 | } // namespace port 52 | } // namespace tsl 53 | 54 | #endif // TENSORFLOW_TSL_PLATFORM_SNAPPY_H_ 55 | -------------------------------------------------------------------------------- /tsl/profiler/lib/profiler_factory.h: -------------------------------------------------------------------------------- 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_TSL_PROFILER_LIB_PROFILER_FACTORY_H_ 16 | #define TENSORFLOW_TSL_PROFILER_LIB_PROFILER_FACTORY_H_ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include "tsl/profiler/lib/profiler_interface.h" 23 | #include "tsl/profiler/protobuf/profiler_options.pb.h" 24 | 25 | namespace tsl { 26 | namespace profiler { 27 | 28 | // A ProfilerFactory returns an instance of ProfilerInterface if ProfileOptions 29 | // require it. Otherwise, it might return nullptr. 30 | using ProfilerFactory = std::function( 31 | const tensorflow::ProfileOptions&)>; 32 | 33 | // Registers a profiler factory. Should be invoked at most once per factory. 34 | void RegisterProfilerFactory(ProfilerFactory factory); 35 | 36 | // Invokes all registered profiler factories with the given options, and 37 | // returns the instantiated (non-null) profiler interfaces. 38 | std::vector> CreateProfilers( 39 | const tensorflow::ProfileOptions& options); 40 | 41 | // For testing only. 42 | void ClearRegisteredProfilersForTest(); 43 | 44 | } // namespace profiler 45 | } // namespace tsl 46 | 47 | #endif // TENSORFLOW_TSL_PROFILER_LIB_PROFILER_FACTORY_H_ 48 | -------------------------------------------------------------------------------- /tsl/profiler/lib/profiler_collection.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 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 | #include "tsl/profiler/lib/profiler_collection.h" 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | #include "absl/status/status.h" 22 | #include "tsl/profiler/lib/profiler_interface.h" 23 | #include "tsl/profiler/protobuf/xplane.pb.h" 24 | 25 | namespace tsl { 26 | namespace profiler { 27 | 28 | ProfilerCollection::ProfilerCollection( 29 | std::vector> profilers) 30 | : profilers_(std::move(profilers)) {} 31 | 32 | absl::Status ProfilerCollection::Start() { 33 | absl::Status status; 34 | for (auto& profiler : profilers_) { 35 | status.Update(profiler->Start()); 36 | } 37 | return status; 38 | } 39 | 40 | absl::Status ProfilerCollection::Stop() { 41 | absl::Status status; 42 | for (auto& profiler : profilers_) { 43 | status.Update(profiler->Stop()); 44 | } 45 | return status; 46 | } 47 | 48 | absl::Status ProfilerCollection::CollectData( 49 | tensorflow::profiler::XSpace* space) { 50 | absl::Status status; 51 | for (auto& profiler : profilers_) { 52 | status.Update(profiler->CollectData(space)); 53 | } 54 | profilers_.clear(); // data has been collected 55 | return status; 56 | } 57 | 58 | } // namespace profiler 59 | } // namespace tsl 60 | -------------------------------------------------------------------------------- /tsl/platform/setround.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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_TSL_PLATFORM_SETROUND_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_SETROUND_H_ 18 | 19 | #if defined(__ANDROID_API__) && (__ANDROID_API__ < 21) 20 | // The header is broken pre-API 21 for several NDK releases. 21 | #define TF_BROKEN_CFENV 22 | #endif 23 | 24 | #if defined(TF_BROKEN_CFENV) 25 | #include // NOLINT 26 | #else 27 | #include // NOLINT 28 | #endif 29 | 30 | #include "xla/tsl/platform/macros.h" 31 | 32 | namespace tsl { 33 | namespace port { 34 | 35 | // While this class is active, floating point rounding mode is set to the given 36 | // mode. The mode can be one of the modes defined in , i.e. FE_DOWNWARD, 37 | // FE_TONEAREST, FE_TOWARDZERO, or FE_UPWARD. The destructor restores the 38 | // original rounding mode if it could be determined. If the original rounding 39 | // mode could not be determined, the destructor sets it to FE_TONEAREST. 40 | class ScopedSetRound { 41 | public: 42 | ScopedSetRound(int mode); 43 | ~ScopedSetRound(); 44 | 45 | private: 46 | int original_mode_; 47 | 48 | ScopedSetRound(const ScopedSetRound&) = delete; 49 | void operator=(const ScopedSetRound&) = delete; 50 | }; 51 | 52 | } // namespace port 53 | } // namespace tsl 54 | 55 | #endif // TENSORFLOW_TSL_PLATFORM_SETROUND_H_ 56 | -------------------------------------------------------------------------------- /tsl/platform/cuda_root_path.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2016 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_TSL_PLATFORM_CUDA_ROOT_PATH_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_CUDA_ROOT_PATH_H_ 18 | 19 | #include 20 | #include 21 | 22 | namespace tsl { 23 | 24 | // Returns, in order of preference, potential locations of the root directory of 25 | // the CUDA SDK, which contains sub-folders such as bin, lib64, and nvvm. 26 | std::vector CandidateCudaRoots(); 27 | 28 | // A convenient wrapper for CandidateCudaRoots, which allows supplying a 29 | // preferred location (inserted first in the output vector), and a flag whether 30 | // the current working directory should be searched (inserted last). 31 | inline std::vector CandidateCudaRoots( 32 | std::string preferred_location, bool use_working_directory = true) { 33 | std::vector candidates = CandidateCudaRoots(); 34 | if (!preferred_location.empty()) { 35 | candidates.insert(candidates.begin(), preferred_location); 36 | } 37 | 38 | // "." is our last resort, even though it probably won't work. 39 | candidates.push_back("."); 40 | 41 | return candidates; 42 | } 43 | 44 | // Returns true if we should prefer ptxas from PATH. 45 | bool PreferPtxasFromPath(); 46 | 47 | } // namespace tsl 48 | 49 | #endif // TENSORFLOW_TSL_PLATFORM_CUDA_ROOT_PATH_H_ 50 | -------------------------------------------------------------------------------- /tsl/platform/abi.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 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 | #include "tsl/platform/abi.h" 17 | 18 | #include "xla/tsl/platform/types.h" 19 | 20 | #if defined(_MSC_VER) 21 | #include 22 | #include 23 | #else 24 | #include 25 | #include 26 | #endif 27 | 28 | #include 29 | #include 30 | 31 | #if defined(_MSC_VER) 32 | 33 | extern "C" char* __unDName(char* output_string, const char* name, 34 | int max_string_length, void* (*p_alloc)(std::size_t), 35 | void (*p_free)(void*), unsigned short disable_flags); 36 | 37 | #endif // defined(_MSC_VER) 38 | 39 | namespace tsl { 40 | namespace port { 41 | 42 | string MaybeAbiDemangle(const char* name) { 43 | #if defined(_MSC_VER) 44 | std::unique_ptr demangled{__unDName(nullptr, name, 0, std::malloc, 45 | std::free, 46 | static_cast(0))}; 47 | 48 | return string(demangled.get() != nullptr ? demangled.get() : name); 49 | #else 50 | int status = 0; 51 | std::unique_ptr res{ 52 | abi::__cxa_demangle(name, nullptr, nullptr, &status), std::free}; 53 | return (status == 0) ? res.get() : name; 54 | #endif 55 | } 56 | 57 | } // namespace port 58 | } // namespace tsl 59 | -------------------------------------------------------------------------------- /tsl/platform/host_info.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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_TSL_PLATFORM_HOST_INFO_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_HOST_INFO_H_ 18 | 19 | #include 20 | 21 | #include "xla/tsl/platform/types.h" 22 | 23 | namespace tsl { 24 | namespace port { 25 | 26 | // Statistical data of IO operations performed by the job. 27 | struct IOStatistics { 28 | struct Distribution { 29 | uint64_t count = 0; 30 | double mean = 0.0; 31 | double std_dev = 0.0; 32 | }; 33 | // Distribution of round trip IO latency in microseconds. 34 | Distribution roundtrip_latency_usec; 35 | // Distribution of data received by IO reads in bytes. 36 | Distribution response_bytes; 37 | }; 38 | 39 | // Return the hostname of the machine on which this process is running. 40 | string Hostname(); 41 | 42 | // Return the job name as a string if it exists, otherwise return an empty 43 | // string. 44 | string JobName(); 45 | 46 | // Returns the Borg job UID as an int64_t if it exists. Otherwise return -1. 47 | int64_t JobUid(); 48 | 49 | // Returns the Borg task ID as an int64_t if it exists. Otherwise return -1. 50 | int64_t TaskId(); 51 | 52 | // Retrieves the host file read statistics. 53 | IOStatistics GetIOStatistics(); 54 | 55 | } // namespace port 56 | } // namespace tsl 57 | 58 | #endif // TENSORFLOW_TSL_PLATFORM_HOST_INFO_H_ 59 | -------------------------------------------------------------------------------- /tsl/platform/stringpiece_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2016 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 | #include "tsl/platform/stringpiece.h" 17 | 18 | #include 19 | 20 | #include "xla/tsl/platform/test.h" 21 | 22 | namespace tsl { 23 | 24 | TEST(StringPiece, Ctor) { 25 | { 26 | // const char* without size. 27 | const char* hello = "hello"; 28 | absl::string_view s20(hello); 29 | EXPECT_TRUE(s20.data() == hello); 30 | EXPECT_EQ(5, s20.size()); 31 | 32 | // const char* with size. 33 | absl::string_view s21(hello, 4); 34 | EXPECT_TRUE(s21.data() == hello); 35 | EXPECT_EQ(4, s21.size()); 36 | 37 | // Not recommended, but valid C++ 38 | absl::string_view s22(hello, 6); 39 | EXPECT_TRUE(s22.data() == hello); 40 | EXPECT_EQ(6, s22.size()); 41 | } 42 | 43 | { 44 | string hola = "hola"; 45 | absl::string_view s30(hola); 46 | EXPECT_TRUE(s30.data() == hola.data()); 47 | EXPECT_EQ(4, s30.size()); 48 | 49 | // std::string with embedded '\0'. 50 | hola.push_back('\0'); 51 | hola.append("h2"); 52 | hola.push_back('\0'); 53 | absl::string_view s31(hola); 54 | EXPECT_TRUE(s31.data() == hola.data()); 55 | EXPECT_EQ(8, s31.size()); 56 | } 57 | } 58 | 59 | TEST(StringPiece, ConversionToString) { 60 | EXPECT_EQ("", string(absl::string_view(""))); 61 | EXPECT_EQ("foo", string(absl::string_view("foo"))); 62 | } 63 | 64 | } // namespace tsl 65 | -------------------------------------------------------------------------------- /tsl/platform/protobuf_util.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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 | #include 17 | 18 | #include "tsl/platform/protobuf.h" 19 | 20 | namespace tsl { 21 | 22 | bool ParseProtoUnlimited(protobuf::MessageLite* proto, 23 | const string& serialized) { 24 | return proto->ParseFromString(serialized); 25 | } 26 | 27 | bool ParseProtoUnlimited(protobuf::MessageLite* proto, const void* serialized, 28 | size_t size) { 29 | return proto->ParseFromArray(serialized, size); 30 | } 31 | 32 | std::string LegacyUnredactedDebugString(const tsl::protobuf::Message& message) { 33 | std::string debug_string; 34 | tsl::protobuf::TextFormat::Printer printer; 35 | printer.SetExpandAny(true); 36 | 37 | printer.PrintToString(message, &debug_string); 38 | return debug_string; 39 | } 40 | 41 | std::string LegacyUnredactedDebugString( 42 | const tsl::protobuf::MessageLite& message) { 43 | return message.DebugString(); 44 | } 45 | 46 | std::string LegacyUnredactedShortDebugString( 47 | const tsl::protobuf::Message& message) { 48 | std::string debug_string; 49 | tsl::protobuf::TextFormat::Printer printer; 50 | printer.SetSingleLineMode(true); 51 | printer.SetExpandAny(true); 52 | 53 | printer.PrintToString(message, &debug_string); 54 | if (!debug_string.empty() && debug_string.back() == ' ') { 55 | debug_string.pop_back(); 56 | } 57 | return debug_string; 58 | } 59 | 60 | } // namespace tsl 61 | -------------------------------------------------------------------------------- /tsl/profiler/lib/context_types.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 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_TSL_PROFILER_LIB_CONTEXT_TYPES_H_ 16 | #define TENSORFLOW_TSL_PROFILER_LIB_CONTEXT_TYPES_H_ 17 | 18 | #include 19 | 20 | namespace tsl { 21 | namespace profiler { 22 | 23 | // Note: Please add new context type after all existing ones. 24 | enum class ContextType : int { 25 | kGeneric = 0, 26 | kLegacy, 27 | kTfExecutor, 28 | kTfrtExecutor, 29 | kSharedBatchScheduler, 30 | kPjRt, 31 | kAdaptiveSharedBatchScheduler, 32 | kTfrtTpuRuntime, 33 | kTpuEmbeddingEngine, 34 | kGpuLaunch, 35 | kBatcher, 36 | kTpuStream, 37 | kTpuLaunch, 38 | kPathwaysExecutor, 39 | kPjrtLibraryCall, 40 | kThreadpoolEvent, 41 | kJaxServingExecutor, 42 | kScOffload, 43 | kLastContextType = ContextType::kScOffload, 44 | }; 45 | 46 | // In XFlow we encode context type as flow category as 6 bits. 47 | static_assert(static_cast(ContextType::kLastContextType) < 64, 48 | "Should have less than 64 categories."); 49 | 50 | const char* GetContextTypeString(ContextType context_type); 51 | 52 | inline ContextType GetSafeContextType(uint32_t context_type) { 53 | if (context_type > static_cast(ContextType::kLastContextType)) { 54 | return ContextType::kGeneric; 55 | } 56 | return static_cast(context_type); 57 | } 58 | 59 | } // namespace profiler 60 | } // namespace tsl 61 | 62 | #endif // TENSORFLOW_TSL_PROFILER_LIB_CONTEXT_TYPES_H_ 63 | -------------------------------------------------------------------------------- /tsl/platform/human_readable_json.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 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_TSL_PLATFORM_HUMAN_READABLE_JSON_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_HUMAN_READABLE_JSON_H_ 18 | 19 | #include 20 | 21 | #include "absl/status/status.h" 22 | #include "absl/status/statusor.h" 23 | #include "xla/tsl/platform/types.h" 24 | #include "tsl/platform/protobuf.h" 25 | 26 | namespace tsl { 27 | 28 | // Converts a proto to a JSON-like string that's meant to be human-readable 29 | // but still machine-parseable. 30 | // 31 | // This string may not be strictly JSON-compliant, but it must be parsable by 32 | // HumanReadableJSONToProto. 33 | // 34 | // When ignore_accuracy_loss = true, this function may ignore JavaScript 35 | // accuracy loss with large integers. 36 | absl::StatusOr ProtoToHumanReadableJson( 37 | const protobuf::Message& proto, bool ignore_accuracy_loss); 38 | absl::StatusOr ProtoToHumanReadableJson( 39 | const protobuf::MessageLite& proto, bool ignore_accuracy_loss); 40 | 41 | // Converts a string produced by ProtoToHumanReadableJSON to a protobuf. Not 42 | // guaranteed to work for general JSON. 43 | absl::Status HumanReadableJsonToProto(const string& str, 44 | protobuf::Message* proto); 45 | absl::Status HumanReadableJsonToProto(const string& str, 46 | protobuf::MessageLite* proto); 47 | 48 | } // namespace tsl 49 | 50 | #endif // TENSORFLOW_TSL_PLATFORM_HUMAN_READABLE_JSON_H_ 51 | -------------------------------------------------------------------------------- /tsl/profiler/lib/context_types.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 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 | #include "tsl/profiler/lib/context_types.h" 16 | 17 | namespace tsl { 18 | namespace profiler { 19 | 20 | const char* GetContextTypeString(ContextType context_type) { 21 | switch (context_type) { 22 | case ContextType::kGeneric: 23 | case ContextType::kLegacy: 24 | return ""; 25 | case ContextType::kTfExecutor: 26 | return "tf_exec"; 27 | case ContextType::kTfrtExecutor: 28 | return "tfrt_exec"; 29 | case ContextType::kSharedBatchScheduler: 30 | return "batch_sched"; 31 | case ContextType::kPjRt: 32 | return "PjRt"; 33 | case ContextType::kAdaptiveSharedBatchScheduler: 34 | return "as_batch_sched"; 35 | case ContextType::kTfrtTpuRuntime: 36 | return "tfrt_rt"; 37 | case ContextType::kTpuEmbeddingEngine: 38 | return "tpu_embed"; 39 | case ContextType::kGpuLaunch: 40 | return "gpu_launch"; 41 | case ContextType::kBatcher: 42 | return "batcher"; 43 | case ContextType::kTpuStream: 44 | return "tpu_stream"; 45 | case ContextType::kTpuLaunch: 46 | return "tpu_launch"; 47 | case ContextType::kPathwaysExecutor: 48 | return "pathways_exec"; 49 | case ContextType::kPjrtLibraryCall: 50 | return "pjrt_library_call"; 51 | case ContextType::kThreadpoolEvent: 52 | return "threadpool_event"; 53 | case ContextType::kJaxServingExecutor: 54 | return "jax_serving"; 55 | case ContextType::kScOffload: 56 | return "sparsecore_offload"; 57 | } 58 | } 59 | 60 | } // namespace profiler 61 | } // namespace tsl 62 | -------------------------------------------------------------------------------- /tsl/platform/protobuf.cc: -------------------------------------------------------------------------------- 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 | #include "tsl/platform/protobuf.h" 17 | 18 | namespace tsl { 19 | 20 | const char* kProtobufInt64Typename = "::tensorflow::protobuf_int64"; 21 | const char* kProtobufUint64Typename = "::tensorflow::protobuf_uint64"; 22 | 23 | TStringOutputStream::TStringOutputStream(tstring* target) : target_(target) {} 24 | 25 | bool TStringOutputStream::Next(void** data, int* size) { 26 | size_t old_size = target_->size(); 27 | 28 | // Grow the string. 29 | if (old_size < target_->capacity()) { 30 | // Resize the string to match its capacity, since we can get away 31 | // without a memory allocation this way. 32 | target_->resize_uninitialized(target_->capacity()); 33 | } else { 34 | // Size has reached capacity, try to double the size. 35 | if (old_size > std::numeric_limits::max() / 2) { 36 | // Can not double the size otherwise it is going to cause integer 37 | // overflow in the expression below: old_size * 2 "; 38 | return false; 39 | } 40 | // Double the size, also make sure that the new size is at least 41 | // kMinimumSize. 42 | target_->resize_uninitialized(std::max( 43 | old_size * 2, 44 | (size_t)kMinimumSize + 0)); // "+ 0" works around GCC4 weirdness. 45 | } 46 | 47 | *data = target_->data() + old_size; 48 | *size = target_->size() - old_size; 49 | return true; 50 | } 51 | 52 | void TStringOutputStream::BackUp(int count) { 53 | target_->resize(target_->size() - count); 54 | } 55 | 56 | int64_t TStringOutputStream::ByteCount() const { return target_->size(); } 57 | 58 | } // namespace tsl 59 | -------------------------------------------------------------------------------- /tsl/platform/integral_types_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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 | #include 17 | #include 18 | 19 | #include "xla/tsl/platform/test.h" 20 | #include "xla/tsl/platform/types.h" 21 | 22 | namespace tsl { 23 | namespace { 24 | 25 | TEST(IntegralTypes, Basic) { 26 | EXPECT_EQ(1, sizeof(int8)); 27 | EXPECT_EQ(2, sizeof(int16)); 28 | EXPECT_EQ(4, sizeof(int32)); 29 | EXPECT_EQ(8, sizeof(int64_t)); 30 | 31 | EXPECT_EQ(1, sizeof(uint8)); 32 | EXPECT_EQ(2, sizeof(uint16)); 33 | EXPECT_EQ(4, sizeof(uint32)); 34 | EXPECT_EQ(8, sizeof(uint64)); 35 | } 36 | 37 | TEST(IntegralTypes, MinAndMaxConstants) { 38 | EXPECT_EQ(static_cast(std::numeric_limits::min()), 39 | static_cast(std::numeric_limits::max()) + 1); 40 | EXPECT_EQ(static_cast(std::numeric_limits::min()), 41 | static_cast(std::numeric_limits::max()) + 1); 42 | EXPECT_EQ(static_cast(std::numeric_limits::min()), 43 | static_cast(std::numeric_limits::max()) + 1); 44 | EXPECT_EQ(static_cast(std::numeric_limits::min()), 45 | static_cast(std::numeric_limits::max()) + 1); 46 | 47 | EXPECT_EQ(0, static_cast(std::numeric_limits::max() + 1)); 48 | EXPECT_EQ(0, static_cast(std::numeric_limits::max() + 1)); 49 | EXPECT_EQ(0, static_cast(std::numeric_limits::max() + 1)); 50 | EXPECT_EQ(0, static_cast(std::numeric_limits::max() + 1)); 51 | } 52 | 53 | } // namespace 54 | } // namespace tsl 55 | -------------------------------------------------------------------------------- /tsl/profiler/lib/profiler_factory.cc: -------------------------------------------------------------------------------- 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 | #include "tsl/profiler/lib/profiler_factory.h" 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | #include "absl/base/const_init.h" 22 | #include "absl/synchronization/mutex.h" 23 | #include "tsl/profiler/lib/profiler_controller.h" 24 | #include "tsl/profiler/lib/profiler_interface.h" 25 | #include "tsl/profiler/protobuf/profiler_options.pb.h" 26 | 27 | namespace tsl { 28 | namespace profiler { 29 | namespace { 30 | 31 | absl::Mutex mu(absl::kConstInit); 32 | 33 | std::vector* GetFactories() { 34 | static auto factories = new std::vector(); 35 | return factories; 36 | } 37 | 38 | } // namespace 39 | 40 | void RegisterProfilerFactory(ProfilerFactory factory) { 41 | absl::MutexLock lock(mu); 42 | GetFactories()->push_back(std::move(factory)); 43 | } 44 | 45 | std::vector> CreateProfilers( 46 | const tensorflow::ProfileOptions& options) { 47 | std::vector> result; 48 | absl::MutexLock lock(mu); 49 | for (const auto& factory : *GetFactories()) { 50 | auto profiler = factory(options); 51 | // A factory might return nullptr based on options. 52 | if (profiler == nullptr) continue; 53 | result.emplace_back( 54 | std::make_unique(std::move(profiler))); 55 | } 56 | return result; 57 | } 58 | 59 | void ClearRegisteredProfilersForTest() { 60 | absl::MutexLock lock(mu); 61 | GetFactories()->clear(); 62 | } 63 | 64 | } // namespace profiler 65 | } // namespace tsl 66 | -------------------------------------------------------------------------------- /tsl/platform/numa_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 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 | #include "tsl/platform/numa.h" 17 | 18 | #include "xla/tsl/platform/logging.h" 19 | #include "xla/tsl/platform/test.h" 20 | 21 | namespace tsl { 22 | namespace internal { 23 | 24 | TEST(Numa, NumNodes) { 25 | if (port::NUMAEnabled()) { 26 | EXPECT_GE(port::NUMANumNodes(), 1); 27 | } 28 | } 29 | 30 | TEST(Numa, Malloc) { 31 | if (port::NUMAEnabled()) { 32 | int num_nodes = port::NUMANumNodes(); 33 | for (int request_node = 0; request_node < num_nodes; ++request_node) { 34 | void* ptr = port::NUMAMalloc(request_node, 8, 0); 35 | EXPECT_NE(ptr, nullptr); 36 | // Affinity cannot be tested until page is touched, so save a value. 37 | *(reinterpret_cast(ptr)) = 0; 38 | int affinity_node = port::NUMAGetMemAffinity(ptr); 39 | EXPECT_EQ(affinity_node, request_node); 40 | port::NUMAFree(ptr, 8); 41 | } 42 | } 43 | } 44 | 45 | TEST(Numa, SetNodeAffinity) { 46 | // NOTE(tucker): This test is not reliable when executed under tap because 47 | // the virtual machine may not have access to all of the available NUMA 48 | // nodes. Not sure what to do about that. 49 | EXPECT_EQ(-1, port::NUMAGetThreadNodeAffinity()); 50 | if (port::NUMAEnabled()) { 51 | int num_nodes = port::NUMANumNodes(); 52 | for (int request_node = 0; request_node < num_nodes; ++request_node) { 53 | port::NUMASetThreadNodeAffinity(request_node); 54 | int affinity_node = port::NUMAGetThreadNodeAffinity(); 55 | EXPECT_EQ(affinity_node, request_node); 56 | } 57 | } 58 | } 59 | 60 | } // namespace internal 61 | } // namespace tsl 62 | -------------------------------------------------------------------------------- /tsl/profiler/lib/profiler_controller.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 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_TSL_PROFILER_LIB_PROFILER_CONTROLLER_H_ 16 | #define TENSORFLOW_TSL_PROFILER_LIB_PROFILER_CONTROLLER_H_ 17 | 18 | #include 19 | 20 | #include "absl/status/status.h" 21 | #include "xla/tsl/platform/status.h" 22 | #include "tsl/profiler/lib/profiler_interface.h" 23 | #include "tsl/profiler/protobuf/xplane.pb.h" 24 | 25 | namespace tsl { 26 | namespace profiler { 27 | 28 | // Decorator for xprof profiler plugins. 29 | // 30 | // Tracks that calls to the underlying profiler interface functions are made 31 | // in the expected order: Start, Stop and CollectData. Making the calls 32 | // in a different order causes them to be aborted. 33 | // 34 | // Calls made in the right order will be aborted if one of the calls to the 35 | // decorated profiler interface fails, and no more calls will be forwarded to 36 | // the decorated profiler. 37 | class ProfilerController : public ProfilerInterface { 38 | public: 39 | explicit ProfilerController(std::unique_ptr profiler); 40 | ~ProfilerController() override; 41 | 42 | absl::Status Start() override; 43 | 44 | absl::Status Stop() override; 45 | 46 | absl::Status CollectData(tensorflow::profiler::XSpace* space) override; 47 | 48 | private: 49 | enum class ProfilerState { 50 | kInit = 0, 51 | kStart = 1, 52 | kStop = 2, 53 | kCollectData = 3, 54 | }; 55 | 56 | ProfilerState state_ = ProfilerState::kInit; 57 | std::unique_ptr profiler_; 58 | absl::Status status_; // result of calls to profiler_ 59 | }; 60 | 61 | } // namespace profiler 62 | } // namespace tsl 63 | 64 | #endif // TENSORFLOW_TSL_PROFILER_LIB_PROFILER_CONTROLLER_H_ 65 | -------------------------------------------------------------------------------- /tsl/platform/numa.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 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_TSL_PLATFORM_NUMA_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_NUMA_H_ 18 | 19 | #include 20 | 21 | namespace tsl { 22 | namespace port { 23 | 24 | // Returns true iff NUMA functions are supported. 25 | bool NUMAEnabled(); 26 | 27 | // Returns the number of NUMA nodes present with respect to CPU operations. 28 | // Typically this will be the number of sockets where some RAM has greater 29 | // affinity with one socket than another. 30 | int NUMANumNodes(); 31 | 32 | static const int kNUMANoAffinity = -1; 33 | 34 | // If possible sets affinity of the current thread to the specified NUMA node. 35 | // If node == kNUMANoAffinity removes affinity to any particular node. 36 | void NUMASetThreadNodeAffinity(int node); 37 | 38 | // Returns NUMA node affinity of the current thread, kNUMANoAffinity if none. 39 | int NUMAGetThreadNodeAffinity(); 40 | 41 | // Like AlignedMalloc, but allocates memory with affinity to the specified NUMA 42 | // node. 43 | // 44 | // Notes: 45 | // 1. node must be >= 0 and < NUMANumNodes. 46 | // 1. minimum_alignment must a factor of system page size, the memory 47 | // returned will be page-aligned. 48 | // 2. This function is likely significantly slower than AlignedMalloc 49 | // and should not be used for lots of small allocations. It makes more 50 | // sense as a backing allocator for BFCAllocator, PoolAllocator, or similar. 51 | void* NUMAMalloc(int node, size_t size, int minimum_alignment); 52 | 53 | // Memory allocated by NUMAMalloc must be freed via NUMAFree. 54 | void NUMAFree(void* ptr, size_t size); 55 | 56 | // Returns NUMA node affinity of memory address, kNUMANoAffinity if none. 57 | int NUMAGetMemAffinity(const void* ptr); 58 | 59 | } // namespace port 60 | } // namespace tsl 61 | #endif // TENSORFLOW_TSL_PLATFORM_NUMA_H_ 62 | -------------------------------------------------------------------------------- /tsl/profiler/protobuf/trace_events.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package tsl.profiler; 4 | 5 | option cc_enable_arenas = true; 6 | option java_outer_classname = "TraceEventsProtos"; 7 | option java_multiple_files = true; 8 | option java_package = "org.tensorflow.framework"; 9 | option go_package = "github.com/tensorflow/tensorflow/tensorflow/go/core/core_protos_go_proto"; 10 | 11 | // A 'Trace' contains metadata for the individual traces of a system. 12 | message Trace { 13 | // The devices that this trace has information about. Maps from device_id to 14 | // more data about the specific device. 15 | map devices = 1; 16 | 17 | // All trace events capturing in the profiling period. 18 | repeated TraceEvent trace_events = 4; 19 | } 20 | 21 | // A 'device' is a physical entity in the system and is comprised of several 22 | // resources. 23 | message Device { 24 | // The name of the device. 25 | string name = 1; 26 | 27 | // The id of this device, unique in a single trace. 28 | uint32 device_id = 2; 29 | 30 | // The resources on this device, keyed by resource_id; 31 | map resources = 3; 32 | } 33 | 34 | // A 'resource' generally is a specific computation component on a device. These 35 | // can range from threads on CPUs to specific arithmetic units on hardware 36 | // devices. 37 | message Resource { 38 | // The name of the resource. 39 | string name = 1; 40 | 41 | // The id of the resource. Unique within a device. 42 | uint32 resource_id = 2; 43 | 44 | // The sort index of the resource. Resources within a device are ordered by 45 | // this value. if absent, use resource id as sort index. 46 | uint32 sort_index = 3; 47 | } 48 | 49 | message TraceEvent { 50 | // The id of the device that this event occurred on. The full dataset should 51 | // have this device present in the Trace object. 52 | uint32 device_id = 1; 53 | 54 | // The id of the resource that this event occurred on. The full dataset should 55 | // have this resource present in the Device object of the Trace object. A 56 | // resource_id is unique on a specific device, but not necessarily within the 57 | // trace. 58 | uint32 resource_id = 2; 59 | 60 | // The name of this trace event. 61 | string name = 3; 62 | 63 | // The timestamp that this event occurred at (in picos since tracing started). 64 | uint64 timestamp_ps = 9; 65 | 66 | // The duration of the event in picoseconds if applicable. 67 | // Events without duration are called instant events. 68 | uint64 duration_ps = 10; 69 | 70 | // Extra arguments that will be displayed in trace view. 71 | map args = 11; 72 | } 73 | -------------------------------------------------------------------------------- /tsl/platform/raw_coding.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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_TSL_PLATFORM_RAW_CODING_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_RAW_CODING_H_ 18 | 19 | #include 20 | 21 | #include "xla/tsl/platform/byte_order.h" 22 | #include "xla/tsl/platform/types.h" 23 | 24 | namespace tsl { 25 | namespace core { 26 | 27 | // Lower-level versions of Get... that read directly from a character buffer 28 | // without any bounds checking. 29 | 30 | inline uint16 DecodeFixed16(const char* ptr) { 31 | if (port::kLittleEndian) { 32 | // Load the raw bytes 33 | uint16 result; 34 | memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load 35 | return result; 36 | } else { 37 | return ((static_cast(static_cast(ptr[0]))) | 38 | (static_cast(static_cast(ptr[1])) << 8)); 39 | } 40 | } 41 | 42 | inline uint32 DecodeFixed32(const char* ptr) { 43 | if (port::kLittleEndian) { 44 | // Load the raw bytes 45 | uint32 result; 46 | memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load 47 | return result; 48 | } else { 49 | return ((static_cast(static_cast(ptr[0]))) | 50 | (static_cast(static_cast(ptr[1])) << 8) | 51 | (static_cast(static_cast(ptr[2])) << 16) | 52 | (static_cast(static_cast(ptr[3])) << 24)); 53 | } 54 | } 55 | 56 | inline uint64 DecodeFixed64(const char* ptr) { 57 | if (port::kLittleEndian) { 58 | // Load the raw bytes 59 | uint64 result; 60 | memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load 61 | return result; 62 | } else { 63 | uint64 lo = DecodeFixed32(ptr); 64 | uint64 hi = DecodeFixed32(ptr + 4); 65 | return (hi << 32) | lo; 66 | } 67 | } 68 | 69 | } // namespace core 70 | } // namespace tsl 71 | 72 | #endif // TENSORFLOW_TSL_PLATFORM_RAW_CODING_H_ 73 | -------------------------------------------------------------------------------- /tsl/platform/blocking_counter.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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_TSL_PLATFORM_BLOCKING_COUNTER_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_BLOCKING_COUNTER_H_ 18 | 19 | #include 20 | 21 | #include "xla/tsl/platform/logging.h" 22 | #include "tsl/platform/mutex.h" 23 | 24 | namespace tsl { 25 | 26 | class BlockingCounter { 27 | public: 28 | BlockingCounter(int initial_count) 29 | : state_(initial_count << 1), notified_(false) { 30 | CHECK_GE(initial_count, 0); 31 | DCHECK_EQ((initial_count << 1) >> 1, initial_count); 32 | } 33 | 34 | ~BlockingCounter() {} 35 | 36 | inline void DecrementCount() { 37 | unsigned int v = state_.fetch_sub(2, std::memory_order_acq_rel) - 2; 38 | if (v != 1) { 39 | DCHECK_NE(((v + 2) & ~1), 0); 40 | return; // either count has not dropped to 0, or waiter is not waiting 41 | } 42 | mutex_lock l(mu_); 43 | DCHECK(!notified_); 44 | notified_ = true; 45 | cond_var_.notify_all(); 46 | } 47 | 48 | inline void Wait() { 49 | unsigned int v = state_.fetch_or(1, std::memory_order_acq_rel); 50 | if ((v >> 1) == 0) return; 51 | mutex_lock l(mu_); 52 | while (!notified_) { 53 | cond_var_.wait(l); 54 | } 55 | } 56 | // Wait for the specified time, return false iff the count has not dropped to 57 | // zero before the timeout expired. 58 | inline bool WaitFor(std::chrono::milliseconds ms) { 59 | unsigned int v = state_.fetch_or(1, std::memory_order_acq_rel); 60 | if ((v >> 1) == 0) return true; 61 | mutex_lock l(mu_); 62 | while (!notified_) { 63 | const std::cv_status status = cond_var_.wait_for(l, ms); 64 | if (status == std::cv_status::timeout) { 65 | return false; 66 | } 67 | } 68 | return true; 69 | } 70 | 71 | private: 72 | mutex mu_; 73 | condition_variable cond_var_; 74 | std::atomic state_; // low bit is waiter flag 75 | bool notified_; 76 | }; 77 | 78 | } // namespace tsl 79 | 80 | #endif // TENSORFLOW_TSL_PLATFORM_BLOCKING_COUNTER_H_ 81 | -------------------------------------------------------------------------------- /tsl/profiler/lib/profiler_lock.h: -------------------------------------------------------------------------------- 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_TSL_PROFILER_LIB_PROFILER_LOCK_H_ 16 | #define TENSORFLOW_TSL_PROFILER_LIB_PROFILER_LOCK_H_ 17 | 18 | #include 19 | 20 | #include "absl/status/statusor.h" 21 | #include "absl/strings/string_view.h" 22 | #include "xla/tsl/platform/statusor.h" 23 | 24 | namespace tsl { 25 | namespace profiler { 26 | 27 | constexpr absl::string_view kProfilerLockContention = 28 | "Another profiling session active."; 29 | 30 | // Handle for the profiler lock. At most one instance of this class, the 31 | // "active" instance, owns the profiler lock. 32 | class ProfilerLock { 33 | public: 34 | // Returns true if the process has active profiling session. 35 | static bool HasActiveSession(); 36 | 37 | // Acquires the profiler lock if no other profiler session is currently 38 | // active. 39 | static absl::StatusOr Acquire(); 40 | 41 | // Default constructor creates an inactive instance. 42 | ProfilerLock() = default; 43 | 44 | // Non-copyable. 45 | ProfilerLock(const ProfilerLock&) = delete; 46 | ProfilerLock& operator=(const ProfilerLock&) = delete; 47 | 48 | // Movable. 49 | ProfilerLock(ProfilerLock&& other) noexcept 50 | : active_(std::exchange(other.active_, false)) {} 51 | ProfilerLock& operator=(ProfilerLock&& other) noexcept { 52 | active_ = std::exchange(other.active_, false); 53 | return *this; 54 | } 55 | 56 | ~ProfilerLock() { ReleaseIfActive(); } 57 | 58 | // Allow creating another active instance. 59 | void ReleaseIfActive(); 60 | 61 | // Returns true if this is the active instance. 62 | bool Active() const { return active_; } 63 | 64 | private: 65 | // Explicit constructor allows creating an active instance, private so it can 66 | // only be called by Acquire. 67 | explicit ProfilerLock(bool active) : active_(active) {} 68 | 69 | bool active_ = false; 70 | }; 71 | 72 | } // namespace profiler 73 | } // namespace tsl 74 | 75 | #endif // TENSORFLOW_TSL_PROFILER_LIB_PROFILER_LOCK_H_ 76 | -------------------------------------------------------------------------------- /tsl/profiler/lib/profiler_lock.cc: -------------------------------------------------------------------------------- 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 | #include "tsl/profiler/lib/profiler_lock.h" 16 | 17 | #include 18 | 19 | #include "absl/status/statusor.h" 20 | #include "xla/tsl/platform/errors.h" 21 | #include "xla/tsl/platform/macros.h" 22 | #include "xla/tsl/util/env_var.h" 23 | 24 | namespace tsl { 25 | namespace profiler { 26 | namespace { 27 | 28 | // Track whether there's an active profiler session. 29 | // Prevents another profiler session from creating ProfilerInterface(s). 30 | std::atomic g_session_active = ATOMIC_VAR_INIT(0); 31 | 32 | // g_session_active implementation must be lock-free for faster execution of 33 | // the ProfilerLock API. 34 | static_assert(ATOMIC_INT_LOCK_FREE == 2, "Assumed atomic was lock free"); 35 | 36 | } // namespace 37 | 38 | /*static*/ bool ProfilerLock::HasActiveSession() { 39 | return g_session_active.load(std::memory_order_relaxed) != 0; 40 | } 41 | 42 | /*static*/ absl::StatusOr ProfilerLock::Acquire() { 43 | // Use environment variable to permanently lock the profiler. 44 | // This allows running TensorFlow under an external profiling tool with all 45 | // built-in profiling disabled. 46 | static bool tf_profiler_disabled = [] { 47 | bool disabled = false; 48 | ReadBoolFromEnvVar("TF_DISABLE_PROFILING", false, &disabled).IgnoreError(); 49 | return disabled; 50 | }(); 51 | if (TF_PREDICT_FALSE(tf_profiler_disabled)) { 52 | return errors::AlreadyExists( 53 | "TensorFlow Profiler is permanently disabled by env var " 54 | "TF_DISABLE_PROFILING."); 55 | } 56 | int already_active = g_session_active.exchange(1, std::memory_order_acq_rel); 57 | if (already_active) { 58 | return errors::AlreadyExists(kProfilerLockContention); 59 | } 60 | return ProfilerLock(/*active=*/true); 61 | } 62 | 63 | void ProfilerLock::ReleaseIfActive() { 64 | if (active_) { 65 | g_session_active.store(0, std::memory_order_release); 66 | active_ = false; 67 | } 68 | } 69 | 70 | } // namespace profiler 71 | } // namespace tsl 72 | -------------------------------------------------------------------------------- /.github/workflows/scorecards-analysis.yml: -------------------------------------------------------------------------------- 1 | # Copyright 2023 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 | name: Scorecards supply-chain security 17 | on: 18 | # Only the default branch is supported. 19 | branch_protection_rule: 20 | schedule: 21 | - cron: '44 14 * * 5' 22 | push: 23 | branches: [ "main" ] 24 | 25 | # Declare default permissions as read only. 26 | permissions: read-all 27 | 28 | jobs: 29 | analysis: 30 | if: github.repository == 'google/tsl' # Don't do this in forks 31 | name: Scorecards analysis 32 | runs-on: ubuntu-latest 33 | permissions: 34 | # Needed to upload the results to code-scanning dashboard. 35 | security-events: write 36 | id-token: write 37 | 38 | steps: 39 | - name: "Checkout code" 40 | uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0 41 | with: 42 | persist-credentials: false 43 | 44 | - name: "Run analysis" 45 | uses: ossf/scorecard-action@15c10fcf1cf912bd22260bfec67569a359ab87da # v2.1.1 46 | with: 47 | results_file: results.sarif 48 | results_format: sarif 49 | # Publish the results to enable scorecard badges. For more details, see 50 | # https://github.com/ossf/scorecard-action#publishing-results. 51 | # For private repositories, `publish_results` will automatically be set to `false`, 52 | # regardless of the value entered here. 53 | publish_results: true 54 | 55 | # Upload the results as artifacts (optional). 56 | - name: "Upload artifact" 57 | uses: actions/upload-artifact@83fd05a356d7e2593de66fc9913b3002723633cb # v3.1.1 58 | with: 59 | name: SARIF file 60 | path: results.sarif 61 | retention-days: 5 62 | 63 | # Upload the results to GitHub's code scanning dashboard. 64 | - name: "Upload to code-scanning" 65 | uses: github/codeql-action/upload-sarif@896079047b4bb059ba6f150a5d87d47dde99e6e5 # v2.11.6 66 | with: 67 | sarif_file: results.sarif 68 | -------------------------------------------------------------------------------- /tsl/platform/base64.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2016 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_TSL_PLATFORM_BASE64_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_BASE64_H_ 18 | 19 | #include 20 | 21 | #include "absl/status/status.h" 22 | #include "absl/strings/string_view.h" 23 | #include "xla/tsl/platform/status.h" 24 | #include "tsl/platform/stringpiece.h" 25 | 26 | namespace tsl { 27 | 28 | /// \brief Converts data into web-safe base64 encoding. 29 | /// 30 | /// See https://en.wikipedia.org/wiki/Base64 31 | template 32 | absl::Status Base64Encode(absl::string_view source, bool with_padding, 33 | T* encoded); 34 | template 35 | absl::Status Base64Encode(absl::string_view source, 36 | T* encoded); // with_padding=false. 37 | 38 | /// \brief Converts data from web-safe base64 encoding. 39 | /// 40 | /// See https://en.wikipedia.org/wiki/Base64 41 | template 42 | absl::Status Base64Decode(absl::string_view data, T* decoded); 43 | 44 | // Explicit instantiations defined in base64.cc. 45 | extern template Status Base64Decode(StringPiece data, 46 | std::string* decoded); 47 | extern template Status Base64Encode(StringPiece source, 48 | std::string* encoded); 49 | extern template Status Base64Encode(StringPiece source, 50 | bool with_padding, 51 | std::string* encoded); 52 | 53 | extern template Status Base64Decode(StringPiece data, 54 | tstring* decoded); 55 | extern template Status Base64Encode(StringPiece source, 56 | tstring* encoded); 57 | extern template Status Base64Encode(StringPiece source, 58 | bool with_padding, 59 | tstring* encoded); 60 | 61 | } // namespace tsl 62 | 63 | #endif // TENSORFLOW_TSL_PLATFORM_BASE64_H_ 64 | -------------------------------------------------------------------------------- /tsl/profiler/lib/profiler_lock_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 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 | #include "tsl/profiler/lib/profiler_lock.h" 16 | 17 | #include 18 | 19 | #include "absl/status/statusor.h" 20 | #include "xla/tsl/platform/test.h" 21 | 22 | namespace tsl { 23 | namespace profiler { 24 | namespace { 25 | 26 | TEST(ProfilerLockTest, DefaultConstructorCreatesInactiveInstance) { 27 | ProfilerLock profiler_lock; 28 | EXPECT_FALSE(profiler_lock.Active()); 29 | } 30 | 31 | TEST(ProfilerLockTest, AcquireAndReleaseExplicitly) { 32 | absl::StatusOr profiler_lock = ProfilerLock::Acquire(); 33 | ASSERT_TRUE(profiler_lock.ok()); 34 | EXPECT_TRUE(profiler_lock->Active()); 35 | profiler_lock->ReleaseIfActive(); 36 | EXPECT_FALSE(profiler_lock->Active()); 37 | } 38 | 39 | TEST(ProfilerLockTest, AcquireAndReleaseOnDestruction) { 40 | absl::StatusOr profiler_lock = ProfilerLock::Acquire(); 41 | ASSERT_TRUE(profiler_lock.ok()); 42 | EXPECT_TRUE(profiler_lock->Active()); 43 | } 44 | 45 | TEST(ProfilerLockTest, ReacquireWithoutReleaseFails) { 46 | absl::StatusOr profiler_lock_1 = ProfilerLock::Acquire(); 47 | absl::StatusOr profiler_lock_2 = ProfilerLock::Acquire(); 48 | ASSERT_TRUE(profiler_lock_1.ok()); 49 | EXPECT_TRUE(profiler_lock_1->Active()); 50 | EXPECT_FALSE(profiler_lock_2.ok()); 51 | } 52 | 53 | TEST(ProfilerLockTest, ReacquireAfterReleaseSucceeds) { 54 | auto profiler_lock_1 = ProfilerLock::Acquire(); 55 | ASSERT_TRUE(profiler_lock_1.ok()); 56 | ASSERT_TRUE(profiler_lock_1->Active()); 57 | profiler_lock_1->ReleaseIfActive(); 58 | ASSERT_FALSE(profiler_lock_1->Active()); 59 | auto profiler_lock_2 = ProfilerLock::Acquire(); 60 | EXPECT_TRUE(profiler_lock_2.ok()); 61 | EXPECT_TRUE(profiler_lock_2->Active()); 62 | } 63 | 64 | TEST(ProfilerLockTest, InactiveAfterMove) { 65 | absl::StatusOr profiler_lock_1 = ProfilerLock::Acquire(); 66 | ASSERT_TRUE(profiler_lock_1.ok()); 67 | ASSERT_TRUE(profiler_lock_1->Active()); 68 | ProfilerLock profiler_lock_2 = std::move(*profiler_lock_1); 69 | EXPECT_FALSE(profiler_lock_1->Active()); 70 | EXPECT_TRUE(profiler_lock_2.Active()); 71 | } 72 | 73 | } // namespace 74 | } // namespace profiler 75 | } // namespace tsl 76 | -------------------------------------------------------------------------------- /tsl/profiler/protobuf/profiler_analysis.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package tensorflow; 4 | 5 | import "tsl/profiler/protobuf/profiler_service.proto"; 6 | 7 | message NewProfileSessionRequest { 8 | ProfileRequest request = 1; 9 | // The place where we will dump profile data. We will normally use 10 | // MODEL_DIR/plugins/profile as the repository root. 11 | string repository_root = 2; 12 | repeated string hosts = 3; // host or host:port, port will be ignored. 13 | string session_id = 4; 14 | } 15 | 16 | message NewProfileSessionResponse { 17 | // Auxiliary error_message. 18 | string error_message = 1; 19 | 20 | // Whether all hosts had returned a empty trace. 21 | bool empty_trace = 2; 22 | } 23 | 24 | message EnumProfileSessionsAndToolsRequest { 25 | string repository_root = 1; 26 | } 27 | 28 | message ProfileSessionInfo { 29 | string session_id = 1; 30 | // Which tool data is available for consumption. 31 | repeated string available_tools = 2; 32 | } 33 | 34 | message EnumProfileSessionsAndToolsResponse { 35 | // Auxiliary error_message. 36 | string error_message = 1; 37 | // If success, the returned sessions information are stored here. 38 | repeated ProfileSessionInfo sessions = 2; 39 | } 40 | 41 | message ProfileSessionDataRequest { 42 | // The place where we will read profile data. We will normally use 43 | // MODEL_DIR/plugins/profile as the repository root. 44 | string repository_root = 1; 45 | string session_id = 2; 46 | // Which host the data is associated. if empty, data from all hosts are 47 | // aggregated. 48 | string host_name = 5; 49 | // Which tool 50 | string tool_name = 3; 51 | // Tool's specific parameters. e.g. TraceViewer's viewport etc 52 | map parameters = 4; 53 | } 54 | 55 | message ProfileSessionDataResponse { 56 | // Auxiliary error_message. 57 | string error_message = 1; 58 | 59 | // Output format. e.g. "json" or "proto" or "blob" 60 | string output_format = 2; 61 | 62 | // TODO(jiesun): figure out whether to put bytes or oneof tool specific proto. 63 | bytes output = 3; 64 | } 65 | //////////////////////////////////////////////////////////////////////////////// 66 | // ProfileAnalysis service provide entry point for profiling TPU and for 67 | // serving profiled data to TensorBoard through GRPC 68 | //////////////////////////////////////////////////////////////////////////////// 69 | service ProfileAnalysis { 70 | // Starts a profiling session, blocks until it completes. 71 | // TPUProfileAnalysis service delegate this to TPUProfiler service. 72 | // Populate the profiled data in repository, then return status to caller. 73 | rpc NewSession(NewProfileSessionRequest) returns (NewProfileSessionResponse) { 74 | } 75 | // Enumerate existing sessions and return available profile tools. 76 | rpc EnumSessions(EnumProfileSessionsAndToolsRequest) 77 | returns (EnumProfileSessionsAndToolsResponse) {} 78 | // Retrieve specific tool's data for specific session. 79 | rpc GetSessionToolData(ProfileSessionDataRequest) 80 | returns (ProfileSessionDataResponse) {} 81 | } 82 | -------------------------------------------------------------------------------- /tsl/profiler/lib/profiler_controller.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2022 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 | #include "tsl/profiler/lib/profiler_controller.h" 16 | 17 | #include 18 | #include 19 | 20 | #include "xla/tsl/platform/errors.h" 21 | #include "xla/tsl/platform/logging.h" 22 | #include "tsl/profiler/lib/profiler_interface.h" 23 | #include "tsl/profiler/protobuf/xplane.pb.h" 24 | 25 | namespace tsl { 26 | namespace profiler { 27 | 28 | ProfilerController::ProfilerController( 29 | std::unique_ptr profiler) 30 | : profiler_(std::move(profiler)) {} 31 | 32 | ProfilerController::~ProfilerController() { 33 | // Ensure a successfully started profiler is stopped. 34 | if (state_ == ProfilerState::kStart && status_.ok()) { 35 | profiler_->Stop().IgnoreError(); 36 | } 37 | } 38 | 39 | absl::Status ProfilerController::Start() { 40 | absl::Status status; 41 | if (state_ == ProfilerState::kInit) { 42 | state_ = ProfilerState::kStart; 43 | if (status_.ok()) { 44 | status = status_ = profiler_->Start(); 45 | } else { 46 | status = errors::Aborted("Previous call returned an error."); 47 | } 48 | } else { 49 | status = errors::Aborted("Start called in the wrong order"); 50 | } 51 | if (!status.ok()) LOG(ERROR) << status; 52 | return status; 53 | } 54 | 55 | absl::Status ProfilerController::Stop() { 56 | absl::Status status; 57 | if (state_ == ProfilerState::kStart) { 58 | state_ = ProfilerState::kStop; 59 | if (status_.ok()) { 60 | status = status_ = profiler_->Stop(); 61 | } else { 62 | status = errors::Aborted("Previous call returned an error."); 63 | } 64 | } else { 65 | status = errors::Aborted("Stop called in the wrong order"); 66 | } 67 | if (!status.ok()) LOG(ERROR) << status; 68 | return status; 69 | } 70 | 71 | absl::Status ProfilerController::CollectData( 72 | tensorflow::profiler::XSpace* space) { 73 | absl::Status status; 74 | if (state_ == ProfilerState::kStop) { 75 | state_ = ProfilerState::kCollectData; 76 | if (status_.ok()) { 77 | status = status_ = profiler_->CollectData(space); 78 | } else { 79 | status = errors::Aborted("Previous call returned an error."); 80 | } 81 | } else { 82 | status = errors::Aborted("CollectData called in the wrong order."); 83 | } 84 | if (!status.ok()) LOG(ERROR) << status; 85 | return status; 86 | } 87 | 88 | } // namespace profiler 89 | } // namespace tsl 90 | -------------------------------------------------------------------------------- /tsl/platform/platform.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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_TSL_PLATFORM_PLATFORM_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_PLATFORM_H_ 18 | 19 | // Set one PLATFORM_* macro and set IS_MOBILE_PLATFORM if the platform is for 20 | // mobile. 21 | 22 | #if !defined(PLATFORM_POSIX) && !defined(PLATFORM_GOOGLE) && \ 23 | !defined(PLATFORM_POSIX_ANDROID) && !defined(PLATFORM_GOOGLE_ANDROID) && \ 24 | !defined(PLATFORM_WINDOWS) 25 | 26 | // Choose which platform we are on. 27 | #if defined(ANDROID) || defined(__ANDROID__) 28 | #define PLATFORM_POSIX_ANDROID 29 | #define IS_MOBILE_PLATFORM 30 | 31 | #elif defined(__APPLE__) 32 | #include "TargetConditionals.h" 33 | #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE 34 | #define PLATFORM_POSIX_IOS 35 | #define IS_MOBILE_PLATFORM 36 | #else 37 | // If no platform specified, use: 38 | #define PLATFORM_POSIX 39 | #endif 40 | 41 | #elif defined(_WIN32) 42 | #define PLATFORM_WINDOWS 43 | 44 | #elif defined(__EMSCRIPTEN__) 45 | #define PLATFORM_PORTABLE_GOOGLE 46 | #define PLATFORM_POSIX 47 | // EMSCRIPTEN builds are considered "mobile" for the sake of portability. 48 | #define IS_MOBILE_PLATFORM 49 | 50 | #elif defined(__TF_CHROMIUMOS__) 51 | #define PLATFORM_PORTABLE_GOOGLE 52 | #define PLATFORM_POSIX 53 | #define PLATFORM_CHROMIUMOS 54 | 55 | #elif defined(__Fuchsia__) 56 | #define PLATFORM_FUCHSIA 57 | // PLATFORM_GOOGLE needs to be defined by default to get the right header 58 | // files. 59 | #define PLATFORM_GOOGLE 60 | 61 | #elif defined(__CHROME__) 62 | #define PLATFORM_POSIX 63 | #define PLATFORM_PORTABLE_GOOGLE 64 | 65 | #else 66 | // If no platform specified, use: 67 | #define PLATFORM_POSIX 68 | 69 | #endif 70 | #endif 71 | 72 | // Look for both gcc/clang and Visual Studio macros indicating we're compiling 73 | // for an x86 device. 74 | #if defined(__x86_64__) || defined(__amd64__) || defined(_M_IX86) || \ 75 | defined(_M_X64) 76 | #define PLATFORM_IS_X86 77 | #endif 78 | 79 | // Check if we are compiling for an arm device. 80 | #if defined(__arm__) || defined(__aarch64__) 81 | #define PLATFORM_IS_ARM 82 | #if defined(__aarch64__) 83 | #define PLATFORM_IS_ARM64 84 | #else 85 | #define PLATFORM_IS_ARM32 86 | #endif 87 | #endif 88 | 89 | #define TSL_IS_IN_OSS 1 90 | 91 | #ifdef __cplusplus 92 | namespace tsl { 93 | // Constant which is false internally and true in open source. 94 | inline constexpr bool kIsOpenSource = TSL_IS_IN_OSS; 95 | } // namespace tsl 96 | #endif 97 | 98 | #endif // TENSORFLOW_TSL_PLATFORM_PLATFORM_H_ 99 | -------------------------------------------------------------------------------- /tsl/platform/coding.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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 | // Endian-neutral encoding: 17 | // * Fixed-length numbers are encoded with least-significant byte first 18 | // * In addition we support variable length "varint" encoding 19 | // * Strings are encoded prefixed by their length in varint format 20 | 21 | #ifndef TENSORFLOW_TSL_PLATFORM_CODING_H_ 22 | #define TENSORFLOW_TSL_PLATFORM_CODING_H_ 23 | 24 | #include "absl/strings/string_view.h" 25 | #include "xla/tsl/platform/types.h" 26 | #include "tsl/platform/stringpiece.h" 27 | #include "tsl/platform/tstring.h" 28 | 29 | namespace tsl { 30 | namespace core { 31 | 32 | // Maximum number of bytes occupied by a varint32. 33 | static const int kMaxVarint32Bytes = 5; 34 | 35 | // Maximum number of bytes occupied by a varint64. 36 | static const int kMaxVarint64Bytes = 10; 37 | 38 | // Lower-level versions of Put... that write directly into a character buffer 39 | // REQUIRES: dst has enough space for the value being written 40 | extern void EncodeFixed16(char* dst, uint16 value); 41 | extern void EncodeFixed32(char* dst, uint32 value); 42 | extern void EncodeFixed64(char* dst, uint64 value); 43 | extern void PutFixed16(string* dst, uint16 value); 44 | extern void PutFixed32(string* dst, uint32 value); 45 | extern void PutFixed64(string* dst, uint64 value); 46 | 47 | extern void PutVarint32(string* dst, uint32 value); 48 | extern void PutVarint64(string* dst, uint64 value); 49 | 50 | extern void PutVarint32(tstring* dst, uint32 value); 51 | extern void PutVarint64(tstring* dst, uint64 value); 52 | 53 | extern bool GetVarint32(absl::string_view* input, uint32* value); 54 | extern bool GetVarint64(absl::string_view* input, uint64* value); 55 | 56 | extern const char* GetVarint32Ptr(const char* p, const char* limit, uint32* v); 57 | extern const char* GetVarint64Ptr(const char* p, const char* limit, uint64* v); 58 | 59 | // Internal routine for use by fallback path of GetVarint32Ptr 60 | extern const char* GetVarint32PtrFallback(const char* p, const char* limit, 61 | uint32* value); 62 | extern const char* GetVarint32Ptr(const char* p, const char* limit, 63 | uint32* value); 64 | extern char* EncodeVarint32(char* dst, uint32 v); 65 | extern char* EncodeVarint64(char* dst, uint64 v); 66 | 67 | // Returns the length of the varint32 or varint64 encoding of "v" 68 | extern int VarintLength(uint64_t v); 69 | 70 | } // namespace core 71 | } // namespace tsl 72 | 73 | #endif // TENSORFLOW_TSL_PLATFORM_CODING_H_ 74 | -------------------------------------------------------------------------------- /tsl/platform/fingerprint_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2016 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 | #include "tsl/platform/fingerprint.h" 17 | 18 | #include 19 | #include 20 | 21 | #include "xla/tsl/platform/test.h" 22 | #include "xla/tsl/platform/types.h" 23 | 24 | namespace tsl { 25 | namespace { 26 | 27 | TEST(Fingerprint64, IsForeverFrozen) { 28 | EXPECT_EQ(15404698994557526151ULL, Fingerprint64("Hello")); 29 | EXPECT_EQ(18308117990299812472ULL, Fingerprint64("World")); 30 | } 31 | 32 | TEST(Fingerprint128, IsForeverFrozen) { 33 | { 34 | const Fprint128 fingerprint = Fingerprint128("Hello"); 35 | EXPECT_EQ(1163506517679092766ULL, fingerprint.low64); 36 | EXPECT_EQ(10829806600034513965ULL, fingerprint.high64); 37 | } 38 | 39 | { 40 | const Fprint128 fingerprint = Fingerprint128("World"); 41 | EXPECT_EQ(14404540403896557767ULL, fingerprint.low64); 42 | EXPECT_EQ(4859093245152058524ULL, fingerprint.high64); 43 | } 44 | } 45 | 46 | TEST(Fingerprint128, Fprint128Hasher) { 47 | // Tests that this compiles: 48 | const std::unordered_set map = {{1, 2}, {3, 4}}; 49 | } 50 | 51 | TEST(FingerprintCat64, IsForeverFrozen) { 52 | EXPECT_EQ(16877292868973613377ULL, 53 | FingerprintCat64(Fingerprint64("Hello"), Fingerprint64("World"))); 54 | // Do not expect commutativity. 55 | EXPECT_EQ(7158413233176775252ULL, 56 | FingerprintCat64(Fingerprint64("World"), Fingerprint64("Hello"))); 57 | } 58 | 59 | // Hashes don't change. 60 | TEST(FingerprintCat64, Idempotence) { 61 | const uint64_t orig = 62 | FingerprintCat64(Fingerprint64("Hello"), Fingerprint64("World")); 63 | EXPECT_EQ(orig, 64 | FingerprintCat64(Fingerprint64("Hello"), Fingerprint64("World"))); 65 | EXPECT_NE(FingerprintCat64(Fingerprint64("Hello"), Fingerprint64("Hi")), 66 | FingerprintCat64(Fingerprint64("Hello"), Fingerprint64("World"))); 67 | 68 | // Go back to the first test data ('orig') and make sure it hasn't changed. 69 | EXPECT_EQ(orig, 70 | FingerprintCat64(Fingerprint64("Hello"), Fingerprint64("World"))); 71 | } 72 | 73 | TEST(Fprint128ToBytes, WorksCorrectly) { 74 | const Fprint128 fprint = {0xCAFEF00DDEADBEEF, 0xC0FFEE123456789A}; 75 | constexpr std::array kExpected = { 76 | '\xca', '\xfe', '\xf0', '\x0d', '\xde', '\xad', '\xbe', '\xef', 77 | '\xc0', '\xff', '\xee', '\x12', '\x34', '\x56', '\x78', '\x9a', 78 | }; 79 | EXPECT_EQ(Fprint128ToBytes(fprint), kExpected); 80 | } 81 | 82 | } // namespace 83 | } // namespace tsl 84 | -------------------------------------------------------------------------------- /tsl/platform/retrying_utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 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_TSL_PLATFORM_RETRYING_UTILS_H_ 16 | #define TENSORFLOW_TSL_PLATFORM_RETRYING_UTILS_H_ 17 | 18 | #include 19 | 20 | #include "absl/time/time.h" 21 | #include "xla/tsl/platform/status.h" 22 | 23 | namespace tsl { 24 | 25 | // Default time before reporting failure: ~100 seconds. 26 | struct RetryConfig { 27 | RetryConfig(int64_t init_delay_time_us = 100 * 1000, 28 | int64_t max_delay_time_us = 32 * 1000 * 1000, 29 | int max_retries = 10) { 30 | this->init_delay_time_us = init_delay_time_us; 31 | this->max_delay_time_us = max_delay_time_us; 32 | this->max_retries = max_retries; 33 | } 34 | 35 | // In case of failure, every call will be retried max_retries times. 36 | int max_retries; 37 | 38 | // Initial backoff time 39 | int64_t init_delay_time_us; 40 | 41 | // Maximum backoff time in microseconds. 42 | int64_t max_delay_time_us; 43 | }; 44 | 45 | class RetryingUtils { 46 | public: 47 | /// \brief Retries the function in case of failure with exponential backoff. 48 | /// 49 | /// The provided callback is retried with an exponential backoff until it 50 | /// returns OK or a non-retriable error status. 51 | /// If initial_delay_microseconds is zero, no delays will be made between 52 | /// retries. 53 | /// If all retries failed, returns the last error status. 54 | static absl::Status CallWithRetries(const std::function& f, 55 | const RetryConfig& config); 56 | 57 | /// sleep_usec is a function that sleeps for the given number of microseconds. 58 | static absl::Status CallWithRetries( 59 | const std::function& f, 60 | const std::function& sleep_usec, 61 | const RetryConfig& config); 62 | /// \brief A retrying wrapper for a function that deletes a resource. 63 | /// 64 | /// The function takes care of the scenario when a delete operation 65 | /// returns a failure but succeeds under the hood: if a retry returns 66 | /// NOT_FOUND, the whole operation is considered a success. 67 | static absl::Status DeleteWithRetries( 68 | const std::function& delete_func, 69 | const RetryConfig& config); 70 | }; 71 | 72 | // Given the total number of retries attempted, returns a randomized duration of 73 | // time to delay before the next retry. 74 | // 75 | // The average computed backoff increases with the number of retries attempted. 76 | // See implementation for details on the calculations. 77 | absl::Duration ComputeRetryBackoff( 78 | int current_retry_attempt, absl::Duration min_delay = absl::Milliseconds(1), 79 | absl::Duration max_delay = absl::Seconds(10)); 80 | 81 | } // namespace tsl 82 | 83 | #endif // TENSORFLOW_TSL_PLATFORM_RETRYING_UTILS_H_ 84 | -------------------------------------------------------------------------------- /tsl/platform/denormal.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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_TSL_PLATFORM_DENORMAL_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_DENORMAL_H_ 18 | 19 | #include "xla/tsl/platform/macros.h" 20 | 21 | namespace tsl { 22 | namespace port { 23 | 24 | // State for handling of denormals. 25 | class DenormalState { 26 | public: 27 | DenormalState(bool flush_to_zero, bool denormals_are_zero) 28 | : flush_to_zero_(flush_to_zero), 29 | denormals_are_zero_(denormals_are_zero) {} 30 | 31 | // Output denormals of floating-point operations are flushed to zero. 32 | inline bool flush_to_zero() const { return flush_to_zero_; } 33 | 34 | // Input denormals to floating-point operations are treated as zero. 35 | inline bool denormals_are_zero() const { return denormals_are_zero_; } 36 | 37 | bool operator==(const DenormalState& other) const; 38 | bool operator!=(const DenormalState& other) const; 39 | 40 | private: 41 | bool flush_to_zero_; 42 | bool denormals_are_zero_; 43 | }; 44 | 45 | // Gets the platform's current state for handling denormals. 46 | DenormalState GetDenormalState(); 47 | 48 | // Sets handling of denormals if the platform allows it. Returns `true` if the 49 | // platform supports setting denormals to the specified state. Otherwise the 50 | // denormal state remains unmodified and false is returned. 51 | bool SetDenormalState(const DenormalState& state); 52 | 53 | // Remembers the flush denormal state on construction and restores that same 54 | // state on destruction. 55 | class ScopedRestoreFlushDenormalState { 56 | public: 57 | ScopedRestoreFlushDenormalState(); 58 | ~ScopedRestoreFlushDenormalState(); 59 | 60 | private: 61 | DenormalState denormal_state_; 62 | ScopedRestoreFlushDenormalState(const ScopedRestoreFlushDenormalState&) = 63 | delete; 64 | void operator=(const ScopedRestoreFlushDenormalState&) = delete; 65 | }; 66 | 67 | // While this class is active, denormal floating point numbers are flushed 68 | // to zero. The destructor restores the original flags. 69 | class ScopedFlushDenormal { 70 | public: 71 | ScopedFlushDenormal(); 72 | 73 | private: 74 | ScopedRestoreFlushDenormalState restore_; 75 | ScopedFlushDenormal(const ScopedFlushDenormal&) = delete; 76 | void operator=(const ScopedFlushDenormal&) = delete; 77 | }; 78 | 79 | // While this class is active, denormal floating point numbers are not flushed 80 | // to zero. The destructor restores the original flags. 81 | class ScopedDontFlushDenormal { 82 | public: 83 | ScopedDontFlushDenormal(); 84 | 85 | private: 86 | ScopedRestoreFlushDenormalState restore_; 87 | ScopedDontFlushDenormal(const ScopedDontFlushDenormal&) = delete; 88 | void operator=(const ScopedDontFlushDenormal&) = delete; 89 | }; 90 | 91 | } // namespace port 92 | } // namespace tsl 93 | 94 | #endif // TENSORFLOW_TSL_PLATFORM_DENORMAL_H_ 95 | -------------------------------------------------------------------------------- /tsl/platform/setround_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 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 | #include "tsl/platform/setround.h" 17 | 18 | #include 19 | 20 | #include "xla/tsl/platform/test.h" 21 | 22 | namespace tsl { 23 | namespace { 24 | 25 | // LLVM does not support . Disable these tests when building with it. 26 | // See b/35384639 for more information. 27 | #if !defined(__clang__) || !defined(__OPTIMIZE__) 28 | 29 | void CheckDownward() { 30 | EXPECT_EQ(12, std::nearbyint(12.0)); 31 | EXPECT_EQ(12, std::nearbyint(12.1)); 32 | EXPECT_EQ(-13, std::nearbyint(-12.1)); 33 | EXPECT_EQ(12, std::nearbyint(12.5)); 34 | EXPECT_EQ(12, std::nearbyint(12.9)); 35 | EXPECT_EQ(-13, std::nearbyint(-12.9)); 36 | EXPECT_EQ(13, std::nearbyint(13.0)); 37 | } 38 | 39 | void CheckToNearest() { 40 | EXPECT_EQ(12, std::nearbyint(12.0)); 41 | EXPECT_EQ(12, std::nearbyint(12.1)); 42 | EXPECT_EQ(-12, std::nearbyint(-12.1)); 43 | EXPECT_EQ(12, std::nearbyint(12.5)); 44 | EXPECT_EQ(13, std::nearbyint(12.9)); 45 | EXPECT_EQ(-13, std::nearbyint(-12.9)); 46 | EXPECT_EQ(13, std::nearbyint(13.0)); 47 | } 48 | 49 | void CheckTowardZero() { 50 | EXPECT_EQ(12, std::nearbyint(12.0)); 51 | EXPECT_EQ(12, std::nearbyint(12.1)); 52 | EXPECT_EQ(-12, std::nearbyint(-12.1)); 53 | EXPECT_EQ(12, std::nearbyint(12.5)); 54 | EXPECT_EQ(12, std::nearbyint(12.9)); 55 | EXPECT_EQ(-12, std::nearbyint(-12.9)); 56 | EXPECT_EQ(13, std::nearbyint(13.0)); 57 | } 58 | 59 | void CheckUpward() { 60 | EXPECT_EQ(12, std::nearbyint(12.0)); 61 | EXPECT_EQ(13, std::nearbyint(12.1)); 62 | EXPECT_EQ(-12, std::nearbyint(-12.1)); 63 | EXPECT_EQ(13, std::nearbyint(12.5)); 64 | EXPECT_EQ(13, std::nearbyint(12.9)); 65 | EXPECT_EQ(-12, std::nearbyint(-12.9)); 66 | EXPECT_EQ(13, std::nearbyint(13.0)); 67 | } 68 | 69 | TEST(SetScopedSetRound, Downward) { 70 | port::ScopedSetRound round(FE_DOWNWARD); 71 | CheckDownward(); 72 | } 73 | 74 | TEST(SetScopedSetRound, ToNearest) { 75 | port::ScopedSetRound round(FE_TONEAREST); 76 | CheckToNearest(); 77 | } 78 | 79 | TEST(SetScopedSetRound, TowardZero) { 80 | port::ScopedSetRound round(FE_TOWARDZERO); 81 | CheckTowardZero(); 82 | } 83 | 84 | TEST(SetScopedSetRound, Upward) { 85 | port::ScopedSetRound round(FE_UPWARD); 86 | CheckUpward(); 87 | } 88 | 89 | TEST(SetScopedSetRound, Scoped) { 90 | std::fesetround(FE_TONEAREST); 91 | CheckToNearest(); 92 | { 93 | port::ScopedSetRound round(FE_UPWARD); 94 | CheckUpward(); 95 | } 96 | // Check that the rounding mode is reset when round goes out of scope. 97 | CheckToNearest(); 98 | } 99 | 100 | #endif // !defined(__clang__) || !defined(__OPTIMIZE__) 101 | 102 | // Ensure at least one test case is linked to avoid test failures. 103 | TEST(Dummy, Test) {} 104 | 105 | } // namespace 106 | } // namespace tsl 107 | -------------------------------------------------------------------------------- /tsl/platform/hash.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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 | #include "tsl/platform/hash.h" 17 | 18 | #include 19 | 20 | #include "xla/tsl/platform/macros.h" 21 | #include "xla/tsl/platform/types.h" 22 | #include "tsl/platform/raw_coding.h" 23 | 24 | namespace tsl { 25 | 26 | // 0xff is in case char is signed. 27 | static inline uint32 ByteAs32(char c) { return static_cast(c) & 0xff; } 28 | static inline uint64 ByteAs64(char c) { return static_cast(c) & 0xff; } 29 | 30 | uint32 Hash32(const char* data, size_t n, uint32 seed) { 31 | // 'm' and 'r' are mixing constants generated offline. 32 | // They're not really 'magic', they just happen to work well. 33 | 34 | const uint32 m = 0x5bd1e995; 35 | const int r = 24; 36 | 37 | // Initialize the hash to a 'random' value 38 | uint32 h = seed ^ n; 39 | 40 | // Mix 4 bytes at a time into the hash 41 | while (n >= 4) { 42 | uint32 k = core::DecodeFixed32(data); 43 | 44 | k *= m; 45 | k ^= k >> r; 46 | k *= m; 47 | 48 | h *= m; 49 | h ^= k; 50 | 51 | data += 4; 52 | n -= 4; 53 | } 54 | 55 | // Handle the last few bytes of the input array 56 | 57 | switch (n) { 58 | case 3: 59 | h ^= ByteAs32(data[2]) << 16; 60 | TF_FALLTHROUGH_INTENDED; 61 | case 2: 62 | h ^= ByteAs32(data[1]) << 8; 63 | TF_FALLTHROUGH_INTENDED; 64 | case 1: 65 | h ^= ByteAs32(data[0]); 66 | h *= m; 67 | } 68 | 69 | // Do a few final mixes of the hash to ensure the last few 70 | // bytes are well-incorporated. 71 | 72 | h ^= h >> 13; 73 | h *= m; 74 | h ^= h >> 15; 75 | 76 | return h; 77 | } 78 | 79 | uint64 Hash64(const char* data, size_t n, uint64 seed) { 80 | const uint64 m = 0xc6a4a7935bd1e995; 81 | const int r = 47; 82 | 83 | uint64 h = seed ^ (n * m); 84 | 85 | while (n >= 8) { 86 | uint64 k = core::DecodeFixed64(data); 87 | data += 8; 88 | n -= 8; 89 | 90 | k *= m; 91 | k ^= k >> r; 92 | k *= m; 93 | 94 | h ^= k; 95 | h *= m; 96 | } 97 | 98 | switch (n) { 99 | case 7: 100 | h ^= ByteAs64(data[6]) << 48; 101 | TF_FALLTHROUGH_INTENDED; 102 | case 6: 103 | h ^= ByteAs64(data[5]) << 40; 104 | TF_FALLTHROUGH_INTENDED; 105 | case 5: 106 | h ^= ByteAs64(data[4]) << 32; 107 | TF_FALLTHROUGH_INTENDED; 108 | case 4: 109 | h ^= ByteAs64(data[3]) << 24; 110 | TF_FALLTHROUGH_INTENDED; 111 | case 3: 112 | h ^= ByteAs64(data[2]) << 16; 113 | TF_FALLTHROUGH_INTENDED; 114 | case 2: 115 | h ^= ByteAs64(data[1]) << 8; 116 | TF_FALLTHROUGH_INTENDED; 117 | case 1: 118 | h ^= ByteAs64(data[0]); 119 | h *= m; 120 | } 121 | 122 | h ^= h >> r; 123 | h *= m; 124 | h ^= h >> r; 125 | 126 | return h; 127 | } 128 | 129 | } // namespace tsl 130 | -------------------------------------------------------------------------------- /tsl/profiler/lib/nvtx_utils.h: -------------------------------------------------------------------------------- 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 | 16 | #ifndef TENSORFLOW_TSL_PROFILER_LIB_NVTX_UTILS_H_ 17 | #define TENSORFLOW_TSL_PROFILER_LIB_NVTX_UTILS_H_ 18 | 19 | #include 20 | 21 | #include 22 | #include 23 | 24 | namespace tsl::profiler { 25 | struct String; 26 | // Opaque handle to a string that has been pre-registered with the profiler/NVTX 27 | // implementation 28 | using StringHandle = String*; 29 | 30 | struct ProfilerDomain; 31 | // Opaque handle to a domain in the profiler/NVTX implementation 32 | using ProfilerDomainHandle = ProfilerDomain*; 33 | 34 | // Get the "TSL" domain if NVTX profiling is enabled, otherwise null 35 | ProfilerDomainHandle DefaultProfilerDomain(); 36 | 37 | // Assign a human-readable name to the current thread 38 | void NameCurrentThread(const std::string&); 39 | 40 | // Assign a human-readable name to the given local device 41 | void NameDevice(int device_id, const std::string& device_name); 42 | 43 | struct Stream; 44 | // Opaque handle to an execution stream 45 | using StreamHandle = Stream*; 46 | 47 | // Assign a human-readable name to the given execution stream 48 | void NameStream(StreamHandle stream, const std::string& stream_name); 49 | 50 | // Register a string with the profiler/NVTX implementation for faster use 51 | StringHandle RegisterString(ProfilerDomainHandle, const std::string&); 52 | 53 | // End a range that was created on this thread by RangePush 54 | void RangePop(ProfilerDomainHandle); 55 | 56 | // Older/simpler version; NVTX implementation copies a C-style string each time 57 | void RangePush(ProfilerDomainHandle domain, const char*); 58 | inline void RangePush(ProfilerDomainHandle domain, const std::string& str) { 59 | RangePush(domain, str.c_str()); 60 | } 61 | 62 | namespace detail { 63 | void RangePush(ProfilerDomainHandle domain, StringHandle title, 64 | uint64_t schema_id, const void* payload, size_t payload_size); 65 | } 66 | 67 | // More powerful version: pass a registered string instead of a C-style 68 | // string, and attach a generic payload. The Annotation type must implement a 69 | // method called NvtxSchemaId() that allows the NVTX backend to interpret the 70 | // payload. 71 | template 72 | void RangePush(ProfilerDomainHandle domain, StringHandle title, 73 | const Annotation& annotation) { 74 | return detail::RangePush(domain, title, annotation.NvtxSchemaId(), 75 | &annotation, sizeof(Annotation)); 76 | } 77 | 78 | // Register the schema of a custom payload type, for use with the more powerful 79 | // version of RangePush 80 | uint64_t RegisterSchema(ProfilerDomainHandle domain, const void* schemaAttr); 81 | 82 | // Mark a memory region as initialized. 83 | // This mitigates false positives from the compute sanitizer (initcheck). 84 | void MarkMemoryInitialized(void const* address, size_t size, 85 | StreamHandle stream); 86 | } // namespace tsl::profiler 87 | #endif // TENSORFLOW_TSL_PROFILER_LIB_NVTX_UTILS_H_ 88 | -------------------------------------------------------------------------------- /tsl/platform/unbounded_work_queue_test.cc: -------------------------------------------------------------------------------- 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 | #include "tsl/platform/unbounded_work_queue.h" 17 | 18 | #include "absl/memory/memory.h" 19 | #include "xla/tsl/platform/env.h" 20 | #include "xla/tsl/platform/test.h" 21 | #include "tsl/platform/blocking_counter.h" 22 | #include "tsl/platform/random.h" 23 | 24 | namespace tsl { 25 | namespace { 26 | 27 | class UnboundedWorkQueueTest : public ::testing::Test { 28 | protected: 29 | UnboundedWorkQueueTest() 30 | : work_queue_( 31 | absl::make_unique(Env::Default(), "test")) {} 32 | ~UnboundedWorkQueueTest() override = default; 33 | 34 | void RunMultipleCopiesOfClosure(const int num_closures, 35 | std::function fn) { 36 | for (int i = 0; i < num_closures; ++i) { 37 | work_queue_->Schedule([this, fn]() { 38 | fn(); 39 | mutex_lock l(mu_); 40 | ++closure_count_; 41 | cond_var_.notify_all(); 42 | }); 43 | } 44 | } 45 | 46 | void BlockUntilClosuresDone(const int num_closures) { 47 | mutex_lock l(mu_); 48 | while (closure_count_ < num_closures) { 49 | cond_var_.wait(l); 50 | } 51 | } 52 | 53 | void ResetQueue() { work_queue_.reset(); } 54 | 55 | int NumClosuresExecuted() { 56 | mutex_lock l(mu_); 57 | return closure_count_; 58 | } 59 | 60 | private: 61 | mutex mu_; 62 | int closure_count_ TF_GUARDED_BY(mu_) = 0; 63 | condition_variable cond_var_; 64 | std::unique_ptr work_queue_; 65 | }; 66 | 67 | TEST_F(UnboundedWorkQueueTest, SingleClosure) { 68 | constexpr int num_closures = 1; 69 | RunMultipleCopiesOfClosure(num_closures, []() {}); 70 | BlockUntilClosuresDone(num_closures); 71 | } 72 | 73 | TEST_F(UnboundedWorkQueueTest, MultipleClosures) { 74 | constexpr int num_closures = 10; 75 | RunMultipleCopiesOfClosure(num_closures, []() {}); 76 | BlockUntilClosuresDone(num_closures); 77 | } 78 | 79 | TEST_F(UnboundedWorkQueueTest, MultipleClosuresSleepingRandomly) { 80 | constexpr int num_closures = 1000; 81 | RunMultipleCopiesOfClosure(num_closures, []() { 82 | Env::Default()->SleepForMicroseconds(random::New64() % 10); 83 | }); 84 | BlockUntilClosuresDone(num_closures); 85 | } 86 | 87 | TEST_F(UnboundedWorkQueueTest, NestedClosures) { 88 | constexpr int num_closures = 10; 89 | // Run `num_closures` closures, each of which runs `num_closures` closures. 90 | RunMultipleCopiesOfClosure(num_closures, [=]() { 91 | RunMultipleCopiesOfClosure(num_closures, []() {}); 92 | }); 93 | BlockUntilClosuresDone(num_closures * num_closures + num_closures); 94 | } 95 | 96 | TEST_F(UnboundedWorkQueueTest, RacyDestructor) { 97 | constexpr int num_closures = 100; 98 | // Run `num_closures` closures, then delete `work_queue_`. 99 | RunMultipleCopiesOfClosure(num_closures, []() {}); 100 | ResetQueue(); 101 | EXPECT_LE(NumClosuresExecuted(), num_closures); 102 | } 103 | 104 | } // namespace 105 | } // namespace tsl 106 | -------------------------------------------------------------------------------- /tsl/profiler/lib/profiler_session.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 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_TSL_PROFILER_LIB_PROFILER_SESSION_H_ 16 | #define TENSORFLOW_TSL_PROFILER_LIB_PROFILER_SESSION_H_ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include "absl/status/status.h" 23 | #include "absl/synchronization/mutex.h" 24 | #include "xla/tsl/platform/status.h" 25 | #include "xla/tsl/platform/types.h" 26 | #include "tsl/platform/platform.h" 27 | #include "tsl/platform/thread_annotations.h" 28 | #include "tsl/profiler/protobuf/profiler_options.pb.h" 29 | #include "tsl/profiler/protobuf/xplane.pb.h" 30 | 31 | #if !defined(IS_MOBILE_PLATFORM) 32 | #include "tsl/profiler/lib/profiler_interface.h" 33 | #include "tsl/profiler/lib/profiler_lock.h" 34 | #endif 35 | 36 | namespace tsl { 37 | 38 | // A profiler which will start profiling when creating the object and will stop 39 | // when either the object is destroyed or CollectData is called. 40 | // Multiple instances can be created, but at most one of them will profile. 41 | // Status() will return OK only for the instance that is profiling. 42 | // Thread-safety: ProfilerSession is thread-safe. 43 | class ProfilerSession { 44 | public: 45 | // Creates a ProfilerSession and starts profiling. 46 | static std::unique_ptr Create( 47 | const tensorflow::ProfileOptions& options); 48 | 49 | static tensorflow::ProfileOptions DefaultOptions() { 50 | tensorflow::ProfileOptions options; 51 | options.set_version(1); 52 | options.set_device_tracer_level(1); 53 | options.set_host_tracer_level(2); 54 | options.set_device_type(tensorflow::ProfileOptions::UNSPECIFIED); 55 | options.set_python_tracer_level(0); 56 | options.set_enable_hlo_proto(true); 57 | options.set_include_dataset_ops(true); 58 | return options; 59 | } 60 | 61 | // Deletes an existing Profiler and enables starting a new one. 62 | ~ProfilerSession(); 63 | 64 | absl::Status Status() TF_LOCKS_EXCLUDED(mutex_); 65 | 66 | // Collects profile data into XSpace. 67 | absl::Status CollectData(tensorflow::profiler::XSpace* space) 68 | TF_LOCKS_EXCLUDED(mutex_); 69 | 70 | private: 71 | // Constructs an instance of the class and starts profiling 72 | explicit ProfilerSession(const tensorflow::ProfileOptions& options); 73 | 74 | // ProfilerSession is neither copyable or movable. 75 | ProfilerSession(const ProfilerSession&) = delete; 76 | ProfilerSession& operator=(const ProfilerSession&) = delete; 77 | 78 | #if !defined(IS_MOBILE_PLATFORM) 79 | // Collects profile data into XSpace without post-processsing. 80 | absl::Status CollectDataInternal(tensorflow::profiler::XSpace* space); 81 | 82 | profiler::ProfilerLock profiler_lock_ TF_GUARDED_BY(mutex_); 83 | 84 | std::unique_ptr profilers_ TF_GUARDED_BY(mutex_); 85 | 86 | uint64 start_time_ns_; 87 | uint64 stop_time_ns_; 88 | tensorflow::ProfileOptions options_; 89 | #endif 90 | absl::Status status_ TF_GUARDED_BY(mutex_); 91 | absl::Mutex mutex_; 92 | }; 93 | 94 | } // namespace tsl 95 | #endif // TENSORFLOW_TSL_PROFILER_LIB_PROFILER_SESSION_H_ 96 | -------------------------------------------------------------------------------- /tsl/profiler/lib/profiler_factory_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2021 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 | #include "tsl/profiler/lib/profiler_factory.h" 16 | 17 | #include 18 | #include 19 | 20 | #include "absl/memory/memory.h" 21 | #include "absl/status/status.h" 22 | #include "xla/tsl/platform/macros.h" 23 | #include "xla/tsl/platform/test.h" 24 | #include "tsl/profiler/lib/profiler_interface.h" 25 | #include "tsl/profiler/protobuf/profiler_options.pb.h" 26 | #include "tsl/profiler/protobuf/xplane.pb.h" 27 | 28 | namespace tsl { 29 | namespace profiler { 30 | namespace { 31 | 32 | class TestProfiler : public ProfilerInterface { 33 | public: 34 | absl::Status Start() override { return absl::OkStatus(); } 35 | absl::Status Stop() override { return absl::OkStatus(); } 36 | absl::Status CollectData(tensorflow::profiler::XSpace*) override { 37 | return absl::OkStatus(); 38 | } 39 | }; 40 | 41 | std::unique_ptr TestFactoryFunction( 42 | const tensorflow::ProfileOptions& options) { 43 | return absl::make_unique(); 44 | } 45 | 46 | TEST(ProfilerFactoryTest, FactoryFunctionPointer) { 47 | ClearRegisteredProfilersForTest(); 48 | RegisterProfilerFactory(&TestFactoryFunction); 49 | auto profilers = CreateProfilers(tensorflow::ProfileOptions()); 50 | EXPECT_EQ(profilers.size(), 1); 51 | } 52 | 53 | TEST(ProfilerFactoryTest, FactoryLambda) { 54 | ClearRegisteredProfilersForTest(); 55 | RegisterProfilerFactory([](const tensorflow::ProfileOptions& options) { 56 | return absl::make_unique(); 57 | }); 58 | auto profilers = CreateProfilers(tensorflow::ProfileOptions()); 59 | EXPECT_EQ(profilers.size(), 1); 60 | } 61 | 62 | std::unique_ptr NullFactoryFunction( 63 | const tensorflow::ProfileOptions& options) { 64 | return nullptr; 65 | } 66 | 67 | TEST(ProfilerFactoryTest, FactoryReturnsNull) { 68 | ClearRegisteredProfilersForTest(); 69 | RegisterProfilerFactory(&NullFactoryFunction); 70 | auto profilers = CreateProfilers(tensorflow::ProfileOptions()); 71 | EXPECT_TRUE(profilers.empty()); 72 | } 73 | 74 | class FactoryClass { 75 | public: 76 | explicit FactoryClass(void* ptr) : ptr_(ptr) {} 77 | FactoryClass(const FactoryClass&) = default; // copyable 78 | FactoryClass(FactoryClass&&) = default; // movable 79 | 80 | std::unique_ptr CreateProfiler( 81 | const tensorflow::ProfileOptions& options) const { 82 | return absl::make_unique(); 83 | } 84 | 85 | private: 86 | void* ptr_ TF_ATTRIBUTE_UNUSED = nullptr; 87 | }; 88 | 89 | TEST(ProfilerFactoryTest, FactoryClassCapturedByLambda) { 90 | ClearRegisteredProfilersForTest(); 91 | static int token = 42; 92 | FactoryClass factory(&token); 93 | RegisterProfilerFactory([factory = std::move(factory)]( 94 | const tensorflow::ProfileOptions& options) { 95 | return factory.CreateProfiler(options); 96 | }); 97 | auto profilers = CreateProfilers(tensorflow::ProfileOptions()); 98 | EXPECT_EQ(profilers.size(), 1); 99 | } 100 | 101 | } // namespace 102 | } // namespace profiler 103 | } // namespace tsl 104 | -------------------------------------------------------------------------------- /tsl/profiler/lib/scoped_annotation.h: -------------------------------------------------------------------------------- 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_TSL_PROFILER_LIB_SCOPED_ANNOTATION_H_ 16 | #define TENSORFLOW_TSL_PROFILER_LIB_SCOPED_ANNOTATION_H_ 17 | 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "absl/strings/string_view.h" 26 | #include "xla/tsl/platform/macros.h" 27 | #include "tsl/platform/platform.h" // IWYU pragma: keep 28 | #include "tsl/profiler/lib/nvtx_utils.h" 29 | 30 | #if !defined(IS_MOBILE_PLATFORM) 31 | #include "xla/tsl/profiler/backends/cpu/annotation_stack.h" 32 | #endif 33 | 34 | namespace tsl::profiler { 35 | 36 | // Adds an annotation to all activities through the currently registered 37 | // TraceCollector until PopAnnotation() is called. 38 | template 39 | void PushAnnotation(const T& generator) { 40 | if (auto domain = DefaultProfilerDomain(); 41 | TF_PREDICT_FALSE(domain != nullptr)) { 42 | RangePush(domain, generator()); 43 | return; 44 | } 45 | 46 | #if !defined(IS_MOBILE_PLATFORM) 47 | if (TF_PREDICT_FALSE(AnnotationStack::IsEnabled())) { 48 | AnnotationStack::PushAnnotation( 49 | static_cast(generator())); 50 | } 51 | #endif 52 | } 53 | 54 | inline void PushAnnotation(const char* name) { 55 | PushAnnotation([&] { return name; }); 56 | } 57 | inline void PushAnnotation(const std::string& name) { 58 | PushAnnotation([&] { return name; }); 59 | } 60 | 61 | inline void PopAnnotation() { 62 | // TODO(b/137971921): without this memory fence, two presubmit tests will 63 | // fail probably due to compiler in that presubmit config. 64 | std::atomic_thread_fence(std::memory_order_acquire); 65 | 66 | if (auto domain = DefaultProfilerDomain(); 67 | TF_PREDICT_FALSE(domain != nullptr)) { 68 | RangePop(domain); 69 | return; 70 | } 71 | 72 | #if !defined(IS_MOBILE_PLATFORM) 73 | if (TF_PREDICT_FALSE(AnnotationStack::IsEnabled())) { 74 | AnnotationStack::PopAnnotation(); 75 | } 76 | #endif 77 | } 78 | 79 | // Adds an annotation to all activities for the duration of the instance 80 | // lifetime through the currently registered TraceCollector. 81 | // 82 | // Usage: { 83 | // ScopedAnnotation annotation("my kernels"); 84 | // Kernel1<<>>; 85 | // LaunchKernel2(); // Launches a CUDA kernel. 86 | // } 87 | // This will add 'my kernels' to both kernels in the profiler UI 88 | class ScopedAnnotation { 89 | public: 90 | template 91 | explicit ScopedAnnotation(T&& annotation) { 92 | PushAnnotation(std::forward(annotation)); 93 | } 94 | 95 | // Pops the name passed in the constructor from the current annotation. 96 | ~ScopedAnnotation() { PopAnnotation(); } 97 | 98 | static bool IsEnabled() { 99 | #if !defined(IS_MOBILE_PLATFORM) 100 | return AnnotationStack::IsEnabled(); 101 | #else 102 | return false; 103 | #endif 104 | } 105 | 106 | private: 107 | ScopedAnnotation(const ScopedAnnotation&) = delete; 108 | ScopedAnnotation& operator=(const ScopedAnnotation&) = delete; 109 | }; 110 | 111 | } // namespace tsl::profiler 112 | 113 | #endif // TENSORFLOW_TSL_PROFILER_LIB_SCOPED_ANNOTATION_H_ 114 | -------------------------------------------------------------------------------- /tsl/profiler/lib/traceme_encode_test.cc: -------------------------------------------------------------------------------- 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 | #include "tsl/profiler/lib/traceme_encode.h" 16 | 17 | #include 18 | 19 | #include "absl/strings/str_cat.h" 20 | #include "absl/strings/str_format.h" 21 | #include "xla/tsl/platform/test.h" 22 | #include "xla/tsl/platform/test_benchmark.h" 23 | #include "tsl/platform/platform.h" 24 | 25 | namespace tsl { 26 | namespace profiler { 27 | namespace { 28 | 29 | TEST(TraceMeEncodeTest, NoArgTest) { 30 | EXPECT_EQ(TraceMeEncode("Hello!", {}), "Hello!"); 31 | } 32 | 33 | TEST(TraceMeEncodeTest, OneArgTest) { 34 | EXPECT_EQ(TraceMeEncode("Hello", {{"context", "World"}}), 35 | "Hello#context=World#"); 36 | } 37 | 38 | TEST(TraceMeEncodeTest, TwoArgsTest) { 39 | EXPECT_EQ(TraceMeEncode("Hello", {{"context", "World"}, {"request_id", 42}}), 40 | "Hello#context=World,request_id=42#"); 41 | } 42 | 43 | TEST(TraceMeEncodeTest, ThreeArgsTest) { 44 | EXPECT_EQ(TraceMeEncode("Hello", {{"context", "World"}, 45 | {"request_id", 42}, 46 | {"addr", absl::Hex(0xdeadbeef)}}), 47 | "Hello#context=World,request_id=42,addr=deadbeef#"); 48 | } 49 | 50 | #if !defined(PLATFORM_WINDOWS) 51 | TEST(TraceMeEncodeTest, TemporaryStringTest) { 52 | EXPECT_EQ(TraceMeEncode("Hello", {{std::string("context"), 53 | absl::StrCat("World:", 2020)}}), 54 | "Hello#context=World:2020#"); 55 | } 56 | #endif 57 | 58 | // This can be removed when the absl version has been updated to include 59 | // AbslStringify for open source builds. 60 | #if defined(PLATFORM_GOOGLE) 61 | 62 | struct Point { 63 | template 64 | friend void AbslStringify(Sink& sink, const Point& p) { 65 | absl::Format(&sink, "(%d, %d)", p.x, p.y); 66 | } 67 | 68 | int x; 69 | int y; 70 | }; 71 | 72 | TEST(TraceMeEncodeTest, AbslStringifyTest) { 73 | EXPECT_EQ(TraceMeEncode("Plot", {{"point", Point{10, 20}}}), 74 | "Plot#point=(10, 20)#"); 75 | } 76 | 77 | #endif 78 | 79 | TEST(TraceMeEncodeTest, NoNameTest) { 80 | EXPECT_EQ(TraceMeEncode({{"context", "World"}, {"request_id", 42}}), 81 | "#context=World,request_id=42#"); 82 | } 83 | 84 | } // namespace 85 | 86 | void BM_TraceMeEncode(::testing::benchmark::State& state) { 87 | for (auto s : state) { 88 | TraceMeEncode( 89 | "MyTestEvent", 90 | {{"Lorem ipsum dolor sit amet", 1}, 91 | {"consectetur adipiscing elit", 2}, 92 | {"sed do eiusmod tempor incididunt", 3.52}, 93 | {"ut labore et dolore magna aliqua", "Ut enim ad minim veniam"}, 94 | {"quis nostrud exercitation ullamco", "laboris nisi ut aliquip ex"}, 95 | {"ea commodo consequat.", 11111.1111}, 96 | {"Duis aute", 1234567890}, 97 | {"irure dolor in", " reprehenderit in voluptate"}, 98 | {"velit esse cillum dolore", "eu fugiat nulla pariatur."}, 99 | {"Excepteur sint", "occaecat cupidatat non proident, sunt in"}, 100 | {"culpa qui officia", "deserunt mollit anim id est laborum."}}); 101 | } 102 | } 103 | BENCHMARK(BM_TraceMeEncode); 104 | 105 | } // namespace profiler 106 | } // namespace tsl 107 | -------------------------------------------------------------------------------- /tsl/profiler/protobuf/BUILD: -------------------------------------------------------------------------------- 1 | # copybara:uncomment(oss-unused) load("//devtools/clif/python:clif_build_rule.bzl", "pyclif_proto_library") 2 | # copybara:uncomment(oss-unused) load("//net/grpc/go/build_defs:go_grpc_library.bzl", "go_grpc_library") 3 | # Placeholder: load py_proto_library 4 | load("@xla//xla/tsl:tsl.bzl", "internal_visibility") 5 | load("@xla//xla/tsl/platform:build_config.bzl", "tf_proto_library") 6 | 7 | # copybara:uncomment package(default_applicable_licenses = ["//tensorflow:license"]) 8 | 9 | package_group( 10 | name = "friends", 11 | includes = [ 12 | "@xla//xla/tsl/profiler:friends", 13 | ], 14 | ) 15 | 16 | tf_proto_library( 17 | name = "xplane_proto", 18 | srcs = ["xplane.proto"], 19 | make_default_target_header_only = True, 20 | visibility = internal_visibility([":friends"]), 21 | ) 22 | 23 | # copybara:uncomment_begin(google-only) 24 | # pyclif_proto_library( 25 | # name = "xplane_pyclif", 26 | # proto_lib = ":xplane_proto", 27 | # visibility = [ 28 | # "//visibility:public", 29 | # ], 30 | # ) 31 | # copybara:uncomment_end 32 | 33 | tf_proto_library( 34 | name = "profiler_options_proto", 35 | srcs = ["profiler_options.proto"], 36 | make_default_target_header_only = True, 37 | visibility = ["//visibility:public"], 38 | ) 39 | 40 | tf_proto_library( 41 | name = "profiler_service_monitor_result_proto", 42 | srcs = ["profiler_service_monitor_result.proto"], 43 | make_default_target_header_only = True, 44 | visibility = ["//visibility:public"], 45 | ) 46 | 47 | tf_proto_library( 48 | name = "profiler_service_proto", 49 | srcs = ["profiler_service.proto"], 50 | has_services = 1, 51 | create_grpc_library = True, 52 | create_java_proto = False, 53 | create_kotlin_proto = False, 54 | make_default_target_header_only = True, 55 | protodeps = [ 56 | ":profiler_options_proto", 57 | ":profiler_service_monitor_result_proto", 58 | ":xplane_proto", 59 | ], 60 | use_grpc_namespace = True, 61 | visibility = ["//visibility:public"], 62 | ) 63 | 64 | tf_proto_library( 65 | name = "profiler_analysis_proto", 66 | srcs = ["profiler_analysis.proto"], 67 | has_services = 1, 68 | create_grpc_library = True, 69 | create_java_proto = False, 70 | create_kotlin_proto = False, 71 | make_default_target_header_only = True, 72 | protodeps = [":profiler_service_proto"], 73 | use_grpc_namespace = True, 74 | visibility = ["//visibility:public"], 75 | ) 76 | 77 | tf_proto_library( 78 | name = "trace_events_proto", 79 | srcs = ["trace_events.proto"], 80 | visibility = internal_visibility([":friends"]), 81 | ) 82 | 83 | # copybara:uncomment_begin(google-only) 84 | # py_proto_library( 85 | # name = "trace_events_py_pb2", 86 | # visibility = ["//visibility:public"], 87 | # deps = [":trace_events_proto"], 88 | # ) 89 | # copybara:uncomment_end 90 | 91 | # This is needed because of how tf_android_core_proto_sources parses proto paths. 92 | exports_files( 93 | srcs = ["xplane.proto"], 94 | visibility = internal_visibility([ 95 | "//tensorflow/core:__pkg__", 96 | "//tsl:__pkg__", 97 | ]), 98 | ) 99 | 100 | tf_proto_library( 101 | name = "profile_proto", 102 | srcs = ["profile.proto"], 103 | visibility = internal_visibility([ 104 | ":friends", 105 | "@xla//xla/tsl/profiler:internal", 106 | ]), 107 | ) 108 | 109 | tf_proto_library( 110 | name = "protos_all", 111 | create_go_proto = False, 112 | create_java_proto = False, 113 | create_kotlin_proto = False, 114 | make_default_target_header_only = True, 115 | protodeps = [ 116 | ":xplane_proto", 117 | ":profiler_options_proto", 118 | ], 119 | visibility = ["//visibility:public"], 120 | ) 121 | 122 | # copybara:uncomment_begin(google-only) 123 | # py_proto_library( 124 | # name = "xplane_py_pb2", 125 | # visibility = internal_visibility([":friends"]), 126 | # deps = [":xplane_proto"], 127 | # ) 128 | # copybara:uncomment_end 129 | 130 | tf_proto_library( 131 | name = "profiled_instructions_proto", 132 | srcs = ["profiled_instructions.proto"], 133 | make_default_target_header_only = True, 134 | visibility = internal_visibility([":friends"]), 135 | ) 136 | -------------------------------------------------------------------------------- /tsl/platform/stringprintf_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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 | #include "tsl/platform/stringprintf.h" 17 | 18 | #include 19 | 20 | #include "xla/tsl/platform/test.h" 21 | 22 | namespace tsl { 23 | namespace strings { 24 | namespace { 25 | 26 | TEST(PrintfTest, Empty) { 27 | EXPECT_EQ("", Printf("%s", string().c_str())); 28 | EXPECT_EQ("", Printf("%s", "")); 29 | } 30 | 31 | TEST(PrintfTest, Misc) { 32 | // MSVC does not support $ format specifier. 33 | #if !defined(_MSC_VER) 34 | EXPECT_EQ("123hello w", Printf("%3$d%2$s %1$c", 'w', "hello", 123)); 35 | #endif // !_MSC_VER 36 | } 37 | 38 | TEST(AppendfTest, Empty) { 39 | string value("Hello"); 40 | const char* empty = ""; 41 | Appendf(&value, "%s", empty); 42 | EXPECT_EQ("Hello", value); 43 | } 44 | 45 | TEST(AppendfTest, EmptyString) { 46 | string value("Hello"); 47 | Appendf(&value, "%s", ""); 48 | EXPECT_EQ("Hello", value); 49 | } 50 | 51 | TEST(AppendfTest, String) { 52 | string value("Hello"); 53 | Appendf(&value, " %s", "World"); 54 | EXPECT_EQ("Hello World", value); 55 | } 56 | 57 | TEST(AppendfTest, Int) { 58 | string value("Hello"); 59 | Appendf(&value, " %d", 123); 60 | EXPECT_EQ("Hello 123", value); 61 | } 62 | 63 | TEST(PrintfTest, Multibyte) { 64 | // If we are in multibyte mode and feed invalid multibyte sequence, 65 | // Printf should return an empty string instead of running 66 | // out of memory while trying to determine destination buffer size. 67 | // see b/4194543. 68 | 69 | char* old_locale = setlocale(LC_CTYPE, nullptr); 70 | // Push locale with multibyte mode 71 | setlocale(LC_CTYPE, "en_US.utf8"); 72 | 73 | const char kInvalidCodePoint[] = "\375\067s"; 74 | string value = Printf("%.*s", 3, kInvalidCodePoint); 75 | 76 | // In some versions of glibc (e.g. eglibc-2.11.1, aka GRTEv2), snprintf 77 | // returns error given an invalid codepoint. Other versions 78 | // (e.g. eglibc-2.15, aka pre-GRTEv3) emit the codepoint verbatim. 79 | // We test that the output is one of the above. 80 | EXPECT_TRUE(value.empty() || value == kInvalidCodePoint); 81 | 82 | // Repeat with longer string, to make sure that the dynamically 83 | // allocated path in StringAppendV is handled correctly. 84 | int n = 2048; 85 | char* buf = new char[n + 1]; 86 | memset(buf, ' ', n - 3); 87 | memcpy(buf + n - 3, kInvalidCodePoint, 4); 88 | value = Printf("%.*s", n, buf); 89 | // See GRTEv2 vs. GRTEv3 comment above. 90 | EXPECT_TRUE(value.empty() || value == buf); 91 | delete[] buf; 92 | 93 | setlocale(LC_CTYPE, old_locale); 94 | } 95 | 96 | TEST(PrintfTest, NoMultibyte) { 97 | // No multibyte handling, but the string contains funny chars. 98 | char* old_locale = setlocale(LC_CTYPE, nullptr); 99 | setlocale(LC_CTYPE, "POSIX"); 100 | string value = Printf("%.*s", 3, "\375\067s"); 101 | setlocale(LC_CTYPE, old_locale); 102 | EXPECT_EQ("\375\067s", value); 103 | } 104 | 105 | TEST(PrintfTest, DontOverwriteErrno) { 106 | // Check that errno isn't overwritten unless we're printing 107 | // something significantly larger than what people are normally 108 | // printing in their badly written PLOG() statements. 109 | errno = ECHILD; 110 | string value = Printf("Hello, %s!", "World"); 111 | EXPECT_EQ(ECHILD, errno); 112 | } 113 | 114 | TEST(PrintfTest, LargeBuf) { 115 | // Check that the large buffer is handled correctly. 116 | int n = 2048; 117 | char* buf = new char[n + 1]; 118 | memset(buf, ' ', n); 119 | buf[n] = 0; 120 | string value = Printf("%s", buf); 121 | EXPECT_EQ(buf, value); 122 | delete[] buf; 123 | } 124 | 125 | } // namespace 126 | 127 | } // namespace strings 128 | } // namespace tsl 129 | -------------------------------------------------------------------------------- /tsl/platform/null_file_system.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 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_TSL_PLATFORM_NULL_FILE_SYSTEM_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_NULL_FILE_SYSTEM_H_ 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | #include "xla/tsl/platform/env.h" 24 | #include "xla/tsl/platform/file_system.h" 25 | #include "xla/tsl/platform/file_system_helper.h" 26 | 27 | namespace tsl { 28 | 29 | // START_SKIP_DOXYGEN 30 | 31 | #ifndef SWIG 32 | // Degenerate file system that provides no implementations. 33 | class NullFileSystem : public FileSystem { 34 | public: 35 | NullFileSystem() {} 36 | 37 | ~NullFileSystem() override = default; 38 | 39 | TF_USE_FILESYSTEM_METHODS_WITH_NO_TRANSACTION_SUPPORT; 40 | 41 | absl::Status NewRandomAccessFile( 42 | const string& fname, TransactionToken* token, 43 | std::unique_ptr* result) override { 44 | return errors::Unimplemented("NewRandomAccessFile unimplemented"); 45 | } 46 | 47 | absl::Status NewWritableFile(const string& fname, TransactionToken* token, 48 | std::unique_ptr* result) override { 49 | return errors::Unimplemented("NewWritableFile unimplemented"); 50 | } 51 | 52 | absl::Status NewAppendableFile( 53 | const string& fname, TransactionToken* token, 54 | std::unique_ptr* result) override { 55 | return errors::Unimplemented("NewAppendableFile unimplemented"); 56 | } 57 | 58 | absl::Status NewReadOnlyMemoryRegionFromFile( 59 | const string& fname, TransactionToken* token, 60 | std::unique_ptr* result) override { 61 | return errors::Unimplemented( 62 | "NewReadOnlyMemoryRegionFromFile unimplemented"); 63 | } 64 | 65 | absl::Status FileExists(const string& fname, 66 | TransactionToken* token) override { 67 | return errors::Unimplemented("FileExists unimplemented"); 68 | } 69 | 70 | absl::Status GetChildren(const string& dir, TransactionToken* token, 71 | std::vector* result) override { 72 | return errors::Unimplemented("GetChildren unimplemented"); 73 | } 74 | 75 | absl::Status GetMatchingPaths(const string& pattern, TransactionToken* token, 76 | std::vector* results) override { 77 | return internal::GetMatchingPaths(this, Env::Default(), pattern, results); 78 | } 79 | 80 | absl::Status DeleteFile(const string& fname, 81 | TransactionToken* token) override { 82 | return errors::Unimplemented("DeleteFile unimplemented"); 83 | } 84 | 85 | absl::Status CreateDir(const string& dirname, 86 | TransactionToken* token) override { 87 | return errors::Unimplemented("CreateDir unimplemented"); 88 | } 89 | 90 | absl::Status DeleteDir(const string& dirname, 91 | TransactionToken* token) override { 92 | return errors::Unimplemented("DeleteDir unimplemented"); 93 | } 94 | 95 | absl::Status GetFileSize(const string& fname, TransactionToken* token, 96 | uint64* file_size) override { 97 | return errors::Unimplemented("GetFileSize unimplemented"); 98 | } 99 | 100 | absl::Status RenameFile(const string& src, const string& target, 101 | TransactionToken* token) override { 102 | return errors::Unimplemented("RenameFile unimplemented"); 103 | } 104 | 105 | absl::Status Stat(const string& fname, TransactionToken* token, 106 | FileStatistics* stat) override { 107 | return errors::Unimplemented("Stat unimplemented"); 108 | } 109 | }; 110 | #endif 111 | 112 | // END_SKIP_DOXYGEN 113 | 114 | } // namespace tsl 115 | 116 | #endif // TENSORFLOW_TSL_PLATFORM_NULL_FILE_SYSTEM_H_ 117 | -------------------------------------------------------------------------------- /tsl/profiler/lib/scoped_annotation_test.cc: -------------------------------------------------------------------------------- 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 | #include "tsl/profiler/lib/scoped_annotation.h" 17 | 18 | #include 19 | 20 | #include "absl/strings/str_cat.h" 21 | #include "xla/tsl/platform/test.h" 22 | #include "xla/tsl/platform/test_benchmark.h" 23 | #include "xla/tsl/profiler/backends/cpu/annotation_stack.h" 24 | 25 | namespace tsl { 26 | namespace profiler { 27 | namespace { 28 | 29 | TEST(ScopedAnnotation, Simple) { 30 | { 31 | ScopedAnnotation trace("blah"); 32 | EXPECT_EQ(AnnotationStack::Get(), ""); // not enabled 33 | } 34 | 35 | { 36 | AnnotationStack::Enable(true); 37 | ScopedAnnotation trace("blah"); 38 | EXPECT_EQ(AnnotationStack::Get(), "blah"); // enabled 39 | AnnotationStack::Enable(false); 40 | } 41 | 42 | { 43 | AnnotationStack::Enable(true); 44 | ScopedAnnotation outer("foo"); 45 | ScopedAnnotation inner("bar"); 46 | EXPECT_EQ(AnnotationStack::Get(), "foo::bar"); // enabled 47 | AnnotationStack::Enable(false); 48 | } 49 | 50 | { 51 | AnnotationStack::Enable(true); 52 | PushAnnotation("foo"); 53 | PushAnnotation("bar"); 54 | EXPECT_EQ(AnnotationStack::Get(), "foo::bar"); // enabled 55 | PopAnnotation(); 56 | PopAnnotation(); 57 | AnnotationStack::Enable(false); 58 | } 59 | 60 | EXPECT_EQ(AnnotationStack::Get(), ""); // not enabled 61 | } 62 | 63 | std::string GenerateRandomString(int length) { 64 | return std::string(length, 'a'); 65 | } 66 | 67 | void BM_ScopedAnnotationDisabled(::testing::benchmark::State& state) { 68 | const int annotation_size = state.range(0); 69 | 70 | std::string annotation = GenerateRandomString(annotation_size); 71 | for (auto s : state) { 72 | ScopedAnnotation trace(annotation); 73 | } 74 | } 75 | 76 | BENCHMARK(BM_ScopedAnnotationDisabled)->Arg(8)->Arg(32)->Arg(128); 77 | 78 | void BM_ScopedAnnotationEnabled(::testing::benchmark::State& state) { 79 | const int annotation_size = state.range(0); 80 | 81 | std::string annotation = GenerateRandomString(annotation_size); 82 | AnnotationStack::Enable(true); 83 | for (auto s : state) { 84 | ScopedAnnotation trace(annotation); 85 | } 86 | AnnotationStack::Enable(false); 87 | } 88 | 89 | BENCHMARK(BM_ScopedAnnotationEnabled)->Arg(8)->Arg(32)->Arg(128); 90 | 91 | void BM_ScopedAnnotationEnabled_Nested(::testing::benchmark::State& state) { 92 | const int annotation_size = state.range(0); 93 | 94 | std::string annotation = GenerateRandomString(annotation_size); 95 | AnnotationStack::Enable(true); 96 | for (auto s : state) { 97 | ScopedAnnotation trace(annotation); 98 | { ScopedAnnotation trace(annotation); } 99 | } 100 | AnnotationStack::Enable(false); 101 | } 102 | 103 | BENCHMARK(BM_ScopedAnnotationEnabled_Nested)->Arg(8)->Arg(32)->Arg(128); 104 | 105 | void BM_ScopedAnnotationEnabled_Adhoc(::testing::benchmark::State& state) { 106 | AnnotationStack::Enable(true); 107 | int i = 0; 108 | for (auto s : state) { 109 | // generate the annotation on the fly. 110 | ScopedAnnotation trace(absl::StrCat(i, "-", i * i)); 111 | ++i; 112 | } 113 | AnnotationStack::Enable(false); 114 | } 115 | 116 | BENCHMARK(BM_ScopedAnnotationEnabled_Adhoc); 117 | 118 | void BM_ScopedAnnotationDisabled_Lambda(::testing::benchmark::State& state) { 119 | int i = 0; 120 | for (auto s : state) { 121 | ScopedAnnotation trace([&]() { return absl::StrCat(i, "-", i * i); }); 122 | ++i; 123 | } 124 | } 125 | 126 | BENCHMARK(BM_ScopedAnnotationDisabled_Lambda); 127 | 128 | void BM_ScopedAnnotationEnabled_Adhoc_Lambda( 129 | ::testing::benchmark::State& state) { 130 | AnnotationStack::Enable(true); 131 | int i = 0; 132 | for (auto s : state) { 133 | ScopedAnnotation trace([&]() { return absl::StrCat(i, "-", i * i); }); 134 | ++i; 135 | } 136 | AnnotationStack::Enable(false); 137 | } 138 | 139 | BENCHMARK(BM_ScopedAnnotationEnabled_Adhoc_Lambda); 140 | 141 | } // namespace 142 | } // namespace profiler 143 | } // namespace tsl 144 | -------------------------------------------------------------------------------- /tsl/platform/mem.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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_TSL_PLATFORM_MEM_H_ 17 | #define TENSORFLOW_TSL_PLATFORM_MEM_H_ 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | // TODO(cwhipkey): remove this when callers use annotations directly. 24 | #include "absl/base/macros.h" 25 | #include "xla/tsl/platform/dynamic_annotations.h" 26 | #include "xla/tsl/platform/types.h" 27 | #include "tsl/platform/platform.h" 28 | 29 | namespace tsl { 30 | namespace port { 31 | 32 | // Aligned allocation/deallocation. `minimum_alignment` must be a power of 2 33 | // and a multiple of sizeof(void*). 34 | void* AlignedMalloc(size_t size, std::align_val_t minimum_alignment); 35 | ABSL_DEPRECATE_AND_INLINE() 36 | inline void* AlignedMalloc(size_t size, int minimum_alignment) { 37 | return AlignedMalloc(size, static_cast(minimum_alignment)); 38 | } 39 | void AlignedFree(void* aligned_memory); 40 | void AlignedSizedFree(void* aligned_memory, size_t size, 41 | std::align_val_t minimum_alignment); 42 | ABSL_DEPRECATE_AND_INLINE() 43 | inline void AlignedSizedFree(void* aligned_memory, size_t alignment, 44 | size_t size) { 45 | AlignedSizedFree(aligned_memory, size, 46 | static_cast(alignment)); 47 | } 48 | 49 | // An allocator that allocates memory with the given minimum alignment. 50 | template 51 | struct AlignedAllocator { 52 | using value_type = T; 53 | 54 | value_type* allocate(size_t n) { 55 | return static_cast( 56 | AlignedMalloc(n * sizeof(value_type), minimum_alignment)); 57 | } 58 | 59 | void deallocate(value_type* p, size_t n) { 60 | return AlignedSizedFree(p, n, minimum_alignment); 61 | } 62 | }; 63 | 64 | void* Malloc(size_t size); 65 | void* Realloc(void* ptr, size_t size); 66 | void Free(void* ptr); 67 | 68 | // Tries to release num_bytes of free memory back to the operating 69 | // system for reuse. Use this routine with caution -- to get this 70 | // memory back may require faulting pages back in by the OS, and 71 | // that may be slow. 72 | // 73 | // Currently, if a malloc implementation does not support this 74 | // routine, this routine is a no-op. 75 | void MallocExtension_ReleaseToSystem(std::size_t num_bytes); 76 | 77 | // Returns the actual number N of bytes reserved by the malloc for the 78 | // pointer p. This number may be equal to or greater than the number 79 | // of bytes requested when p was allocated. 80 | // 81 | // This routine is just useful for statistics collection. The 82 | // client must *not* read or write from the extra bytes that are 83 | // indicated by this call. 84 | // 85 | // Example, suppose the client gets memory by calling 86 | // p = malloc(10) 87 | // and GetAllocatedSize(p) may return 16. The client must only use the 88 | // first 10 bytes p[0..9], and not attempt to read or write p[10..15]. 89 | // 90 | // Currently, if a malloc implementation does not support this 91 | // routine, this routine returns 0. 92 | std::size_t MallocExtension_GetAllocatedSize(const void* p); 93 | 94 | struct MemoryInfo { 95 | int64_t total = 0; 96 | int64_t free = 0; 97 | }; 98 | 99 | struct MemoryBandwidthInfo { 100 | int64_t bw_used = 0; // memory bandwidth used across all CPU (in MBs/second) 101 | }; 102 | 103 | // Retrieves the host memory information. If any of the fields in the returned 104 | // MemoryInfo structure is INT64_MAX, it means such information is not 105 | // available. 106 | MemoryInfo GetMemoryInfo(); 107 | 108 | // Retrieves the host memory bandwidth information. If any field in the returned 109 | // structure is INT64_MAX, it means such information is not available. 110 | MemoryBandwidthInfo GetMemoryBandwidthInfo(); 111 | 112 | // Returns the amount of RAM available in bytes, or INT64_MAX if unknown. 113 | static inline int64_t AvailableRam() { return GetMemoryInfo().free; } 114 | 115 | } // namespace port 116 | } // namespace tsl 117 | 118 | #endif // TENSORFLOW_TSL_PLATFORM_MEM_H_ 119 | -------------------------------------------------------------------------------- /tsl/platform/mutex_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 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 | #include "tsl/platform/mutex.h" 17 | 18 | #include "xla/tsl/platform/test.h" 19 | #include "xla/tsl/platform/threadpool.h" 20 | 21 | namespace tsl { 22 | namespace { 23 | 24 | // Check that mutex_lock and shared_mutex_lock are movable. 25 | class MutexTest : public ::testing::Test { 26 | protected: 27 | mutex_lock GetLock() TF_NO_THREAD_SAFETY_ANALYSIS { 28 | // Known false positive with thread safety analysis: the mutex is not 29 | // unlocked because the scoped lock doesn't destruct, which is WAI as the 30 | // caller will destruct it, but thread safety analysis complains. 31 | // See https://github.com/llvm/llvm-project/issues/58480. 32 | return mutex_lock{mu_}; 33 | } 34 | 35 | tf_shared_lock GetSharedLock() TF_NO_THREAD_SAFETY_ANALYSIS { 36 | // Same known false positive as above. 37 | return tf_shared_lock{mu_}; 38 | } 39 | 40 | // tsl::mutex does not have methods to test if a lock is held. In order to 41 | // test whether the lock is held, provide test-friendly wrappers around the 42 | // try_lock methods. These are obviously not suitable for production use, but 43 | // work in a single-threaded test. 44 | 45 | bool test_try_lock() { 46 | bool test = mu_.try_lock(); 47 | if (test) mu_.unlock(); 48 | return test; 49 | } 50 | 51 | bool test_try_lock_shared() { 52 | bool test = mu_.try_lock_shared(); 53 | if (test) mu_.unlock_shared(); 54 | return test; 55 | } 56 | 57 | mutex mu_; 58 | }; 59 | 60 | TEST_F(MutexTest, MovableMutexLockTest) { 61 | // Unlocked: we can get a normal lock. 62 | EXPECT_TRUE(test_try_lock()); 63 | { 64 | mutex_lock lock = GetLock(); 65 | // Locked: we can't get either kind of lock. 66 | EXPECT_FALSE(test_try_lock()); 67 | EXPECT_FALSE(test_try_lock_shared()); 68 | } 69 | // Unlocked: we can get a normal lock. 70 | EXPECT_TRUE(test_try_lock()); 71 | } 72 | 73 | TEST_F(MutexTest, SharedMutexLockTest) { 74 | // Unlocked: we can get a normal lock. 75 | EXPECT_TRUE(test_try_lock()); 76 | { 77 | tf_shared_lock lock = GetSharedLock(); 78 | // Locked in shared mode: we can't get a normal lock, but we can get a 79 | // shared one. 80 | EXPECT_FALSE(test_try_lock()); 81 | EXPECT_TRUE(test_try_lock_shared()); 82 | } 83 | // Unlocked: we can get a normal lock. 84 | EXPECT_TRUE(test_try_lock()); 85 | } 86 | 87 | TEST(ConditionVariableTest, WaitWithPredicate) { 88 | constexpr int kNumThreads = 4; 89 | mutex mu; 90 | condition_variable cv; 91 | bool ready = false; 92 | int count = 0; 93 | 94 | // Add tasks to threads that wait on the `ready` flag. 95 | tsl::thread::ThreadPool pool(Env::Default(), 96 | "condition_variable_test_wait_with_predicate", 97 | kNumThreads); 98 | for (int i = 0; i < kNumThreads; ++i) { 99 | pool.Schedule([&mu, &cv, &ready, &count]() { 100 | mutex_lock lock(mu); 101 | cv.wait(lock, [&ready] { return ready; }); 102 | ++count; 103 | cv.notify_one(); 104 | }); 105 | } 106 | 107 | // Verify threads are still waiting. 108 | { 109 | mutex_lock lock(mu); 110 | EXPECT_EQ(count, 0); 111 | } 112 | 113 | // Start worker threads. 114 | { 115 | mutex_lock lock(mu); 116 | ready = true; 117 | cv.notify_all(); 118 | } 119 | 120 | // Wait for workers to complete. 121 | { 122 | mutex_lock lock(mu); 123 | // NOLINTNEXTLINE: MSVC requires kNumThreads to be captured. 124 | cv.wait(lock, [&count, kNumThreads] { return count == kNumThreads; }); 125 | EXPECT_EQ(count, kNumThreads); 126 | } 127 | } 128 | 129 | TEST(ConditionVariableTest, WaitWithTruePredicateDoesntBlock) { 130 | mutex mu; 131 | mutex_lock lock(mu); 132 | condition_variable cv; 133 | 134 | // CV doesn't wait if predicate is true. 135 | cv.wait(lock, [] { return true; }); 136 | 137 | // Verify the lock is still locked. 138 | EXPECT_TRUE(static_cast(lock)); 139 | } 140 | 141 | } // namespace 142 | } // namespace tsl 143 | -------------------------------------------------------------------------------- /tsl/platform/hash.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2015 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 | // Simple hash functions used for internal data structures 17 | 18 | #ifndef TENSORFLOW_TSL_PLATFORM_HASH_H_ 19 | #define TENSORFLOW_TSL_PLATFORM_HASH_H_ 20 | 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | #include "absl/strings/string_view.h" 28 | #include "xla/tsl/platform/types.h" 29 | #include "tsl/platform/stringpiece.h" 30 | 31 | namespace tsl { 32 | 33 | extern uint32 Hash32(const char* data, size_t n, uint32 seed); 34 | extern uint64 Hash64(const char* data, size_t n, uint64 seed); 35 | 36 | inline uint64 Hash64(const char* data, size_t n) { 37 | return Hash64(data, n, 0xDECAFCAFFE); 38 | } 39 | 40 | inline uint64 Hash64(const char* data) { return Hash64(data, ::strlen(data)); } 41 | 42 | inline uint64 Hash64(const std::string& str) { 43 | return Hash64(str.data(), str.size()); 44 | } 45 | 46 | inline uint64 Hash64(const tstring& str) { 47 | return Hash64(str.data(), str.size()); 48 | } 49 | 50 | inline uint64 Hash64Combine(uint64 a, uint64 b) { 51 | return a ^ (b + 0x9e3779b97f4a7800ULL + (a << 10) + (a >> 4)); 52 | } 53 | 54 | // Combine two hashes in an order-independent way. This operation should be 55 | // associative and compute the same hash for a collection of elements 56 | // independent of traversal order. Note that it is better to combine hashes 57 | // symmetrically with addition rather than XOR, since (x^x) == 0 but (x+x) != 0. 58 | inline uint64 Hash64CombineUnordered(uint64 a, uint64 b) { return a + b; } 59 | 60 | // Hash functor suitable for use with power-of-two sized hashtables. Use 61 | // instead of std::hash. 62 | // 63 | // In particular, tsl::hash is not the identity function for pointers. 64 | // This is important for power-of-two sized hashtables like FlatMap and FlatSet, 65 | // because otherwise they waste the majority of their hash buckets. 66 | // 67 | // The second type argument is only used for SFNIAE below. 68 | template 69 | struct hash { 70 | size_t operator()(const T& t) const { return std::hash()(t); } 71 | }; 72 | 73 | template 74 | struct hash::value>::type> { 75 | size_t operator()(T value) const { 76 | // This works around a defect in the std::hash C++ spec that isn't fixed in 77 | // (at least) gcc 4.8.4: 78 | // http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2148 79 | // 80 | // We should be able to remove this and use the default 81 | // tsl::hash() once we stop building with GCC versions old 82 | // enough to not have this defect fixed. 83 | return std::hash()(static_cast(value)); 84 | } 85 | }; 86 | 87 | template 88 | struct hash { 89 | size_t operator()(const T* t) const { 90 | // Hash pointers as integers, but bring more entropy to the lower bits. 91 | size_t k = static_cast(reinterpret_cast(t)); 92 | return k + (k >> 6); 93 | } 94 | }; 95 | 96 | template <> 97 | struct hash { 98 | size_t operator()(const string& s) const { 99 | return static_cast(Hash64(s)); 100 | } 101 | }; 102 | 103 | template <> 104 | struct hash { 105 | size_t operator()(const tstring& s) const { 106 | return static_cast(Hash64(s.data(), s.size())); 107 | } 108 | }; 109 | 110 | template <> 111 | struct hash { 112 | size_t operator()(absl::string_view sp) const { 113 | return static_cast(Hash64(sp.data(), sp.size())); 114 | } 115 | }; 116 | using StringPieceHasher = ::tsl::hash; 117 | 118 | template 119 | struct hash> { 120 | size_t operator()(const std::pair& p) const { 121 | return Hash64Combine(hash()(p.first), hash()(p.second)); 122 | } 123 | }; 124 | 125 | } // namespace tsl 126 | 127 | namespace std { 128 | template <> 129 | struct hash { 130 | size_t operator()(const tsl::tstring& s) const { 131 | return static_cast(tsl::Hash64(s.data(), s.size())); 132 | } 133 | }; 134 | } // namespace std 135 | 136 | #endif // TENSORFLOW_TSL_PLATFORM_HASH_H_ 137 | --------------------------------------------------------------------------------