├── .bazelversion ├── tensorflow_federated ├── python │ ├── common_libs │ │ ├── golden_test_goldens │ │ │ ├── test_check_string_updates.expected │ │ │ ├── test_check_string_fails.expected │ │ │ └── test_check_string_succeeds.expected │ │ └── __init__.py │ ├── aggregators │ │ └── README.md │ ├── core │ │ ├── backends │ │ │ ├── mapreduce │ │ │ │ └── intrinsics_test_goldens │ │ │ │ │ └── federated_secure_modular_sum.expected │ │ │ ├── BUILD │ │ │ ├── __init__.py │ │ │ ├── test │ │ │ │ └── __init__.py │ │ │ └── native │ │ │ │ └── __init__.py │ │ ├── environments │ │ │ ├── __init__.py │ │ │ ├── tensorflow_backend │ │ │ │ ├── __init__.py │ │ │ │ └── tensorflow_executor_bindings.py │ │ │ ├── tensorflow_frontend │ │ │ │ └── __init__.py │ │ │ ├── BUILD │ │ │ └── tensorflow │ │ │ │ ├── BUILD │ │ │ │ └── __init__.py │ │ ├── __init__.py │ │ ├── impl │ │ │ ├── compiler │ │ │ │ ├── compiler_test_utils_test_goldens │ │ │ │ │ └── test_check_computations.expected │ │ │ │ ├── __init__.py │ │ │ │ └── tree_transformations_test_goldens │ │ │ │ │ └── uniquify_names_blocks_nested_inside_of_locals.expected │ │ │ ├── executors │ │ │ │ ├── __init__.py │ │ │ │ ├── executor_test_utils_bindings.py │ │ │ │ ├── cardinality.py │ │ │ │ └── data_conversions.py │ │ │ ├── __init__.py │ │ │ ├── execution_contexts │ │ │ │ ├── __init__.py │ │ │ │ └── BUILD │ │ │ ├── executor_stacks │ │ │ │ ├── __init__.py │ │ │ │ └── executor_stack_bindings_test.py │ │ │ └── BUILD │ │ ├── BUILD │ │ ├── framework │ │ │ ├── BUILD │ │ │ └── __init__.py │ │ └── templates │ │ │ ├── __init__.py │ │ │ └── errors.py │ ├── tests │ │ ├── README.md │ │ └── temperature_sensor_example.py │ ├── analytics │ │ ├── heavy_hitters │ │ │ ├── BUILD │ │ │ ├── __init__.py │ │ │ └── iblt │ │ │ │ └── __init__.py │ │ ├── hierarchical_histogram │ │ │ ├── __init__.py │ │ │ ├── build_tree_from_leaf_test.py │ │ │ └── build_tree_from_leaf.py │ │ ├── __init__.py │ │ └── histogram_processing.py │ ├── __init__.py │ ├── simulation │ │ ├── models │ │ │ ├── __init__.py │ │ │ └── BUILD │ │ └── baselines │ │ │ ├── landmark │ │ │ └── __init__.py │ │ │ ├── stackoverflow │ │ │ ├── constants.py │ │ │ └── __init__.py │ │ │ ├── emnist │ │ │ └── __init__.py │ │ │ ├── shakespeare │ │ │ └── __init__.py │ │ │ ├── cifar100 │ │ │ └── __init__.py │ │ │ └── __init__.py │ ├── BUILD │ ├── learning │ │ ├── programs │ │ │ └── __init__.py │ │ ├── optimizers │ │ │ ├── optimizer_test_utils_test.py │ │ │ └── __init__.py │ │ ├── metrics │ │ │ └── types.py │ │ ├── client_weight_lib.py │ │ └── models │ │ │ └── __init__.py │ └── program │ │ └── __init__.py ├── cc │ ├── core │ │ └── impl │ │ │ ├── aggregation │ │ │ ├── protocol │ │ │ │ ├── checkpoint_aggregator.proto │ │ │ │ ├── checkpoint_header.h │ │ │ │ ├── python │ │ │ │ │ └── BUILD │ │ │ │ ├── federated_compute_checkpoint_builder.h │ │ │ │ ├── federated_compute_checkpoint_parser.h │ │ │ │ ├── resource_resolver.h │ │ │ │ ├── simple_aggregation │ │ │ │ │ └── cancelable_callback.h │ │ │ │ └── config_converter.h │ │ │ ├── BUILD │ │ │ ├── core │ │ │ │ ├── datatype.cc │ │ │ │ ├── fedsql_constants.h │ │ │ │ ├── intrinsic.h │ │ │ │ ├── dp_histogram_test_utils.cc │ │ │ │ ├── tensor_spec.cc │ │ │ │ ├── tensor_data.cc │ │ │ │ ├── agg_core.proto │ │ │ │ └── tensor_slice_data.cc │ │ │ ├── base │ │ │ │ ├── base_name.cc │ │ │ │ ├── base_name.h │ │ │ │ ├── monitoring.cc │ │ │ │ ├── simulated_clock.cc │ │ │ │ └── move_to_lambda_test.cc │ │ │ ├── testing │ │ │ │ └── test_data.h │ │ │ └── tensorflow │ │ │ │ ├── tensorflow_checkpoint_builder_factory.h │ │ │ │ └── tensorflow_checkpoint_parser_factory.h │ │ │ ├── BUILD │ │ │ └── executors │ │ │ ├── executor.cc │ │ │ ├── mock_executor.cc │ │ │ ├── status_conversion.h │ │ │ ├── array_shape_utils.h │ │ │ ├── session_provider_test.cc │ │ │ ├── cardinalities.cc │ │ │ ├── type_utils.h │ │ │ ├── sequence_executor.h │ │ │ ├── computations.h │ │ │ ├── reference_resolving_executor.h │ │ │ ├── status_conversion.cc │ │ │ ├── sequence_intrinsics.h │ │ │ ├── federating_executor.h │ │ │ ├── tensorflow_executor.h │ │ │ ├── data_executor.h │ │ │ ├── dataset_utils.h │ │ │ ├── tensor_serialization.h │ │ │ ├── executor_test_utils_bindings.cc │ │ │ ├── remote_executor.h │ │ │ ├── sequence_intrinsics.cc │ │ │ ├── array_shape_test_utils.h │ │ │ ├── value_validation.h │ │ │ ├── cardinalities.h │ │ │ ├── dataset_from_tensor_structures.h │ │ │ ├── streaming_remote_executor.h │ │ │ └── executor_test_base.h │ ├── testing │ │ ├── oss_test_main.cc │ │ └── BUILD │ └── simulation │ │ └── BUILD ├── data │ ├── README.md │ └── BUILD ├── proto │ ├── BUILD │ ├── v0 │ │ ├── __init__.py │ │ └── BUILD │ └── __init__.py ├── version.py └── BUILD ├── examples ├── README.md ├── datasets │ ├── BUILD │ └── load_flair.py ├── learning │ └── federated_program │ │ └── vizier │ │ ├── study_spec.textproto │ │ ├── BUILD │ │ ├── vizier_service.py │ │ └── vizier_service_test.py ├── personalization │ └── BUILD ├── custom_data_backend │ ├── BUILD │ └── data_backend_example.h ├── stateful_clients │ ├── BUILD │ └── README.md └── simple_fedavg │ └── BUILD ├── docs ├── design │ ├── TFF_102_executors.pdf │ ├── TFF_101_lingua_federata.pdf │ └── TFF_103_transformations.pdf ├── get_started.md ├── multiframework.md ├── learning │ └── federated_program_guide.md ├── collaborations │ ├── notes │ │ ├── 2022-09.29.md │ │ └── 2022-10-13.md │ └── README.md └── tutorials │ └── README.md ├── third_party ├── BUILD ├── tensorflow │ ├── BUILD │ ├── internal_visibility.patch │ └── tf2xla_visibility.patch ├── federated_language │ ├── BUILD │ ├── structure_visibility.patch │ ├── proto_library_loads.patch │ ├── numpy.patch │ └── README.md └── eigen.BUILD ├── .gitignore ├── CITATION.cff ├── BUILD ├── .github └── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md ├── tools ├── BUILD ├── python_package │ └── BUILD └── build_docs.py └── requirements-test.txt /.bazelversion: -------------------------------------------------------------------------------- 1 | 6.5.0 2 | -------------------------------------------------------------------------------- /tensorflow_federated/python/common_libs/golden_test_goldens/test_check_string_updates.expected: -------------------------------------------------------------------------------- 1 | old 2 | data 3 | -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 | # TensorFlow Federated Examples 2 | 3 | This directory contains examples of how to use TFF. 4 | -------------------------------------------------------------------------------- /tensorflow_federated/python/common_libs/golden_test_goldens/test_check_string_fails.expected: -------------------------------------------------------------------------------- 1 | what 2 | you 3 | expected 4 | -------------------------------------------------------------------------------- /tensorflow_federated/python/common_libs/golden_test_goldens/test_check_string_succeeds.expected: -------------------------------------------------------------------------------- 1 | foo 2 | bar 3 | baz 4 | fizzbuzz 5 | -------------------------------------------------------------------------------- /docs/design/TFF_102_executors.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google-parfait/tensorflow-federated/HEAD/docs/design/TFF_102_executors.pdf -------------------------------------------------------------------------------- /docs/design/TFF_101_lingua_federata.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google-parfait/tensorflow-federated/HEAD/docs/design/TFF_101_lingua_federata.pdf -------------------------------------------------------------------------------- /docs/design/TFF_103_transformations.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google-parfait/tensorflow-federated/HEAD/docs/design/TFF_103_transformations.pdf -------------------------------------------------------------------------------- /third_party/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_applicable_licenses = ["//:package_license"], 3 | default_visibility = ["//visibility:private"], 4 | ) 5 | 6 | licenses(["notice"]) 7 | -------------------------------------------------------------------------------- /tensorflow_federated/python/aggregators/README.md: -------------------------------------------------------------------------------- 1 | # TensorFlow Federated Aggregators 2 | 3 | Contact: @prachetit, @zacharygarrett 4 | 5 | For the public module description, see `__init__.py`. 6 | -------------------------------------------------------------------------------- /third_party/tensorflow/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_applicable_licenses = ["//:package_license"], 3 | default_visibility = ["//visibility:private"], 4 | ) 5 | 6 | licenses(["notice"]) 7 | -------------------------------------------------------------------------------- /third_party/federated_language/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_applicable_licenses = ["//:package_license"], 3 | default_visibility = ["//visibility:private"], 4 | ) 5 | 6 | licenses(["notice"]) 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # MacOS 2 | .DS_Store 3 | 4 | # Python 5 | *.py[cod] 6 | __pycache__/ 7 | venv/ 8 | 9 | # Bazel 10 | /.bazelrc 11 | /bazel-* 12 | /bazel_pip 13 | 14 | # Notebooks 15 | .ipynb_checkpoints 16 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/protocol/checkpoint_aggregator.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package tensorflow_federated.aggregation; 4 | 5 | // Internal state representation of a CheckpointAggregator. 6 | message CheckpointAggregatorState { 7 | repeated bytes aggregators = 1; 8 | } 9 | -------------------------------------------------------------------------------- /CITATION.cff: -------------------------------------------------------------------------------- 1 | cff-version: 1.2.0 2 | message: "If you use this software, please cite it as below." 3 | authors: 4 | - affiliation: Google 5 | given-names: "The TensorFlow Federated Authors" 6 | title: "TensorFlow Federated" 7 | version: 0.86.0 8 | date-released: 2018-12-12 9 | url: "https://github.com/google-parfait/tensorflow-federated" 10 | -------------------------------------------------------------------------------- /tensorflow_federated/data/README.md: -------------------------------------------------------------------------------- 1 | # Package Data 2 | 3 | This directory contains package data for the Python package and is installed 4 | into the package when it is created using 5 | https://github.com/google-parfait/tensorflow-federated/blob/main/pyproject.toml. See 6 | https://docs.python.org/3/distutils/setupscript.html#installing-package-data for 7 | more information. 8 | -------------------------------------------------------------------------------- /examples/datasets/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_binary") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = ["//visibility:private"], 6 | ) 7 | 8 | licenses(["notice"]) 9 | 10 | py_binary( 11 | name = "load_flair", 12 | srcs = ["load_flair.py"], 13 | deps = ["//tensorflow_federated"], 14 | ) 15 | -------------------------------------------------------------------------------- /tensorflow_federated/proto/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_library") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = ["//visibility:private"], 6 | ) 7 | 8 | licenses(["notice"]) 9 | 10 | py_library( 11 | name = "proto", 12 | srcs = ["__init__.py"], 13 | visibility = ["//tools/python_package:python_package_tool"], 14 | ) 15 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/backends/mapreduce/intrinsics_test_goldens/federated_secure_modular_sum.expected: -------------------------------------------------------------------------------- 1 | (let 2 | mod=2 3 | in federated_secure_modular_sum(< 4 | federated_map(< 5 | some_compiled_comp, 6 | federated_zip_at_clients(< 7 | federated_value_at_clients(1), 8 | federated_broadcast(federated_value_at_server(some_compiled_comp(mod))) 9 | >) 10 | >), 11 | mod 12 | >)) 13 | -------------------------------------------------------------------------------- /third_party/eigen.BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_visibility = ["//visibility:private"], 3 | ) 4 | 5 | licenses(["notice"]) 6 | 7 | cc_library( 8 | name = "eigen", 9 | hdrs = glob([ 10 | "Eigen/**", 11 | "unsupported/Eigen/**", 12 | ]), 13 | defines = [ 14 | "EIGEN_MAX_ALIGN_BYTES=64", 15 | ], 16 | includes = ["."], 17 | visibility = ["//visibility:public"], 18 | ) 19 | -------------------------------------------------------------------------------- /examples/learning/federated_program/vizier/study_spec.textproto: -------------------------------------------------------------------------------- 1 | # proto-file: https://github.com/google/vizier/blob/main/vizier/_src/service/study.proto 2 | # proto-message: StudySpec 3 | 4 | metrics: { 5 | metric_id: "eval/loss" 6 | goal: MINIMIZE 7 | } 8 | parameters: { 9 | parameter_id: "finalizer/learning_rate" 10 | double_value_spec: { 11 | min_value: 0.001 12 | max_value: 2.0 13 | } 14 | scale_type: UNIT_LOG_SCALE 15 | } 16 | -------------------------------------------------------------------------------- /tensorflow_federated/python/tests/README.md: -------------------------------------------------------------------------------- 1 | # TensorFlow Federated Tests 2 | 3 | This directory contains end-to-end tests of the TFF Python package. 4 | 5 | These tests operate at a high level; use the TFF API; and are intended to verify 6 | that the package as a whole is working. These test are expected to excercise any 7 | or all of the modules in the TFF package and may make use of external resources 8 | such as databases, files systems, and network services. 9 | -------------------------------------------------------------------------------- /third_party/federated_language/structure_visibility.patch: -------------------------------------------------------------------------------- 1 | diff --git federated_language/common_libs/BUILD federated_language/common_libs/BUILD 2 | index 5d5378f..a6cd61a 100644 3 | --- federated_language/common_libs/BUILD 4 | +++ federated_language/common_libs/BUILD 5 | @@ -91,6 +91,7 @@ py_library( 6 | py_library( 7 | name = "structure", 8 | srcs = ["structure.py"], 9 | + visibility = ["//visibility:public"], 10 | deps = [":py_typecheck"], 11 | ) 12 | 13 | -------------------------------------------------------------------------------- /third_party/tensorflow/internal_visibility.patch: -------------------------------------------------------------------------------- 1 | diff --git tensorflow/BUILD tensorflow/BUILD 2 | index 202553cd531..171eb04665c 100644 3 | --- tensorflow/BUILD 4 | +++ tensorflow/BUILD 5 | @@ -1039,7 +1039,7 @@ package_group( 6 | "//smartass/brain/configure/...", 7 | "//tensorflow/...", 8 | "//tensorflow_decision_forests/...", 9 | - "//tensorflow_federated/...", 10 | + "public", 11 | "//third_party/cloud_tpu/convergence_tools/sdc_monitoring/...", 12 | "//third_party/cloud_tpu/inference_converter/...", 13 | "//third_party/py/cloud_ml_autoflow/...", 14 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_applicable_licenses = ["//:package_license"], 3 | default_visibility = ["//visibility:private"], 4 | ) 5 | 6 | package_group( 7 | name = "impl_packages", 8 | packages = [ 9 | "//tensorflow_federated/cc/core/impl/...", 10 | "//tensorflow_federated/python/core/impl/...", 11 | ], 12 | ) 13 | 14 | package_group( 15 | name = "impl_users", 16 | includes = [ 17 | "//tensorflow_federated/cc/simulation:simulation_packages", 18 | "//tensorflow_federated/python/core/impl:impl_users", 19 | ], 20 | ) 21 | 22 | licenses(["notice"]) 23 | -------------------------------------------------------------------------------- /BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_license//rules:license.bzl", "license") 2 | 3 | package( 4 | default_applicable_licenses = [":package_license"], 5 | default_visibility = ["//visibility:public"], 6 | ) 7 | 8 | license( 9 | name = "package_license", 10 | package_name = "tensorflow_federated", 11 | license_kinds = ["@rules_license//licenses/spdx:Apache-2.0"], 12 | ) 13 | 14 | licenses(["notice"]) 15 | 16 | exports_files([ 17 | "LICENSE", 18 | "README.md", 19 | "requirements.txt", 20 | ]) 21 | 22 | filegroup( 23 | name = "pyproject_toml", 24 | srcs = ["pyproject.toml"], 25 | visibility = ["//tools/python_package:python_package_tool"], 26 | ) 27 | -------------------------------------------------------------------------------- /tensorflow_federated/python/analytics/heavy_hitters/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_library") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = ["//visibility:private"], 6 | ) 7 | 8 | package_group( 9 | name = "heavy_hitters_packages", 10 | packages = ["//tensorflow_federated/python/analytics/heavy_hitters/..."], 11 | ) 12 | 13 | licenses(["notice"]) 14 | 15 | py_library( 16 | name = "heavy_hitters", 17 | srcs = ["__init__.py"], 18 | visibility = ["//tensorflow_federated/python/analytics:__pkg__"], 19 | deps = ["//tensorflow_federated/python/analytics/heavy_hitters/iblt"], 20 | ) 21 | -------------------------------------------------------------------------------- /third_party/federated_language/proto_library_loads.patch: -------------------------------------------------------------------------------- 1 | diff --git federated_language/proto/BUILD federated_language/proto/BUILD 2 | index d23d7cf..004dcb2 100644 3 | --- federated_language/proto/BUILD 4 | +++ federated_language/proto/BUILD 5 | @@ -1,6 +1,5 @@ 6 | -load("@protobuf//bazel:cc_proto_library.bzl", "cc_proto_library") 7 | -load("@protobuf//bazel:proto_library.bzl", "proto_library") 8 | -load("@protobuf//bazel:py_proto_library.bzl", "py_proto_library") 9 | +load("@com_github_grpc_grpc//bazel:python_rules.bzl", "py_proto_library") 10 | +load("@rules_cc//cc:defs.bzl", "cc_proto_library") 11 | load("@rules_python//python:defs.bzl", "py_library") 12 | 13 | package( 14 | -------------------------------------------------------------------------------- /third_party/tensorflow/tf2xla_visibility.patch: -------------------------------------------------------------------------------- 1 | diff --git tensorflow/compiler/tf2xla/BUILD tensorflow/compiler/tf2xla/BUILD 2 | index 22d9877bed9..3f6b421465d 100644 3 | --- tensorflow/compiler/tf2xla/BUILD 4 | +++ tensorflow/compiler/tf2xla/BUILD 5 | @@ -46,7 +46,7 @@ package_group( 6 | packages = [ 7 | "//platforms/performance/automl/...", 8 | "//tensorflow/...", 9 | - "//tensorflow_federated/cc/core/impl/executors/...", 10 | + "public", 11 | "//tensorflow_models/...", 12 | "//third_party/deepmind/deepmind_research/density_functional_approximation_dm21/...", 13 | "//third_party/mlir_edge/model_curriculum/iree/...", 14 | -------------------------------------------------------------------------------- /tensorflow_federated/proto/v0/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019, The TensorFlow Federated Authors. 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 | -------------------------------------------------------------------------------- /tensorflow_federated/python/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018, The TensorFlow Federated Authors. 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 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/environments/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018, The TensorFlow Federated Authors. 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 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: enhancement 6 | assignees: '' 7 | --- 8 | 9 | **Is your feature request related to a problem? Please describe.** 10 | A clear and concise description of what the problem is. Ex. I'm always 11 | frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've 18 | considered. 19 | 20 | **Additional context** 21 | Add any other context about the feature request here. 22 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018, The TensorFlow Federated Authors. 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 | """The TensorFlow Federated core library.""" 15 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/impl/compiler/compiler_test_utils_test_goldens/test_check_computations.expected: -------------------------------------------------------------------------------- 1 | computation_1: 2 | 3 | (arg -> (let 4 | y=1, 5 | z=2 6 | in < 7 | (let 8 | v=< 9 | (let 10 | t=3 11 | in 4), 12 | (let 13 | u=5 14 | in 6) 15 | >[0] 16 | in 7), 17 | (let 18 | x=10 19 | in (let 20 | w=8 21 | in 9)) 22 | >))(11) 23 | 24 | computation_2: 25 | 26 | (arg -> (let 27 | y=1, 28 | z=2 29 | in < 30 | (let 31 | v=< 32 | (let 33 | t=3 34 | in 4), 35 | (let 36 | u=5 37 | in 6) 38 | >[0] 39 | in 7), 40 | (let 41 | x=10 42 | in (let 43 | w=8 44 | in 9)) 45 | >))(11) 46 | 47 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/impl/compiler/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019, The TensorFlow Federated Authors. 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 | """Libraries for compiling computations.""" 15 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/impl/executors/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019, The TensorFlow Federated Authors. 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 | """Libraries for executing computations.""" 15 | -------------------------------------------------------------------------------- /tensorflow_federated/version.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019, The TensorFlow Federated Authors. 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 | """TensorFlow Federated version.""" 15 | 16 | __version__ = '0.88.0' 17 | -------------------------------------------------------------------------------- /tensorflow_federated/proto/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019, The TensorFlow Federated Authors. 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 | """Protobuf libraries for use in TensorFlow Federated core library.""" 15 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/impl/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018, The TensorFlow Federated Authors. 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 | """The implementation of the TensorFlow Federated core library.""" 15 | -------------------------------------------------------------------------------- /tensorflow_federated/python/common_libs/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018, The TensorFlow Federated Authors. 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 | """Libraries that extend Python for use in TensorFlow Federated.""" 15 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/impl/execution_contexts/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Execution contexts for TensorFlow-Federated.""" 15 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/impl/executor_stacks/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019, The TensorFlow Federated Authors. 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 | """Libraries for interacting with stacks of executors.""" 15 | -------------------------------------------------------------------------------- /tensorflow_federated/python/analytics/hierarchical_histogram/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Libraries for computing hierarchical histogram.""" 15 | -------------------------------------------------------------------------------- /tensorflow_federated/data/BUILD: -------------------------------------------------------------------------------- 1 | load("@bazel_skylib//rules:copy_file.bzl", "copy_file") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = [ 6 | ":data_packages", 7 | ":data_users", 8 | ], 9 | ) 10 | 11 | package_group( 12 | name = "data_packages", 13 | packages = ["//tensorflow_federated/data/..."], 14 | ) 15 | 16 | package_group( 17 | name = "data_users", 18 | includes = [ 19 | "//tensorflow_federated/python:python_packages", 20 | "//tools:tools_packages", 21 | ], 22 | ) 23 | 24 | licenses(["notice"]) 25 | 26 | copy_file( 27 | name = "_worker_binary", 28 | src = "//tensorflow_federated/cc/simulation:worker_binary", 29 | out = "worker_binary", 30 | ) 31 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/backends/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_library") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = ["//visibility:private"], 6 | ) 7 | 8 | package_group( 9 | name = "backends_packages", 10 | packages = ["//tensorflow_federated/python/core/backends/..."], 11 | ) 12 | 13 | licenses(["notice"]) 14 | 15 | py_library( 16 | name = "backends", 17 | srcs = ["__init__.py"], 18 | visibility = ["//tensorflow_federated:__pkg__"], 19 | deps = [ 20 | "//tensorflow_federated/python/core/backends/mapreduce", 21 | "//tensorflow_federated/python/core/backends/native", 22 | "//tensorflow_federated/python/core/backends/test", 23 | ], 24 | ) 25 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/environments/tensorflow_backend/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023, The TensorFlow Federated Authors. 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 | """Libraries for interacting with a TensorFlow backend.""" 15 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/environments/tensorflow_frontend/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020, The TensorFlow Federated Authors. 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 | """Libraries for interacting with a TensorFlow frontend.""" 15 | -------------------------------------------------------------------------------- /examples/personalization/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = ["//visibility:private"], 6 | ) 7 | 8 | licenses(["notice"]) 9 | 10 | py_library( 11 | name = "p13n_utils", 12 | srcs = ["p13n_utils.py"], 13 | deps = ["//tensorflow_federated"], 14 | ) 15 | 16 | py_test( 17 | name = "p13n_utils_test", 18 | srcs = ["p13n_utils_test.py"], 19 | deps = [ 20 | ":p13n_utils", 21 | "//tensorflow_federated", 22 | ], 23 | ) 24 | 25 | py_binary( 26 | name = "emnist_p13n_main", 27 | srcs = ["emnist_p13n_main.py"], 28 | deps = [ 29 | ":p13n_utils", 30 | "//tensorflow_federated", 31 | ], 32 | ) 33 | -------------------------------------------------------------------------------- /examples/custom_data_backend/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:cc_library.bzl", "cc_library") 2 | 3 | licenses(["notice"]) 4 | 5 | cc_library( 6 | name = "data_backend_example_cc", 7 | srcs = ["data_backend_example.cc"], 8 | hdrs = ["data_backend_example.h"], 9 | deps = [ 10 | "//tensorflow_federated/cc/core/impl/executors:data_backend", 11 | "//tensorflow_federated/cc/core/impl/executors:tensorflow_utils", 12 | "//tensorflow_federated/proto/v0:executor_cc_proto", 13 | "@com_google_absl//absl/status", 14 | "@federated_language//federated_language/proto:array_cc_proto", 15 | "@federated_language//federated_language/proto:computation_cc_proto", 16 | "@org_tensorflow//tensorflow/core:framework", 17 | "@org_tensorflow//tensorflow/core:protos_all_cc", 18 | ], 19 | ) 20 | -------------------------------------------------------------------------------- /tensorflow_federated/python/simulation/models/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019, The TensorFlow Federated Authors. 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 | """Models for running TensorFlow Federated simulations.""" 15 | 16 | from tensorflow_federated.python.simulation.models import mnist 17 | -------------------------------------------------------------------------------- /tensorflow_federated/python/analytics/heavy_hitters/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Libraries for computing private heavy hitters.""" 15 | 16 | from tensorflow_federated.python.analytics.heavy_hitters import iblt 17 | -------------------------------------------------------------------------------- /tensorflow_federated/python/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_library") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = ["//visibility:private"], 6 | ) 7 | 8 | package_group( 9 | name = "python_packages", 10 | packages = ["//tensorflow_federated/python/..."], 11 | ) 12 | 13 | package_group( 14 | name = "internal_packages", 15 | packages = [ 16 | "//tensorflow_federated/python/...", 17 | 18 | # The examples and tests in TFF should depend on the public API. 19 | "-//tensorflow_federated/examples/...", 20 | "-//tensorflow_federated/python/tests/...", 21 | ], 22 | ) 23 | 24 | licenses(["notice"]) 25 | 26 | py_library( 27 | name = "python", 28 | srcs = ["__init__.py"], 29 | visibility = ["//tools/python_package:python_package_tool"], 30 | ) 31 | -------------------------------------------------------------------------------- /tensorflow_federated/python/simulation/baselines/landmark/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022, Google LLC. 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 | """Libraries for constructing baseline tasks for the GLDv2 dataset.""" 15 | 16 | from tensorflow_federated.python.simulation.baselines.landmark.landmark_tasks import create_landmark_classification_task 17 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/impl/compiler/tree_transformations_test_goldens/uniquify_names_blocks_nested_inside_of_locals.expected: -------------------------------------------------------------------------------- 1 | Before transformation: 2 | 3 | (let 4 | a=(let 5 | a=(let 6 | a=(let 7 | a=1 8 | in 1) 9 | in 1) 10 | in 1), 11 | a=(let 12 | a=(let 13 | a=(let 14 | a=a 15 | in 1) 16 | in 1) 17 | in 1) 18 | in (let 19 | a=(let 20 | a=(let 21 | a=a 22 | in 1) 23 | in 1) 24 | in 1)) 25 | 26 | After transformation: 27 | 28 | (let 29 | _var3=(let 30 | _var2=(let 31 | _var1=(let 32 | a=1 33 | in 1) 34 | in 1) 35 | in 1), 36 | _var7=(let 37 | _var6=(let 38 | _var5=(let 39 | _var4=_var3 40 | in 1) 41 | in 1) 42 | in 1) 43 | in (let 44 | _var10=(let 45 | _var9=(let 46 | _var8=_var7 47 | in 1) 48 | in 1) 49 | in 1)) 50 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/executor.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 "tensorflow_federated/cc/core/impl/executors/executor.h" 17 | 18 | namespace tensorflow_federated {} // namespace tensorflow_federated 19 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/environments/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_library") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = ["//visibility:private"], 6 | ) 7 | 8 | package_group( 9 | name = "environments_packages", 10 | packages = [ 11 | "//tensorflow_federated/python/core/environments/...", 12 | ], 13 | ) 14 | 15 | package_group( 16 | name = "environments_users", 17 | includes = [ 18 | "//tensorflow_federated/python/core:core_users", 19 | "//tensorflow_federated/python/core/backends:backends_packages", 20 | ], 21 | packages = [ 22 | "//tensorflow_federated", 23 | ], 24 | ) 25 | 26 | licenses(["notice"]) 27 | 28 | py_library( 29 | name = "environments", 30 | srcs = ["__init__.py"], 31 | visibility = ["//tools/python_package:python_package_tool"], 32 | ) 33 | -------------------------------------------------------------------------------- /tensorflow_federated/python/simulation/baselines/stackoverflow/constants.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Default values for Stack Overflow baseline tasks.""" 15 | 16 | DEFAULT_SEQUENCE_LENGTH = 20 17 | DEFAULT_SHUFFLE_BUFFER_SIZE = 1000 18 | DEFAULT_TAG_VOCAB_SIZE = 500 19 | DEFAULT_WORD_VOCAB_SIZE = 10000 20 | -------------------------------------------------------------------------------- /tools/BUILD: -------------------------------------------------------------------------------- 1 | load("@bazel_skylib//:bzl_library.bzl", "bzl_library") 2 | load("@rules_python//python:defs.bzl", "py_binary") 3 | 4 | package( 5 | default_applicable_licenses = ["//:package_license"], 6 | default_visibility = [":tools_packages"], 7 | ) 8 | 9 | package_group( 10 | name = "tools_packages", 11 | packages = ["//tools/..."], 12 | ) 13 | 14 | licenses(["notice"]) 15 | 16 | bzl_library( 17 | name = "build_defs_bzl", 18 | srcs = ["build_defs.bzl"], 19 | visibility = [ 20 | "//tensorflow_federated:__subpackages__", # Scheuklappen: keep 21 | ], 22 | deps = [ 23 | "@rules_cc//cc:core_rules", 24 | "@rules_python//python:defs.bzl", 25 | ], 26 | ) 27 | 28 | py_binary( 29 | name = "build_docs", 30 | srcs = ["build_docs.py"], 31 | ) 32 | 33 | filegroup( 34 | name = "update_version", 35 | srcs = ["update_version.sh"], 36 | tags = ["ignore_srcs"], 37 | ) 38 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/impl/executors/executor_test_utils_bindings.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023, The TensorFlow Federated Authors. 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 | """Python interface to C++ Executor test utils implementations.""" 15 | 16 | from tensorflow_federated.cc.core.impl.executors import executor_test_utils_bindings 17 | 18 | 19 | create_mock_executor = executor_test_utils_bindings.create_mock_executor 20 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 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 | package( 16 | default_applicable_licenses = ["//:package_license"], 17 | default_visibility = ["//visibility:private"], 18 | ) 19 | 20 | package_group( 21 | name = "aggregation_packages", 22 | packages = [ 23 | "//tensorflow_federated/cc/core/impl/aggregation/...", 24 | ], 25 | ) 26 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_library") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = ["//visibility:private"], 6 | ) 7 | 8 | package_group( 9 | name = "core_packages", 10 | packages = ["//tensorflow_federated/python/core/..."], 11 | ) 12 | 13 | package_group( 14 | name = "core_users", 15 | includes = [ 16 | "//tensorflow_federated/python/aggregators:aggregators_packages", 17 | "//tensorflow_federated/python/analytics:analytics_packages", 18 | "//tensorflow_federated/python/learning:learning_packages", 19 | "//tensorflow_federated/python/program:program_packages", 20 | "//tensorflow_federated/python/simulation:simulation_packages", 21 | ], 22 | ) 23 | 24 | licenses(["notice"]) 25 | 26 | py_library( 27 | name = "core", 28 | srcs = ["__init__.py"], 29 | visibility = ["//tools/python_package:python_package_tool"], 30 | ) 31 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/environments/tensorflow/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_library") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = [":tensorflow_packages"], 6 | ) 7 | 8 | package_group( 9 | name = "tensorflow_packages", 10 | packages = ["//tensorflow_federated/python/core/environments/tensorflow/..."], 11 | ) 12 | 13 | licenses(["notice"]) 14 | 15 | py_library( 16 | name = "tensorflow", 17 | srcs = ["__init__.py"], 18 | visibility = ["//tensorflow_federated:__pkg__"], 19 | deps = [ 20 | "//tensorflow_federated/python/core/environments/tensorflow_backend:tensorflow_tree_transformations", 21 | "//tensorflow_federated/python/core/environments/tensorflow_backend:type_conversions", 22 | "//tensorflow_federated/python/core/environments/tensorflow_frontend:tensorflow_computation", 23 | "//tensorflow_federated/python/core/environments/tensorflow_frontend:tensorflow_types", 24 | ], 25 | ) 26 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/environments/tensorflow_backend/tensorflow_executor_bindings.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Python interface to C++ Executor implementations.""" 15 | 16 | # Required to load TF Python extension. 17 | import tensorflow as tf # pylint: disable=unused-import 18 | 19 | from tensorflow_federated.cc.core.impl.executors import tensorflow_bindings 20 | 21 | create_tensorflow_executor = tensorflow_bindings.create_tensorflow_executor 22 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/testing/oss_test_main.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 "googletest/include/gtest/gtest.h" 19 | #include "absl/flags/parse.h" 20 | 21 | GTEST_API_ int main(int argc, char **argv) { 22 | printf("Running main() from %s\n", __FILE__); 23 | testing::InitGoogleTest(&argc, argv); 24 | absl::ParseCommandLine(argc, argv); 25 | return RUN_ALL_TESTS(); 26 | } 27 | -------------------------------------------------------------------------------- /docs/get_started.md: -------------------------------------------------------------------------------- 1 | # TensorFlow Federated 2 | 3 | The TensorFlow Federated (TFF) platform consists of two layers: 4 | 5 | * [Federated Learning (FL)](federated_learning.md), high-level interfaces to 6 | plug existing Keras or non-Keras machine learning models into the TFF 7 | framework. You can perform basic tasks, such as federated training or 8 | evaluation, without having to study the details of federated learning 9 | algorithms. 10 | * [Federated Core (FC)](federated_core.md), lower-level interfaces to 11 | concisely express custom federated algorithms by combining TensorFlow with 12 | distributed communication operators within a strongly-typed functional 13 | programming environment. 14 | 15 | Start with the [TFF tutorials](tutorials/tutorials_overview.md) that walk you 16 | through the main TFF concepts and APIs using practical examples. Make sure to 17 | follow the [installation instructions](install.md) to configure your environment 18 | for use with TFF. 19 | 20 | The more detailed guides (see the left sidebar of this page) then provide 21 | reference information on important topics. 22 | -------------------------------------------------------------------------------- /tensorflow_federated/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_library") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = ["//visibility:private"], 6 | ) 7 | 8 | licenses(["notice"]) 9 | 10 | py_library( 11 | name = "tensorflow_federated", 12 | srcs = ["__init__.py"], 13 | visibility = ["//visibility:public"], 14 | deps = [ 15 | ":version", 16 | "//tensorflow_federated/python/aggregators", 17 | "//tensorflow_federated/python/analytics", 18 | "//tensorflow_federated/python/common_libs:structure", 19 | "//tensorflow_federated/python/core/backends", 20 | "//tensorflow_federated/python/core/environments/tensorflow", 21 | "//tensorflow_federated/python/core/framework", 22 | "//tensorflow_federated/python/core/templates", 23 | "//tensorflow_federated/python/learning", 24 | "//tensorflow_federated/python/program", 25 | "//tensorflow_federated/python/simulation", 26 | ], 27 | ) 28 | 29 | py_library( 30 | name = "version", 31 | srcs = ["version.py"], 32 | ) 33 | -------------------------------------------------------------------------------- /tensorflow_federated/python/simulation/baselines/emnist/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Libraries for constructing baseline tasks for the EMNIST dataset.""" 15 | 16 | from tensorflow_federated.python.simulation.baselines.emnist.autoencoder_tasks import create_autoencoder_task 17 | from tensorflow_federated.python.simulation.baselines.emnist.char_recognition_tasks import CharacterRecognitionModel 18 | from tensorflow_federated.python.simulation.baselines.emnist.char_recognition_tasks import create_character_recognition_task 19 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/mock_executor.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2023, The TensorFlow Federated Authors. 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 "tensorflow_federated/cc/core/impl/executors/mock_executor.h" 17 | 18 | #include 19 | 20 | #include "tensorflow_federated/cc/core/impl/executors/executor.h" 21 | 22 | namespace tensorflow_federated { 23 | 24 | std::shared_ptr CreateMockExecutor() { 25 | return std::make_shared(); 26 | } 27 | 28 | } // namespace tensorflow_federated 29 | -------------------------------------------------------------------------------- /tensorflow_federated/python/simulation/baselines/shakespeare/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Libraries for constructing baseline tasks for the Shakespeare dataset.""" 15 | 16 | from tensorflow_federated.python.simulation.baselines.shakespeare.char_prediction_tasks import create_character_prediction_task 17 | from tensorflow_federated.python.simulation.baselines.shakespeare.char_prediction_tasks import DEFAULT_SEQUENCE_LENGTH 18 | from tensorflow_federated.python.simulation.baselines.shakespeare.char_prediction_tasks import VOCAB_LENGTH 19 | -------------------------------------------------------------------------------- /examples/learning/federated_program/vizier/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test") 2 | 3 | py_library( 4 | name = "data_sources", 5 | srcs = ["data_sources.py"], 6 | deps = [ 7 | "//tensorflow_federated", 8 | "@federated_language//federated_language", 9 | ], 10 | ) 11 | 12 | py_library( 13 | name = "learning_process", 14 | srcs = ["learning_process.py"], 15 | deps = ["//tensorflow_federated"], 16 | ) 17 | 18 | py_binary( 19 | name = "program", 20 | srcs = ["program.py"], 21 | data = [ 22 | ":study_spec.textproto", 23 | ], 24 | deps = [ 25 | ":data_sources", 26 | ":learning_process", 27 | ":vizier_service", 28 | "//tensorflow_federated", 29 | "@federated_language//federated_language", 30 | ], 31 | ) 32 | 33 | py_library( 34 | name = "vizier_service", 35 | srcs = ["vizier_service.py"], 36 | ) 37 | 38 | py_test( 39 | name = "vizier_service_test", 40 | srcs = ["vizier_service_test.py"], 41 | data = [ 42 | ":study_spec.textproto", 43 | ], 44 | deps = [":vizier_service"], 45 | ) 46 | -------------------------------------------------------------------------------- /docs/multiframework.md: -------------------------------------------------------------------------------- 1 | # Multi-Framework Support in TensorFlow Federated 2 | 3 | TensorFlow Federated (TFF) has been designed to support a broad range of 4 | federated computations, expressed through a combination of TFF's federated 5 | operators that model distributed communication, and local processing logic. 6 | 7 | Currently local processing logic can be expressed using TensorFlow APIs (via 8 | `@tff.tensorflow.computation`) at the frontend, and is executed via the 9 | TensorFlow runtime at the backend. However, we aim to support multiple other 10 | (non-TensorFlow) frontend and backend frameworks for local computations, 11 | including non-ML frameworks (e.g., for logic expressed in SQL or general-purpose 12 | programming languages). 13 | 14 | In this section, we'll include information on: 15 | 16 | * Mechanisms that TFF provides to support alternative frameworks, and how you 17 | can add support for your preferred type of frontend or backend to TFF. 18 | 19 | * Experimental implementations of support for non-TensorFlow frameworks, with 20 | examples. 21 | 22 | * Tentative future roadmap for graduating these capabilities beyond the 23 | experimental status. 24 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/core/datatype.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "tensorflow_federated/cc/core/impl/aggregation/core/datatype.h" 18 | 19 | namespace tensorflow_federated { 20 | namespace aggregation { 21 | 22 | namespace internal { 23 | TypeKind GetTypeKind(DataType dtype) { 24 | TypeKind type_kind = TypeKind::kUnknown; 25 | DTYPE_CASES(dtype, T, type_kind = internal::TypeTraits::type_kind); 26 | return type_kind; 27 | } 28 | 29 | } // namespace internal 30 | } // namespace aggregation 31 | } // namespace tensorflow_federated 32 | -------------------------------------------------------------------------------- /examples/stateful_clients/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = ["//visibility:private"], 6 | ) 7 | 8 | licenses(["notice"]) 9 | 10 | py_binary( 11 | name = "emnist_fedavg_main", 12 | srcs = ["emnist_fedavg_main.py"], 13 | deps = [ 14 | ":stateful_fedavg_tf", 15 | ":stateful_fedavg_tff", 16 | "//tensorflow_federated", 17 | ], 18 | ) 19 | 20 | py_library( 21 | name = "stateful_fedavg_tf", 22 | srcs = ["stateful_fedavg_tf.py"], 23 | deps = ["//tensorflow_federated"], 24 | ) 25 | 26 | py_library( 27 | name = "stateful_fedavg_tff", 28 | srcs = ["stateful_fedavg_tff.py"], 29 | deps = [ 30 | ":stateful_fedavg_tf", 31 | "//tensorflow_federated", 32 | "@federated_language//federated_language", 33 | ], 34 | ) 35 | 36 | py_test( 37 | name = "stateful_fedavg_test", 38 | size = "medium", 39 | srcs = ["stateful_fedavg_test.py"], 40 | deps = [ 41 | ":stateful_fedavg_tf", 42 | ":stateful_fedavg_tff", 43 | "//tensorflow_federated", 44 | ], 45 | ) 46 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | --- 8 | 9 | **Describe the bug** 10 | A clear and concise description of what the bug is. It is often helpful to 11 | provide a link to a [colab](https://colab.research.google.com/) notebook that 12 | reproduces the bug. 13 | 14 | **Environment (please complete the following information):** 15 | * OS Platform and Distribution (e.g., Linux Ubuntu 16.04): 16 | * Python package versions (e.g., TensorFlow Federated, TensorFlow): 17 | * Python version: 18 | * Bazel version (if building from source): 19 | * CUDA/cuDNN version: 20 | * What TensorFlow Federated execution stack are you using? 21 | 22 | Note: You can collect the Python package information by running `pip3 freeze` 23 | from the command line and most of the other information can be collected using 24 | TensorFlows environment capture 25 | [script](https://github.com/tensorflow/tensorflow/blob/master/tools/tf_env_collect.sh). 26 | 27 | **Expected behavior** 28 | A clear and concise description of what you expected to happen. 29 | 30 | **Additional context** 31 | Add any other context about the problem here. 32 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/protocol/checkpoint_header.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_CHECKPOINT_HEADER_H_ 18 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_CHECKPOINT_HEADER_H_ 19 | 20 | namespace tensorflow_federated::aggregation { 21 | inline constexpr const char kFederatedComputeCheckpointHeader[] = "FCv1"; 22 | 23 | } // namespace tensorflow_federated::aggregation 24 | 25 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_CHECKPOINT_HEADER_H_ 26 | -------------------------------------------------------------------------------- /examples/simple_fedavg/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = ["//visibility:private"], 6 | ) 7 | 8 | licenses(["notice"]) 9 | 10 | py_binary( 11 | name = "emnist_fedavg_main", 12 | srcs = ["emnist_fedavg_main.py"], 13 | deps = [ 14 | ":simple_fedavg_tff", 15 | "//tensorflow_federated", 16 | ], 17 | ) 18 | 19 | py_library( 20 | name = "simple_fedavg_tf", 21 | srcs = ["simple_fedavg_tf.py"], 22 | deps = ["//tensorflow_federated"], 23 | ) 24 | 25 | py_library( 26 | name = "simple_fedavg_tff", 27 | srcs = ["simple_fedavg_tff.py"], 28 | deps = [ 29 | ":simple_fedavg_tf", 30 | "//tensorflow_federated", 31 | "@federated_language//federated_language", 32 | ], 33 | ) 34 | 35 | py_test( 36 | name = "simple_fedavg_test", 37 | size = "medium", 38 | srcs = ["simple_fedavg_test.py"], 39 | shard_count = 3, 40 | tags = ["nokokoro"], 41 | deps = [ 42 | ":simple_fedavg_tf", 43 | ":simple_fedavg_tff", 44 | "//tensorflow_federated", 45 | "@federated_language//federated_language", 46 | ], 47 | ) 48 | -------------------------------------------------------------------------------- /tensorflow_federated/python/simulation/baselines/cifar100/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Libraries for constructing baseline tasks for the CIFAR-100 dataset.""" 15 | 16 | from tensorflow_federated.python.simulation.baselines.cifar100.image_classification_tasks import create_image_classification_task 17 | from tensorflow_federated.python.simulation.baselines.cifar100.image_classification_tasks import DEFAULT_CROP_HEIGHT 18 | from tensorflow_federated.python.simulation.baselines.cifar100.image_classification_tasks import DEFAULT_CROP_WIDTH 19 | from tensorflow_federated.python.simulation.baselines.cifar100.image_classification_tasks import ResnetModel 20 | -------------------------------------------------------------------------------- /tensorflow_federated/python/learning/programs/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022, The TensorFlow Federated Authors. 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 | """Package of methods for compositional federated program logic.""" 15 | 16 | from tensorflow_federated.python.learning.programs.evaluation_program_logic import EvaluationManager 17 | from tensorflow_federated.python.learning.programs.evaluation_program_logic import extract_and_rewrap_metrics 18 | from tensorflow_federated.python.learning.programs.training_program_logic import ProgramState 19 | from tensorflow_federated.python.learning.programs.training_program_logic import train_model 20 | from tensorflow_federated.python.learning.programs.vizier_program_logic import train_model_with_vizier 21 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/framework/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_library") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = [":framework_packages"], 6 | ) 7 | 8 | package_group( 9 | name = "framework_packages", 10 | packages = ["//tensorflow_federated/python/core/framework/..."], 11 | ) 12 | 13 | licenses(["notice"]) 14 | 15 | py_library( 16 | name = "framework", 17 | srcs = ["__init__.py"], 18 | visibility = ["//tensorflow_federated:__pkg__"], 19 | deps = [ 20 | "//tensorflow_federated/python/core/impl/compiler:transformations", 21 | "//tensorflow_federated/python/core/impl/execution_contexts:mergeable_comp_execution_context", 22 | "//tensorflow_federated/python/core/impl/executor_stacks:executor_factory", 23 | "//tensorflow_federated/python/core/impl/executor_stacks:python_executor_stacks", 24 | "//tensorflow_federated/python/core/impl/executors:remote_executor", 25 | "//tensorflow_federated/python/core/impl/executors:remote_executor_grpc_stub", 26 | "//tensorflow_federated/python/core/impl/executors:remote_executor_stub", 27 | "//tensorflow_federated/python/core/impl/executors:value_serialization", 28 | ], 29 | ) 30 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/backends/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018, The TensorFlow Federated Authors. 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 | """Backends for constructing, compiling, and executing computations. 15 | 16 | Computations expressed in TFF can be executed on a variety of backends, 17 | including native backends that implement TFF's interfaces such as 18 | `federated_language.framework.Executor`, as well as custom non-native backends 19 | such as MapReduce-like systems that may only be able to run a subset of 20 | computations expressibe in TFF. 21 | """ 22 | 23 | from tensorflow_federated.python.core.backends import mapreduce 24 | from tensorflow_federated.python.core.backends import native 25 | from tensorflow_federated.python.core.backends import test 26 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/status_conversion.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_STATUS_CONVERSION_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_STATUS_CONVERSION_H_ 18 | 19 | #include "absl/status/status.h" 20 | #include "include/grpcpp/support/status.h" 21 | 22 | namespace tensorflow_federated { 23 | 24 | absl::Status grpc_to_absl(grpc::Status status); 25 | grpc::Status absl_to_grpc(absl::Status status); 26 | 27 | } // namespace tensorflow_federated 28 | 29 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_STATUS_CONVERSION_H_ 30 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/array_shape_utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_ARRAY_SHAPE_UTILS_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_ARRAY_SHAPE_UTILS_H_ 18 | 19 | #include "federated_language/proto/array.pb.h" 20 | 21 | namespace tensorflow_federated { 22 | 23 | inline bool IsScalar(const federated_language::ArrayShape& shape) { 24 | return shape.dim().empty() && !shape.unknown_rank(); 25 | } 26 | 27 | } // namespace tensorflow_federated 28 | 29 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_ARRAY_SHAPE_UTILS_H_ 30 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/backends/test/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018, The TensorFlow Federated Authors. 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 | """Libraries for interacting with test backends.""" 15 | 16 | from tensorflow_federated.python.core.backends.test.compiler import replace_secure_intrinsics_with_bodies 17 | from tensorflow_federated.python.core.backends.test.execution_contexts import create_async_test_cpp_execution_context 18 | from tensorflow_federated.python.core.backends.test.execution_contexts import create_sync_test_cpp_execution_context 19 | from tensorflow_federated.python.core.backends.test.execution_contexts import set_async_test_cpp_execution_context 20 | from tensorflow_federated.python.core.backends.test.execution_contexts import set_sync_test_cpp_execution_context 21 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/session_provider_test.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 "tensorflow_federated/cc/core/impl/executors/session_provider.h" 17 | 18 | #include 19 | 20 | #include "googletest/include/gtest/gtest.h" 21 | #include "tensorflow_federated/cc/testing/status_matchers.h" 22 | 23 | namespace tensorflow_federated { 24 | 25 | namespace { 26 | 27 | TEST(SessionProviderTest, TestStandaloneTakeSession) { 28 | tensorflow::GraphDef graphdef_pb; 29 | SessionProvider session_provider(std::move(graphdef_pb)); 30 | TFF_ASSERT_OK(session_provider.TakeSession()); 31 | } 32 | 33 | } // namespace 34 | } // namespace tensorflow_federated 35 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/cardinalities.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 "tensorflow_federated/cc/core/impl/executors/cardinalities.h" 17 | 18 | #include "absl/status/status.h" 19 | #include "absl/status/statusor.h" 20 | 21 | namespace tensorflow_federated { 22 | 23 | absl::StatusOr NumClientsFromCardinalities( 24 | const CardinalityMap& cardinalities) { 25 | auto entry_iter = cardinalities.find(kClientsUri); 26 | if (entry_iter == cardinalities.end()) { 27 | return absl::NotFoundError( 28 | "No cardinality provided for `clients` placement."); 29 | } 30 | return entry_iter->second; 31 | } 32 | 33 | } // namespace tensorflow_federated 34 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/type_utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2023, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_TYPE_UTILS_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_TYPE_UTILS_H_ 18 | 19 | #include "absl/status/statusor.h" 20 | #include "federated_language/proto/computation.pb.h" 21 | #include "tensorflow_federated/proto/v0/executor.pb.h" 22 | 23 | namespace tensorflow_federated { 24 | 25 | absl::StatusOr InferTypeFromValue( 26 | const v0::Value& value_pb); 27 | 28 | } // namespace tensorflow_federated 29 | 30 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_TYPE_UTILS_H_ 31 | -------------------------------------------------------------------------------- /tensorflow_federated/python/simulation/baselines/stackoverflow/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Libraries for constructing baseline tasks for the Stack Overflow dataset.""" 15 | 16 | from tensorflow_federated.python.simulation.baselines.stackoverflow.constants import DEFAULT_SEQUENCE_LENGTH 17 | from tensorflow_federated.python.simulation.baselines.stackoverflow.constants import DEFAULT_TAG_VOCAB_SIZE 18 | from tensorflow_federated.python.simulation.baselines.stackoverflow.constants import DEFAULT_WORD_VOCAB_SIZE 19 | from tensorflow_federated.python.simulation.baselines.stackoverflow.tag_prediction_tasks import create_tag_prediction_task 20 | from tensorflow_federated.python.simulation.baselines.stackoverflow.word_prediction_tasks import create_word_prediction_task 21 | -------------------------------------------------------------------------------- /requirements-test.txt: -------------------------------------------------------------------------------- 1 | absl-py==1.4.0 2 | astunparse==1.6.3 3 | attrs==23.1.0 4 | cachetools==5.5.2 5 | certifi==2025.7.14 6 | charset-normalizer==3.4.2 7 | cloudpickle==3.1.1 8 | decorator==5.2.1 9 | dm-tree==0.1.8 10 | dp-accounting==0.4.3 11 | flatbuffers==25.2.10 12 | gast==0.6.0 13 | google-auth==2.40.3 14 | google-auth-oauthlib==1.0.0 15 | google-pasta==0.2.0 16 | google-vizier==0.1.11 17 | googleapis-common-protos==1.61.0 18 | greenlet==3.2.3 19 | grpcio==1.73.1 20 | grpcio-tools==1.62.3 21 | h5py==3.14.0 22 | idna==3.10 23 | joblib==1.5.1 24 | keras==2.14.0 25 | libclang==18.1.1 26 | markdown==3.8.2 27 | markupsafe==3.0.2 28 | ml-dtypes==0.2.0 29 | mpmath==1.3.0 30 | numpy==1.25.2 31 | oauthlib==3.3.1 32 | opt-einsum==3.4.0 33 | packaging==22.0 34 | portpicker==1.6.0 35 | protobuf==4.25.8 36 | psutil==7.0.0 37 | pyasn1==0.6.1 38 | pyasn1-modules==0.4.2 39 | requests==2.32.4 40 | requests-oauthlib==2.0.0 41 | rsa==4.9.1 42 | scikit-learn==1.7.0 43 | scipy==1.9.3 44 | six==1.17.0 45 | sqlalchemy==1.4.20 46 | tensorboard==2.14.1 47 | tensorboard-data-server==0.7.2 48 | tensorflow==2.19.0 49 | tensorflow-estimator==2.14.0 50 | tensorflow-io-gcs-filesystem==0.37.1 51 | tensorflow-privacy==0.9.0 52 | tensorflow-probability==0.22.1 53 | termcolor==3.1.0 54 | threadpoolctl==3.6.0 55 | tqdm==4.67.1 56 | typing-extensions==4.5.0 57 | urllib3==2.5.0 58 | werkzeug==3.1.3 59 | wheel==0.45.1 60 | wrapt==1.14.1 61 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/impl/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_library") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = ["//visibility:private"], 6 | ) 7 | 8 | package_group( 9 | name = "impl_packages", 10 | packages = ["//tensorflow_federated/python/core/impl/..."], 11 | ) 12 | 13 | package_group( 14 | name = "impl_users", 15 | includes = [ 16 | "//tensorflow_federated/python/aggregators:aggregators_packages", 17 | "//tensorflow_federated/python/analytics:analytics_packages", 18 | "//tensorflow_federated/python/core/backends:backends_packages", 19 | "//tensorflow_federated/python/core/environments:environments_packages", 20 | "//tensorflow_federated/python/core/framework:framework_packages", 21 | "//tensorflow_federated/python/core/templates:templates_packages", 22 | "//tensorflow_federated/python/learning:learning_packages", 23 | "//tensorflow_federated/python/program:program_packages", 24 | "//tensorflow_federated/python/simulation:simulation_packages", 25 | ], 26 | packages = [ 27 | "//tensorflow_federated", 28 | ], 29 | ) 30 | 31 | licenses(["notice"]) 32 | 33 | py_library( 34 | name = "impl", 35 | srcs = ["__init__.py"], 36 | visibility = ["//tools/python_package:python_package_tool"], 37 | ) 38 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/sequence_executor.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_SEQUENCE_EXECUTOR_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_SEQUENCE_EXECUTOR_H_ 18 | 19 | #include 20 | 21 | #include "tensorflow_federated/cc/core/impl/executors/executor.h" 22 | 23 | namespace tensorflow_federated { 24 | 25 | // Returns an executor that can execute TFF's Sequence* intrinsics. 26 | std::shared_ptr CreateSequenceExecutor( 27 | std::shared_ptr target_executor); 28 | 29 | } // namespace tensorflow_federated 30 | 31 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_SEQUENCE_EXECUTOR_H_ 32 | -------------------------------------------------------------------------------- /tensorflow_federated/python/simulation/models/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_library", "py_test") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = [ 6 | ":models_packages", 7 | "//tensorflow_federated/python/simulation:simulation_users", 8 | "//tensorflow_federated/python/simulation/baselines:baselines_packages", 9 | ], 10 | ) 11 | 12 | package_group( 13 | name = "models_packages", 14 | packages = ["//tensorflow_federated/python/simulation/models/..."], 15 | ) 16 | 17 | licenses(["notice"]) 18 | 19 | py_library( 20 | name = "models", 21 | srcs = ["__init__.py"], 22 | visibility = ["//tensorflow_federated/python/simulation:__pkg__"], 23 | deps = [":mnist"], 24 | ) 25 | 26 | py_library( 27 | name = "mnist", 28 | srcs = ["mnist.py"], 29 | deps = ["//tensorflow_federated/python/learning/metrics:counters"], 30 | ) 31 | 32 | py_library( 33 | name = "mobilenet_v2", 34 | srcs = ["mobilenet_v2.py"], 35 | ) 36 | 37 | py_test( 38 | name = "mobilenet_v2_test", 39 | srcs = ["mobilenet_v2_test.py"], 40 | deps = [":mobilenet_v2"], 41 | ) 42 | 43 | py_library( 44 | name = "resnet_models", 45 | srcs = ["resnet_models.py"], 46 | ) 47 | 48 | py_test( 49 | name = "resnet_models_test", 50 | srcs = ["resnet_models_test.py"], 51 | deps = [":resnet_models"], 52 | ) 53 | -------------------------------------------------------------------------------- /docs/learning/federated_program_guide.md: -------------------------------------------------------------------------------- 1 | # Learning Federated Program Developer Guide 2 | 3 | This documentation is for anyone who is interested in authoring 4 | [federated program logic](https://github.com/google-parfait/tensorflow-federated/blob/main/docs/program/federated_program.md#program-logic) 5 | in 6 | [`tff.learning`](https://www.tensorflow.org/federated/api_docs/python/tff/learning). 7 | It assumes knowledge of `tff.learning` and the 8 | [Federated Program Developer Guide](https://github.com/google-parfait/tensorflow-federated/blob/main/docs/program/guide.md). 9 | 10 | [TOC] 11 | 12 | ## Program Logic 13 | 14 | This section defines guidelines for how 15 | [program logic](https://github.com/google-parfait/tensorflow-federated/blob/main/docs/program/federated_program.md#program-logic) 16 | should be authored **in `tff.learning`**. 17 | 18 | ### Learning Components 19 | 20 | **Do** use learning components in program logic (e.g. 21 | [`tff.learning.templates.LearningProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/templates/LearningProcess) 22 | and 23 | [`tff.learning.programs.EvaluationManager`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/programs/EvaluationManager)). 24 | 25 | ## Program 26 | 27 | Typically 28 | [programs](https://github.com/google-parfait/tensorflow-federated/blob/main/docs/program/federated_program.md#programs) 29 | are not authored in `tff.learning`. 30 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/templates/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018, The TensorFlow Federated Authors. 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 | """Templates for commonly used computations.""" 15 | 16 | from tensorflow_federated.python.core.templates.aggregation_process import AggregationProcess 17 | from tensorflow_federated.python.core.templates.estimation_process import EstimationProcess 18 | from tensorflow_federated.python.core.templates.iterative_process import IterativeProcess 19 | from tensorflow_federated.python.core.templates.measured_process import chain_measured_processes 20 | from tensorflow_federated.python.core.templates.measured_process import concatenate_measured_processes 21 | from tensorflow_federated.python.core.templates.measured_process import MeasuredProcess 22 | from tensorflow_federated.python.core.templates.measured_process import MeasuredProcessOutput 23 | -------------------------------------------------------------------------------- /tensorflow_federated/python/simulation/baselines/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Libraries for constructing baseline learning tasks suitable for simulation.""" 15 | 16 | from tensorflow_federated.python.simulation.baselines import cifar100 17 | from tensorflow_federated.python.simulation.baselines import emnist 18 | from tensorflow_federated.python.simulation.baselines import landmark 19 | from tensorflow_federated.python.simulation.baselines import shakespeare 20 | from tensorflow_federated.python.simulation.baselines import stackoverflow 21 | from tensorflow_federated.python.simulation.baselines.baseline_task import BaselineTask 22 | from tensorflow_federated.python.simulation.baselines.client_spec import ClientSpec 23 | from tensorflow_federated.python.simulation.baselines.task_data import BaselineTaskDatasets 24 | -------------------------------------------------------------------------------- /tensorflow_federated/proto/v0/BUILD: -------------------------------------------------------------------------------- 1 | load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library") 2 | load("@com_github_grpc_grpc//bazel:python_rules.bzl", "py_grpc_library", "py_proto_library") 3 | load("@rules_cc//cc:defs.bzl", "cc_proto_library") 4 | load("@rules_python//python:defs.bzl", "py_library") 5 | 6 | package( 7 | default_applicable_licenses = ["//:package_license"], 8 | default_visibility = ["//visibility:public"], 9 | ) 10 | 11 | licenses(["notice"]) 12 | 13 | py_library( 14 | name = "v0", 15 | srcs = ["__init__.py"], 16 | visibility = ["//tools/python_package:python_package_tool"], 17 | ) 18 | 19 | proto_library( 20 | name = "executor_proto", 21 | srcs = ["executor.proto"], 22 | deps = [ 23 | "@federated_language//federated_language/proto:array_proto", 24 | "@federated_language//federated_language/proto:computation_proto", 25 | ], 26 | ) 27 | 28 | py_proto_library( 29 | name = "executor_py_pb2", 30 | deps = [":executor_proto"], 31 | ) 32 | 33 | py_grpc_library( 34 | name = "executor_py_pb2_grpc", 35 | srcs = [":executor_proto"], 36 | deps = [":executor_py_pb2"], 37 | ) 38 | 39 | cc_proto_library( 40 | name = "executor_cc_proto", 41 | deps = [":executor_proto"], 42 | ) 43 | 44 | cc_grpc_library( 45 | name = "executor_cc_grpc_proto", 46 | srcs = [":executor_proto"], 47 | grpc_only = True, 48 | deps = [":executor_cc_proto"], 49 | ) 50 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/base/base_name.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "tensorflow_federated/cc/core/impl/aggregation/base/base_name.h" 18 | 19 | #include 20 | #include 21 | 22 | namespace tensorflow_federated { 23 | 24 | #ifdef _WIN32 25 | constexpr char kPathSeparator = '\\'; 26 | #else 27 | constexpr char kPathSeparator = '/'; 28 | #endif 29 | 30 | std::string BaseName(const std::string& path) { 31 | // Note: the code below needs to be compatible with baremetal build with 32 | // nanolibc. Therefore it is implemented via the standard "C" library strrchr. 33 | const char* separator_ptr = strrchr(path.c_str(), kPathSeparator); 34 | if (separator_ptr == nullptr) return path; 35 | 36 | return path.substr((separator_ptr - path.c_str()) + 1); 37 | } 38 | 39 | } // namespace tensorflow_federated 40 | -------------------------------------------------------------------------------- /tensorflow_federated/python/analytics/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Libraries for using Federated Analytics algorithms.""" 15 | 16 | from tensorflow_federated.python.analytics import count_distinct 17 | from tensorflow_federated.python.analytics import data_processing 18 | from tensorflow_federated.python.analytics import differential_privacy 19 | from tensorflow_federated.python.analytics import heavy_hitters 20 | from tensorflow_federated.python.analytics import histogram_processing 21 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.iblt_factory import IbltFactory 22 | from tensorflow_federated.python.analytics.hierarchical_histogram.hierarchical_histogram_decoder import HierarchicalHistogramDecoder 23 | from tensorflow_federated.python.analytics.hierarchical_histogram.hierarchical_histogram_lib import build_hierarchical_histogram_process 24 | -------------------------------------------------------------------------------- /docs/collaborations/notes/2022-09.29.md: -------------------------------------------------------------------------------- 1 | # Notes from the SIG Federated meeting on 9/29/2022 2 | 3 | * TFF and OpenMined (now joining the SIG) collaborating 4 | * Companion blog posts just published this week 5 | * https://blog.tensorflow.org/2022/09/bridging-communities-tensorflow-federated-tff-and-openmined.html 6 | * https://blog.openmined.org/announcing-proof-of-concept-support-for-tff-in-pysyft-0-7/ 7 | * Similarities vs. differences compared to what LinkedIn and others are 8 | building? 9 | * Both logical vs. physical isolation are important in scenarios of 10 | interest to LinkedIn 11 | * Jeremy: interested mostly in classical x-silo (not this prototype; 12 | coming later) 13 | * Deep dive in next meeting - schedules for 10/13/2022 14 | * Continued from September 22 15 | * Performance discussion 16 | * Observing about 2x penalty in scenatoips with O(1000s) or rounds 17 | * Suspects: TFF setup cost, communication 18 | * Overlapping data ingestion and computation likely to help 19 | * Code to support this upcoming 20 | * https://github.com/google-parfait/tensorflow-federated/blob/main/tensorflow\_federated/python/program/prefetching\_data\_source.py 21 | * Discussion continued on the Discord server 22 | * Versioning - also relevant to OpenMined (need to resolve 3-way) 23 | * To discuss at a future meeting 24 | -------------------------------------------------------------------------------- /examples/stateful_clients/README.md: -------------------------------------------------------------------------------- 1 | # Demo of Stateful Clients in Federated Averaging 2 | 3 | This is an example project demonstrating how to implment stateful clients in TFF 4 | simulation. Note that client states are generally discouraged in cross-device 5 | federated learning (see definition in 6 | [Advances and Open Problems in Federated Learning](https://arxiv.org/abs/1912.04977)) 7 | because of the large number of total clients and the intention for privacy 8 | protection. 9 | 10 | This project is based on the standalone implementaion of Federated Averaging 11 | algorithm in 12 | [`simple_fedavg`](https://github.com/google-parfait/tensorflow-federated/blob/main/examples/simple_fedavg). 13 | We introduce a coutner on each client, which tracks the total number of 14 | iterations for model training on the clients. For example, if client A has been 15 | sampled m times at round n, and each time local model training has been run with 16 | q iterations, then the counter in client A state would be q\*m at round n. On 17 | the server, we also aggregate the total number of iterations for all the clients 18 | sampled in this round. 19 | 20 | The client states are stored in memory in the current implementation. It would 21 | become challenging if the size of as single client state and/or the number of 22 | clients become too large. A possible extension is to use high performance IO to 23 | efficiently save and load client states on disk. We would encourge readers to 24 | explore and contribute. 25 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/base/base_name.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_BASE_BASE_NAME_H_ 18 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_BASE_BASE_NAME_H_ 19 | 20 | #include 21 | 22 | #include "absl/strings/string_view.h" 23 | 24 | namespace tensorflow_federated { 25 | 26 | /** 27 | * Returns the file base name of a path. 28 | */ 29 | std::string BaseName(const std::string& path); 30 | 31 | inline std::string BaseName(const char* path) { 32 | return BaseName(std::string(path)); 33 | } 34 | 35 | inline std::string BaseName(absl::string_view path) { 36 | return BaseName(std::string(path)); 37 | } 38 | 39 | } // namespace tensorflow_federated 40 | 41 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_BASE_BASE_NAME_H_ 42 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/computations.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_COMPUTATIONS_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_COMPUTATIONS_H_ 18 | 19 | #include "federated_language/proto/computation.pb.h" 20 | 21 | namespace tensorflow_federated { 22 | 23 | inline federated_language::Computation IdentityComp() { 24 | federated_language::Computation comp; 25 | federated_language::Lambda* lambda = comp.mutable_lambda(); 26 | *lambda->mutable_parameter_name() = "x"; 27 | *lambda->mutable_result()->mutable_reference()->mutable_name() = "x"; 28 | return comp; 29 | } 30 | 31 | } // namespace tensorflow_federated 32 | 33 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_COMPUTATIONS_H_ 34 | -------------------------------------------------------------------------------- /docs/collaborations/README.md: -------------------------------------------------------------------------------- 1 | # TensorFlow Federated Collaborations 2 | 3 | ## Welcome! 4 | 5 | One of the key goals for TensorFlow Federated is to serve as a platform for 6 | collaborations and joint development in the open between Google and the OSS 7 | community and industry partners. 8 | 9 | To follow conversations among developers contributing to the TFF ecosystem, 10 | please join our [Discord server](https://discord.com/invite/5shux83qZ5). All 11 | content on the Discord server is open to access by all interested parties. 12 | 13 | By default, you can only view, but not post. Posting is restricted to committed 14 | contributors. In order to actively participate as a contributor, please email 15 | tff-hello at google dot com to introduce yourself and tell us a few words about 16 | how you would like to contribute to the TFF ecosystem. The expectation of all 17 | contributors is to make regular engineering contributions. 18 | 19 | Information about workstreams and participants, as well as any artifacts that 20 | emerge during the course of conversations, including notes from meetings, 21 | project plans, design proposals, product and development roadmaps jointly 22 | developed with partners, and pointers to components maintained by community 23 | contributors, will be hosted on or linked from the Discord server. Some of the 24 | documents may be shareable by request. 25 | 26 | We hope that you will join us and help us develop the TFF platform together. 27 | 28 | Looking forward to hearing from you! 29 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/testing/test_data.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_TESTING_TEST_DATA_H_ 18 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_TESTING_TEST_DATA_H_ 19 | 20 | #include 21 | #include 22 | 23 | #include "tensorflow_federated/cc/core/impl/aggregation/core/mutable_vector_data.h" 24 | 25 | namespace tensorflow_federated::aggregation { 26 | 27 | // Creates test tensor data based on a vector. 28 | template 29 | std::unique_ptr> CreateTestData( 30 | std::initializer_list values) { 31 | return std::make_unique>(values); 32 | } 33 | 34 | } // namespace tensorflow_federated::aggregation 35 | 36 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_TESTING_TEST_DATA_H_ 37 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/reference_resolving_executor.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_REFERENCE_RESOLVING_EXECUTOR_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_REFERENCE_RESOLVING_EXECUTOR_H_ 18 | 19 | #include 20 | 21 | #include "tensorflow_federated/cc/core/impl/executors/executor.h" 22 | 23 | namespace tensorflow_federated { 24 | 25 | // Returns an executor that can resolve Lambdas and References, relying on 26 | // a child executor to complete the computation. 27 | std::shared_ptr CreateReferenceResolvingExecutor( 28 | std::shared_ptr child); 29 | 30 | } // namespace tensorflow_federated 31 | 32 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_REFERENCE_RESOLVING_EXECUTOR_H_ 33 | -------------------------------------------------------------------------------- /tensorflow_federated/python/learning/optimizers/optimizer_test_utils_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | import tensorflow as tf 16 | 17 | from tensorflow_federated.python.learning.optimizers import optimizer_test_utils 18 | 19 | 20 | class OptimizerTestUtilsTest(tf.test.TestCase): 21 | 22 | def test_test_problem(self): 23 | init_w, fn, grad_fn = optimizer_test_utils.test_quadratic_problem() 24 | weights = init_w() 25 | 26 | # Function value in initial point is not negligible and gradient non-zero. 27 | self.assertGreater(fn(weights), 5.0) 28 | self.assertGreater(tf.linalg.norm(grad_fn(weights)), 0.01) 29 | 30 | # All-zeros is optimum with function value of 0.0. 31 | self.assertAllClose(0.0, fn(tf.zeros_like(weights))[0, 0]) 32 | self.assertAllClose(tf.zeros_like(weights), grad_fn(tf.zeros_like(weights))) 33 | 34 | 35 | if __name__ == '__main__': 36 | tf.test.main() 37 | -------------------------------------------------------------------------------- /tensorflow_federated/python/learning/metrics/types.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023, The TensorFlow Federated Authors. 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 | """Typing information for metrics finalizers.""" 15 | 16 | import collections 17 | from collections.abc import Callable 18 | from typing import Any, Optional, Protocol 19 | 20 | import federated_language 21 | 22 | 23 | MetricFinalizersType = collections.OrderedDict[str, Callable[[Any], Any]] 24 | MetricsState = collections.OrderedDict[str, Any] 25 | FunctionalMetricFinalizersType = Callable[[MetricsState], MetricsState] 26 | 27 | 28 | # TODO: b/319261270 - delete the local_unfinalized_metrics_type entirely. 29 | class MetricsAggregatorType(Protocol): 30 | 31 | def __call__( 32 | self, 33 | metric_finalizers: MetricFinalizersType, 34 | local_unfinalized_metrics_type: Optional[ 35 | federated_language.StructWithPythonType 36 | ] = None, 37 | ) -> federated_language.framework.Computation: 38 | ... 39 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/impl/execution_contexts/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_python//python:defs.bzl", "py_library", "py_test") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = [ 6 | ":execution_contexts_packages", 7 | "//tensorflow_federated/python/core/impl:impl_users", 8 | ], 9 | ) 10 | 11 | package_group( 12 | name = "execution_contexts_packages", 13 | packages = ["//tensorflow_federated/python/core/impl/execution_contexts/..."], 14 | ) 15 | 16 | licenses(["notice"]) 17 | 18 | py_library( 19 | name = "execution_contexts", 20 | srcs = ["__init__.py"], 21 | visibility = ["//tools/python_package:python_package_tool"], 22 | ) 23 | 24 | py_library( 25 | name = "mergeable_comp_execution_context", 26 | srcs = ["mergeable_comp_execution_context.py"], 27 | deps = [ 28 | "//tensorflow_federated/python/common_libs:async_utils", 29 | "//tensorflow_federated/python/common_libs:py_typecheck", 30 | "//tensorflow_federated/python/common_libs:structure", 31 | "@federated_language//federated_language", 32 | ], 33 | ) 34 | 35 | py_test( 36 | name = "mergeable_comp_execution_context_test", 37 | size = "small", 38 | timeout = "moderate", 39 | srcs = ["mergeable_comp_execution_context_test.py"], 40 | deps = [ 41 | ":mergeable_comp_execution_context", 42 | "//tensorflow_federated/python/common_libs:structure", 43 | "@federated_language//federated_language", 44 | ], 45 | ) 46 | -------------------------------------------------------------------------------- /third_party/federated_language/numpy.patch: -------------------------------------------------------------------------------- 1 | diff --git federated_language/types/dtype_utils.py federated_language/types/dtype_utils.py 2 | index 59f8937..342fbee 100644 3 | --- federated_language/types/dtype_utils.py 4 | +++ federated_language/types/dtype_utils.py 5 | @@ -102,15 +102,21 @@ def can_cast( 6 | dtype: The dtype to check against. 7 | """ 8 | 9 | - # When encountering an overflow, numpy issues a `RuntimeWarning` for floating 10 | - # dtypes and raises an `OverflowError` for integer dtypes. 11 | - with warnings.catch_warnings(): 12 | - warnings.simplefilter(action='error', category=RuntimeWarning) 13 | - try: 14 | - np.asarray(value, dtype=dtype) 15 | - return True 16 | - except (OverflowError, RuntimeWarning): 17 | - return False 18 | + # `np.can_cast` does not support Python scalars (since version 2.0). Casting 19 | + # the value to a numpy value and testing for an overflow is equivalent to 20 | + # testing the Python value. 21 | + numpy_version = tuple(int(x) for x in np.__version__.split('.')) 22 | + if numpy_version >= (2, 0): 23 | + # When encountering an overflow, numpy issues a `RuntimeWarning` for 24 | + # floating dtypes and raises an `OverflowError` for integer dtypes. 25 | + with warnings.catch_warnings(action='error', category=RuntimeWarning): 26 | + try: 27 | + np.asarray(value, dtype=dtype) 28 | + return True 29 | + except (OverflowError, RuntimeWarning): 30 | + return False 31 | + else: 32 | + return np.can_cast(value, dtype) 33 | 34 | 35 | def infer_dtype( 36 | -------------------------------------------------------------------------------- /tensorflow_federated/python/learning/client_weight_lib.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Enums for client weighting in learning methods.""" 15 | 16 | from collections.abc import Callable 17 | import enum 18 | from typing import Any, Union 19 | 20 | import tensorflow as tf 21 | 22 | 23 | class ClientWeighting(enum.Enum): 24 | """Enum for built-in methods for weighing clients.""" 25 | 26 | UNIFORM = 1 27 | NUM_EXAMPLES = 2 28 | 29 | 30 | ClientWeightFnType = Callable[[Any], tf.Tensor] 31 | ClientWeightType = Union[ClientWeighting, ClientWeightFnType] 32 | 33 | 34 | def check_is_client_weighting_or_callable(client_weighting): 35 | if not isinstance(client_weighting, ClientWeighting) and not callable( 36 | client_weighting 37 | ): 38 | raise TypeError( 39 | '`client_weighting` must be either an instance of ' 40 | '`ClientWeighting` or it must be callable. ' 41 | f'Found type {type(client_weighting)}.' 42 | ) 43 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/protocol/python/BUILD: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC 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 | load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") 16 | 17 | package( 18 | default_applicable_licenses = ["//:package_license"], 19 | default_visibility = ["//visibility:public"], 20 | ) 21 | 22 | pybind_extension( 23 | name = "aggregation_protocol", 24 | srcs = ["aggregation_protocol.cc"], 25 | deps = [ 26 | "//tensorflow_federated/cc/core/impl/aggregation/protocol:aggregation_protocol", 27 | "//tensorflow_federated/cc/core/impl/aggregation/protocol:aggregation_protocol_messages_cc_proto", 28 | "//tensorflow_federated/cc/core/impl/aggregation/protocol:configuration_cc_proto", 29 | "@com_google_absl//absl/status", 30 | "@pybind11_abseil//pybind11_abseil:absl_casters", 31 | "@pybind11_abseil//pybind11_abseil:status_casters", 32 | "@pybind11_protobuf//pybind11_protobuf:native_proto_caster", 33 | ], 34 | ) 35 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/status_conversion.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 "tensorflow_federated/cc/core/impl/executors/status_conversion.h" 17 | 18 | #include 19 | 20 | #include "absl/status/status.h" 21 | #include "include/grpcpp/support/status.h" 22 | 23 | namespace tensorflow_federated { 24 | 25 | absl::Status grpc_to_absl(grpc::Status status) { 26 | if (status.ok()) { 27 | return absl::OkStatus(); 28 | } 29 | return absl::Status(static_cast(status.error_code()), 30 | status.error_message()); 31 | } 32 | 33 | grpc::Status absl_to_grpc(absl::Status status) { 34 | if (status.ok()) { 35 | return grpc::Status::OK; 36 | } 37 | return grpc::Status(static_cast(status.code()), 38 | std::string(status.message())); 39 | } 40 | 41 | } // namespace tensorflow_federated 42 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/core/fedsql_constants.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2023 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_CORE_FEDSQL_CONSTANTS_H_ 18 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_CORE_FEDSQL_CONSTANTS_H_ 19 | 20 | namespace tensorflow_federated { 21 | namespace aggregation { 22 | 23 | // Constants related to intrinsic definitions that are used in multiple files. 24 | // Ideally these would be marked inline to ensure a single copy of each variable 25 | // but this requires c++17 which is not available when building for bare metal. 26 | 27 | // URI of GroupByAggregator 28 | constexpr char kGroupByUri[] = "fedsql_group_by"; 29 | 30 | // URI prefix of inner intrinsics 31 | constexpr char kFedSqlPrefix[] = "GoogleSQL:"; 32 | 33 | } // namespace aggregation 34 | } // namespace tensorflow_federated 35 | 36 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_CORE_FEDSQL_CONSTANTS_H_ 37 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/sequence_intrinsics.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_SEQUENCE_INTRINSICS_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_SEQUENCE_INTRINSICS_H_ 18 | 19 | #include "absl/status/statusor.h" 20 | #include "absl/strings/string_view.h" 21 | 22 | namespace tensorflow_federated { 23 | const absl::string_view kSequenceReduceUri = "sequence_reduce"; 24 | const absl::string_view kSequenceMapUri = "sequence_map"; 25 | 26 | enum class SequenceIntrinsic { 27 | MAP, 28 | REDUCE, 29 | }; 30 | 31 | absl::StatusOr SequenceIntrinsicFromUri( 32 | const absl::string_view uri); 33 | 34 | absl::string_view SequenceIntrinsicToUri(const SequenceIntrinsic& intrinsic); 35 | 36 | } // namespace tensorflow_federated 37 | 38 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_SEQUENCE_INTRINSICS_H_ 39 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/protocol/federated_compute_checkpoint_builder.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_FEDERATED_COMPUTE_CHECKPOINT_BUILDER_H_ 18 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_FEDERATED_COMPUTE_CHECKPOINT_BUILDER_H_ 19 | 20 | #include 21 | 22 | #include "tensorflow_federated/cc/core/impl/aggregation/protocol/checkpoint_builder.h" 23 | 24 | namespace tensorflow_federated::aggregation { 25 | 26 | // A CheckpointBuilderFactory implementation that builds checkpoint using new 27 | // wire format for federated compute. 28 | class FederatedComputeCheckpointBuilderFactory 29 | : public CheckpointBuilderFactory { 30 | public: 31 | std::unique_ptr Create() const override; 32 | }; 33 | 34 | } // namespace tensorflow_federated::aggregation 35 | 36 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_FEDERATED_COMPUTE_CHECKPOINT_BUILDER_H_ 37 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/federating_executor.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_FEDERATING_EXECUTOR_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_FEDERATING_EXECUTOR_H_ 18 | 19 | #include 20 | 21 | #include "absl/status/statusor.h" 22 | #include "tensorflow_federated/cc/core/impl/executors/cardinalities.h" 23 | #include "tensorflow_federated/cc/core/impl/executors/executor.h" 24 | 25 | namespace tensorflow_federated { 26 | 27 | // Returns an executor that can resolve federated values and intrinsics. 28 | absl::StatusOr> CreateFederatingExecutor( 29 | std::shared_ptr server_child, 30 | std::shared_ptr client_child, 31 | const CardinalityMap& cardinalities); 32 | 33 | } // namespace tensorflow_federated 34 | 35 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_FEDERATING_EXECUTOR_H_ 36 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/templates/errors.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020, The TensorFlow Federated Authors. 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 | """Defines errors used by multiple modules in the `tff.templates` package.""" 15 | 16 | 17 | class TemplateInitFnParamNotEmptyError(TypeError): 18 | """`TypeError` for `initialize_fn` having arguments.""" 19 | 20 | pass 21 | 22 | 23 | class TemplateStateNotAssignableError(TypeError): 24 | """`TypeError` for `state` not being assignable to expected `state`.""" 25 | 26 | pass 27 | 28 | 29 | class TemplateNotMeasuredProcessOutputError(TypeError): 30 | """`TypeError` for output of `next_fn` not being a `MeasuredProcessOutput`.""" 31 | 32 | pass 33 | 34 | 35 | class TemplateNextFnNumArgsError(TypeError): 36 | """`TypeError` for `next_fn` not having expected number of input arguments.""" 37 | 38 | 39 | class TemplateNotFederatedError(TypeError): 40 | """`TypeError` for template functions not being federated.""" 41 | 42 | 43 | class TemplatePlacementError(TypeError): 44 | """`TypeError` for template types not being placed as expected.""" 45 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/tensorflow_executor.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_TENSORFLOW_EXECUTOR_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_TENSORFLOW_EXECUTOR_H_ 18 | 19 | #include 20 | #include 21 | 22 | #include "tensorflow_federated/cc/core/impl/executors/executor.h" 23 | 24 | namespace tensorflow_federated { 25 | 26 | // Returns an executor that can resolve TensorFlow computations and structures 27 | // of tensors. `max_concurrent_computation_calls` can be used to limit the 28 | // maximum number of TensorFlow sessions executing in parallel; non-positive 29 | // values indicate no max. 30 | std::shared_ptr CreateTensorFlowExecutor( 31 | int32_t max_concurrent_computation_calls = -1); 32 | 33 | } // namespace tensorflow_federated 34 | 35 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_TENSORFLOW_EXECUTOR_H_ 36 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/core/intrinsic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2023 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_CORE_INTRINSIC_H_ 18 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_CORE_INTRINSIC_H_ 19 | 20 | #include 21 | #include 22 | 23 | #include "tensorflow_federated/cc/core/impl/aggregation/core/tensor.h" 24 | #include "tensorflow_federated/cc/core/impl/aggregation/core/tensor_spec.h" 25 | 26 | namespace tensorflow_federated { 27 | namespace aggregation { 28 | 29 | // A tuple representing an aggregation intrinsic, meant to be immutable once 30 | // initialized. 31 | struct Intrinsic { 32 | std::string uri; 33 | std::vector inputs; 34 | std::vector outputs; 35 | std::vector parameters; 36 | std::vector nested_intrinsics; 37 | }; 38 | } // namespace aggregation 39 | } // namespace tensorflow_federated 40 | 41 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_CORE_INTRINSIC_H_ 42 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/data_executor.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_DATA_EXECUTOR_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_DATA_EXECUTOR_H_ 18 | 19 | #include 20 | 21 | #include "tensorflow_federated/cc/core/impl/executors/data_backend.h" 22 | #include "tensorflow_federated/cc/core/impl/executors/executor.h" 23 | 24 | namespace tensorflow_federated { 25 | 26 | // Returns an executor that resolves `Data` blocks using `data_backend`. 27 | // 28 | // Note that this executor does not transform values not at top-level, so it is 29 | // advisable to place this beneath a `ReferenceResolvingExecutor`. 30 | std::shared_ptr CreateDataExecutor( 31 | std::shared_ptr child, std::shared_ptr data_backend); 32 | 33 | } // namespace tensorflow_federated 34 | 35 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_DATA_EXECUTOR_H_ 36 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/dataset_utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_DATASET_UTILS_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_DATASET_UTILS_H_ 18 | 19 | #include 20 | 21 | #include "absl/status/statusor.h" 22 | #include "federated_language/proto/computation.pb.h" 23 | #include "tensorflow/core/data/standalone.h" 24 | #include "tensorflow/core/framework/tensor.h" 25 | #include "tensorflow_federated/proto/v0/executor.pb.h" 26 | 27 | namespace tensorflow_federated { 28 | 29 | absl::StatusOr GraphDefTensorFromSequence( 30 | const v0::Value::Sequence& sequence_pb); 31 | 32 | absl::StatusOr> 33 | DatasetFromGraphDefTensor(const tensorflow::Tensor& tensor); 34 | 35 | } // namespace tensorflow_federated 36 | 37 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_DATASET_UTILS_H_ 38 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/tensor_serialization.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_EXECUTOR_SERIALIZATION_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_EXECUTOR_SERIALIZATION_H_ 18 | 19 | #include "absl/status/status.h" 20 | #include "absl/status/statusor.h" 21 | #include "tensorflow/core/framework/tensor.h" 22 | #include "tensorflow_federated/proto/v0/executor.pb.h" 23 | 24 | namespace tensorflow_federated { 25 | 26 | // Serializes a tensor to a TFF Value protobuf. 27 | absl::Status SerializeTensorValue(const tensorflow::Tensor tensor, 28 | v0::Value* value_pb); 29 | 30 | // Deserializes a TFF Value protobuf back to a tf::Tensor. 31 | absl::StatusOr DeserializeTensorValue( 32 | const v0::Value& value_pb); 33 | 34 | } // namespace tensorflow_federated 35 | 36 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_EXECUTOR_SERIALIZATION_H_ 37 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/tensorflow/tensorflow_checkpoint_builder_factory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_TENSORFLOW_TENSORFLOW_CHECKPOINT_BUILDER_FACTORY_H_ 18 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_TENSORFLOW_TENSORFLOW_CHECKPOINT_BUILDER_FACTORY_H_ 19 | 20 | #include 21 | 22 | #include "tensorflow_federated/cc/core/impl/aggregation/protocol/checkpoint_builder.h" 23 | 24 | namespace tensorflow_federated::aggregation::tensorflow { 25 | 26 | // A CheckpointBuilderFactory implementation that writes TensorFlow checkpoints. 27 | class TensorflowCheckpointBuilderFactory 28 | : public tensorflow_federated::aggregation::CheckpointBuilderFactory { 29 | public: 30 | std::unique_ptr Create() 31 | const override; 32 | }; 33 | 34 | } // namespace tensorflow_federated::aggregation::tensorflow 35 | 36 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_TENSORFLOW_TENSORFLOW_CHECKPOINT_BUILDER_FACTORY_H_ 37 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/environments/tensorflow/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024, The TensorFlow Federated Authors. 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 | """Libraries for interacting with a TensorFlow frontend and backend.""" 15 | 16 | # pylint: disable=g-importing-member 17 | from tensorflow_federated.python.core.environments.tensorflow_backend.tensorflow_tree_transformations import replace_intrinsics_with_bodies 18 | from tensorflow_federated.python.core.environments.tensorflow_backend.type_conversions import structure_from_tensor_type_tree 19 | from tensorflow_federated.python.core.environments.tensorflow_backend.type_conversions import type_to_tf_tensor_specs 20 | from tensorflow_federated.python.core.environments.tensorflow_frontend.tensorflow_computation import tf_computation as computation 21 | from tensorflow_federated.python.core.environments.tensorflow_frontend.tensorflow_computation import transform_args 22 | from tensorflow_federated.python.core.environments.tensorflow_frontend.tensorflow_computation import transform_result 23 | from tensorflow_federated.python.core.environments.tensorflow_frontend.tensorflow_types import to_type 24 | # pylint: enable=g-importing-member 25 | -------------------------------------------------------------------------------- /examples/datasets/load_flair.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023, The TensorFlow Federated Authors. 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 | """An example script that loads and processes the FLAIR dataset.""" 15 | 16 | import collections 17 | from collections.abc import Sequence 18 | import tempfile 19 | 20 | from absl import app 21 | from absl import logging 22 | import tensorflow_federated as tff 23 | 24 | 25 | def main(argv: Sequence[str]) -> None: 26 | if len(argv) > 1: 27 | raise app.UsageError('Too many command-line arguments.') 28 | 29 | data_dir = tempfile.mkdtemp() 30 | tff.simulation.datasets.flair.download_data(data_dir) 31 | cache_dir = tempfile.mkdtemp() 32 | tff.simulation.datasets.flair.write_data(data_dir, cache_dir) 33 | flair_train, flair_val, flair_test = tff.simulation.datasets.flair.load_data( 34 | cache_dir 35 | ) 36 | flair_data = collections.OrderedDict( 37 | train=flair_train, 38 | val=flair_val, 39 | test=flair_test, 40 | ) 41 | for split, client_data in flair_data.items(): 42 | num_clients = len(client_data.client_ids) 43 | logging.info('Num clients, %s split: %s', split, num_clients) 44 | 45 | 46 | if __name__ == '__main__': 47 | app.run(main) 48 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/executor_test_utils_bindings.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2023, The TensorFlow Federated Authors. 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 "include/pybind11/pybind11.h" 17 | #include "tensorflow_federated/cc/core/impl/executors/mock_executor.h" 18 | 19 | namespace tensorflow_federated { 20 | 21 | namespace { 22 | 23 | //////////////////////////////////////////////////////////////////////////////// 24 | // The Python module definition `executor_test_utils_bindings`. 25 | // 26 | // This will be used with `import executor_test_utils_bindings` in Python. This 27 | // module should _not_ be directly imported into the public pip API. The methods 28 | // here will raise `NotOkStatus` errors from absl, which are not user friendly. 29 | //////////////////////////////////////////////////////////////////////////////// 30 | PYBIND11_MODULE(executor_test_utils_bindings, m) { 31 | m.doc() = "Bindings for the C++ "; 32 | 33 | // Executor construction methods. 34 | m.def("create_mock_executor", &CreateMockExecutor, "Creates a MockExecutor."); 35 | } 36 | 37 | } // namespace 38 | } // namespace tensorflow_federated 39 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/base/monitoring.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "tensorflow_federated/cc/core/impl/aggregation/base/monitoring.h" 18 | 19 | #include 20 | #include 21 | 22 | #include "tensorflow_federated/cc/core/impl/aggregation/base/base_name.h" 23 | 24 | namespace tensorflow_federated { 25 | 26 | namespace internal { 27 | 28 | StatusBuilder::StatusBuilder(StatusCode code, const char* file, int line) 29 | : file_(file), line_(line), code_(code), message_() {} 30 | 31 | StatusBuilder::StatusBuilder(StatusBuilder const& other) 32 | : file_(other.file_), 33 | line_(other.line_), 34 | code_(other.code_), 35 | message_(other.message_.str()) {} 36 | 37 | StatusBuilder::operator Status() { 38 | auto message_str = message_.str(); 39 | if (code_ != OK) { 40 | std::ostringstream status_message; 41 | status_message << "(at " << BaseName(file_) << ":" << line_ << message_str; 42 | message_str = status_message.str(); 43 | } 44 | return Status(code_, message_str); 45 | } 46 | 47 | } // namespace internal 48 | 49 | } // namespace tensorflow_federated 50 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/protocol/federated_compute_checkpoint_parser.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_FEDERATED_COMPUTE_CHECKPOINT_PARSER_H_ 18 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_FEDERATED_COMPUTE_CHECKPOINT_PARSER_H_ 19 | 20 | #include 21 | 22 | #include "absl/status/statusor.h" 23 | #include "absl/strings/cord.h" 24 | #include "tensorflow_federated/cc/core/impl/aggregation/protocol/checkpoint_parser.h" 25 | 26 | namespace tensorflow_federated::aggregation { 27 | 28 | // A CheckpointParserFactory implementation that creates federated compute wire 29 | // format checkpoint parser. 30 | class FederatedComputeCheckpointParserFactory : public CheckpointParserFactory { 31 | public: 32 | absl::StatusOr> Create( 33 | const absl::Cord& serialized_checkpoint) const override; 34 | }; 35 | 36 | } // namespace tensorflow_federated::aggregation 37 | 38 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_FEDERATED_COMPUTE_CHECKPOINT_PARSER_H_ 39 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/remote_executor.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_REMOTE_EXECUTOR_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_REMOTE_EXECUTOR_H_ 18 | 19 | #include 20 | 21 | #include "include/grpcpp/grpcpp.h" 22 | #include "tensorflow_federated/cc/core/impl/executors/cardinalities.h" 23 | #include "tensorflow_federated/cc/core/impl/executors/executor.h" 24 | #include "tensorflow_federated/proto/v0/executor.grpc.pb.h" 25 | 26 | namespace tensorflow_federated { 27 | 28 | // Returns an executor which communicates with a remote executor service. 29 | std::shared_ptr CreateRemoteExecutor( 30 | std::shared_ptr channel, 31 | const CardinalityMap& cardinalities); 32 | std::shared_ptr CreateRemoteExecutor( 33 | std::unique_ptr stub, 34 | const CardinalityMap& cardinalities); 35 | } // namespace tensorflow_federated 36 | 37 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_REMOTE_EXECUTOR_H_ 38 | -------------------------------------------------------------------------------- /examples/learning/federated_program/vizier/vizier_service.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023, The TensorFlow Federated Authors. 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 | """Utilities for interacting with the Vizier service.""" 15 | 16 | import getpass 17 | 18 | import tensorflow as tf 19 | 20 | from google.protobuf import text_format # pylint: disable=g-bad-import-order 21 | 22 | from vizier.service import clients 23 | from vizier.service import pyvizier 24 | 25 | # The proto message type does not appear to be in the public API any longer, 26 | # but it is necessary to be able to parse text formatted proto strings. 27 | _StudySpec = type(pyvizier.StudyConfig().to_proto()) 28 | 29 | 30 | def create_study_config(config_path: str) -> pyvizier.StudyConfig: 31 | with tf.io.gfile.GFile(config_path) as f: 32 | proto = text_format.Parse(f.read(), _StudySpec()) 33 | return pyvizier.StudyConfig.from_proto(proto) 34 | 35 | 36 | def create_study( 37 | study_config: pyvizier.StudyConfig, 38 | name: str, 39 | owner: str = getpass.getuser(), 40 | ) -> clients.Study: 41 | """Creates a Vizier Study for the problem.""" 42 | return clients.Study.from_study_config( 43 | config=study_config, owner=owner, study_id=name 44 | ) 45 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/protocol/resource_resolver.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2023 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_RESOURCE_RESOLVER_H_ 18 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_RESOURCE_RESOLVER_H_ 19 | 20 | #include 21 | #include 22 | 23 | #include "absl/status/statusor.h" 24 | #include "absl/strings/cord.h" 25 | 26 | namespace tensorflow_federated::aggregation { 27 | 28 | // Describes an abstract interface for resolving a resource from a given client 29 | // and uri. 30 | class ResourceResolver { 31 | public: 32 | virtual ~ResourceResolver() = default; 33 | 34 | // Retrieves a resource for the given `client_id` and `uri` combination. 35 | // The resource can be accessed exactly once and must be deleted (best-effort) 36 | // after it is returned. 37 | virtual absl::StatusOr RetrieveResource( 38 | int64_t client_id, const std::string& uri) = 0; 39 | }; 40 | } // namespace tensorflow_federated::aggregation 41 | 42 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_RESOURCE_RESOLVER_H_ 43 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/sequence_intrinsics.cc: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 "tensorflow_federated/cc/core/impl/executors/sequence_intrinsics.h" 17 | 18 | #include "absl/status/status.h" 19 | #include "absl/status/statusor.h" 20 | #include "absl/strings/str_cat.h" 21 | #include "absl/strings/string_view.h" 22 | 23 | namespace tensorflow_federated { 24 | 25 | absl::StatusOr SequenceIntrinsicFromUri( 26 | const absl::string_view uri) { 27 | if (uri == kSequenceMapUri) { 28 | return SequenceIntrinsic::MAP; 29 | } else if (uri == kSequenceReduceUri) { 30 | return SequenceIntrinsic::REDUCE; 31 | } else { 32 | return absl::UnimplementedError( 33 | absl::StrCat("Unsupported sequence intrinsic URI: ", uri)); 34 | } 35 | } 36 | absl::string_view SequenceIntrinsicToUri(const SequenceIntrinsic& intrinsic) { 37 | switch (intrinsic) { 38 | case SequenceIntrinsic::MAP: 39 | return kSequenceMapUri; 40 | case SequenceIntrinsic::REDUCE: 41 | return kSequenceReduceUri; 42 | } 43 | } 44 | } // namespace tensorflow_federated 45 | -------------------------------------------------------------------------------- /tensorflow_federated/python/program/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018, The TensorFlow Federated Authors. 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 | """Libraries for creating federated programs.""" 15 | 16 | # pylint: disable=g-importing-member 17 | from tensorflow_federated.python.program.client_id_data_source import ClientIdDataSource 18 | from tensorflow_federated.python.program.client_id_data_source import ClientIdDataSourceIterator 19 | from tensorflow_federated.python.program.dataset_data_source import DatasetDataSource 20 | from tensorflow_federated.python.program.dataset_data_source import DatasetDataSourceIterator 21 | from tensorflow_federated.python.program.file_program_state_manager import FileProgramStateManager 22 | from tensorflow_federated.python.program.file_release_manager import CSVFileReleaseManager 23 | from tensorflow_federated.python.program.file_release_manager import CSVKeyFieldnameNotFoundError 24 | from tensorflow_federated.python.program.file_release_manager import CSVSaveMode 25 | from tensorflow_federated.python.program.file_release_manager import SavedModelFileReleaseManager 26 | from tensorflow_federated.python.program.tensorboard_release_manager import TensorBoardReleaseManager 27 | # pylint: enable=g-importing-member 28 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/array_shape_test_utils.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2024, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_ARRAY_SHAPE_TEST_UTILS_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_ARRAY_SHAPE_TEST_UTILS_H_ 18 | 19 | #include 20 | #include 21 | 22 | #include "federated_language/proto/array.pb.h" 23 | 24 | namespace tensorflow_federated { 25 | namespace testing { 26 | 27 | inline federated_language::ArrayShape CreateArrayShape( 28 | std::initializer_list dims, bool unknown_rank) { 29 | federated_language::ArrayShape shape_pb; 30 | shape_pb.mutable_dim()->Assign(dims.begin(), dims.end()); 31 | shape_pb.set_unknown_rank(unknown_rank); 32 | return shape_pb; 33 | } 34 | 35 | inline federated_language::ArrayShape CreateArrayShape( 36 | std::initializer_list dims) { 37 | return CreateArrayShape(dims, false); 38 | } 39 | 40 | } // namespace testing 41 | } // namespace tensorflow_federated 42 | 43 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_ARRAY_SHAPE_TEST_UTILS_H_ 44 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/backends/native/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020, The TensorFlow Federated Authors. 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 | """Libraries for interacting with native backends.""" 15 | 16 | from tensorflow_federated.python.core.backends.native.compiler import desugar_and_transform_to_native 17 | from tensorflow_federated.python.core.backends.native.compiler import transform_to_native_form 18 | from tensorflow_federated.python.core.backends.native.execution_contexts import create_async_local_cpp_execution_context 19 | from tensorflow_federated.python.core.backends.native.execution_contexts import create_mergeable_comp_execution_context 20 | from tensorflow_federated.python.core.backends.native.execution_contexts import create_sync_local_cpp_execution_context 21 | from tensorflow_federated.python.core.backends.native.execution_contexts import set_async_local_cpp_execution_context 22 | from tensorflow_federated.python.core.backends.native.execution_contexts import set_mergeable_comp_execution_context 23 | from tensorflow_federated.python.core.backends.native.execution_contexts import set_sync_local_cpp_execution_context 24 | from tensorflow_federated.python.core.backends.native.mergeable_comp_compiler import compile_to_mergeable_comp_form 25 | -------------------------------------------------------------------------------- /tensorflow_federated/python/learning/optimizers/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Libraries for optimization algorithms.""" 15 | 16 | from tensorflow_federated.python.learning.optimizers.adafactor import build_adafactor 17 | from tensorflow_federated.python.learning.optimizers.adagrad import build_adagrad 18 | from tensorflow_federated.python.learning.optimizers.adam import build_adam 19 | from tensorflow_federated.python.learning.optimizers.adamw import build_adamw 20 | from tensorflow_federated.python.learning.optimizers.optimizer import check_weights_gradients_match 21 | from tensorflow_federated.python.learning.optimizers.optimizer import handle_indexed_slices_gradients 22 | from tensorflow_federated.python.learning.optimizers.optimizer import LEARNING_RATE_KEY 23 | from tensorflow_federated.python.learning.optimizers.optimizer import Optimizer 24 | from tensorflow_federated.python.learning.optimizers.rmsprop import build_rmsprop 25 | from tensorflow_federated.python.learning.optimizers.scheduling import schedule_learning_rate 26 | from tensorflow_federated.python.learning.optimizers.sgdm import build_sgdm 27 | from tensorflow_federated.python.learning.optimizers.yogi import build_yogi 28 | -------------------------------------------------------------------------------- /examples/custom_data_backend/data_backend_example.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2022, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_EXAMPLES_CUSTOM_DATA_BACKEND_DATA_BACKEND_EXAMPLE_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_EXAMPLES_CUSTOM_DATA_BACKEND_DATA_BACKEND_EXAMPLE_H_ 18 | 19 | #include "absl/status/status.h" 20 | #include "federated_language/proto/computation.pb.h" 21 | #include "tensorflow_federated/cc/core/impl/executors/data_backend.h" 22 | #include "tensorflow_federated/proto/v0/executor.pb.h" 23 | 24 | namespace tensorflow_federated_examples { 25 | 26 | // An example implementation of `DataBackend` used to show Python interop. 27 | class DataBackendExample : public tensorflow_federated::DataBackend { 28 | public: 29 | absl::Status ResolveToValue(const federated_language::Data& data_reference, 30 | const federated_language::Type& data_type, 31 | tensorflow_federated::v0::Value& value_out) final; 32 | }; 33 | 34 | } // namespace tensorflow_federated_examples 35 | 36 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_EXAMPLES_CUSTOM_DATA_BACKEND_DATA_BACKEND_EXAMPLE_H_ 37 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/core/dp_histogram_test_utils.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "tensorflow_federated/cc/core/impl/aggregation/core/dp_histogram_test_utils.h" 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "tensorflow_federated/cc/core/impl/aggregation/core/agg_vector.h" 25 | #include "tensorflow_federated/cc/core/impl/aggregation/core/tensor.pb.h" 26 | #include "tensorflow_federated/cc/core/impl/aggregation/core/tensor_spec.h" 27 | 28 | namespace tensorflow_federated { 29 | namespace aggregation { 30 | namespace dp_histogram_testing { 31 | 32 | TensorSpec CreateTensorSpec(std::string name, DataType dtype) { 33 | return TensorSpec(std::move(name), dtype, {-1}); 34 | } 35 | 36 | std::vector CreateTopLevelParameters(double epsilon, double delta, 37 | int64_t l0_bound) { 38 | return CreateTopLevelParameters(epsilon, delta, 39 | l0_bound); 40 | } 41 | 42 | } // namespace dp_histogram_testing 43 | } // namespace aggregation 44 | } // namespace tensorflow_federated 45 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/tensorflow/tensorflow_checkpoint_parser_factory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_TENSORFLOW_TENSORFLOW_CHECKPOINT_PARSER_FACTORY_H_ 18 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_TENSORFLOW_TENSORFLOW_CHECKPOINT_PARSER_FACTORY_H_ 19 | 20 | #include 21 | 22 | #include "absl/status/statusor.h" 23 | #include "absl/strings/cord.h" 24 | #include "tensorflow_federated/cc/core/impl/aggregation/protocol/checkpoint_parser.h" 25 | 26 | namespace tensorflow_federated::aggregation::tensorflow { 27 | 28 | // A CheckpointParserFactory implementation that reads TensorFlow checkpoints. 29 | class TensorflowCheckpointParserFactory 30 | : public tensorflow_federated::aggregation::CheckpointParserFactory { 31 | public: 32 | absl::StatusOr< 33 | std::unique_ptr> 34 | Create(const absl::Cord& serialized_checkpoint) const override; 35 | }; 36 | 37 | } // namespace tensorflow_federated::aggregation::tensorflow 38 | 39 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_TENSORFLOW_TENSORFLOW_CHECKPOINT_PARSER_FACTORY_H_ 40 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/core/tensor_spec.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "tensorflow_federated/cc/core/impl/aggregation/core/tensor_spec.h" 18 | 19 | #include 20 | 21 | #include "tensorflow_federated/cc/core/impl/aggregation/base/monitoring.h" 22 | #include "tensorflow_federated/cc/core/impl/aggregation/core/tensor.pb.h" 23 | #include "tensorflow_federated/cc/core/impl/aggregation/core/tensor_shape.h" 24 | 25 | namespace tensorflow_federated { 26 | namespace aggregation { 27 | 28 | StatusOr TensorSpec::FromProto( 29 | const TensorSpecProto& tensor_spec_proto) { 30 | TFF_ASSIGN_OR_RETURN(TensorShape shape, 31 | TensorShape::FromProto(tensor_spec_proto.shape())); 32 | return TensorSpec(tensor_spec_proto.name(), tensor_spec_proto.dtype(), 33 | std::move(shape)); 34 | } 35 | 36 | TensorSpecProto TensorSpec::ToProto() const { 37 | TensorSpecProto tensor_spec_proto; 38 | tensor_spec_proto.set_name(name_); 39 | tensor_spec_proto.set_dtype(dtype_); 40 | *tensor_spec_proto.mutable_shape() = shape_.ToProto(); 41 | return tensor_spec_proto; 42 | } 43 | 44 | } // namespace aggregation 45 | } // namespace tensorflow_federated 46 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/testing/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:cc_library.bzl", "cc_library") 2 | 3 | package( 4 | default_applicable_licenses = ["//:package_license"], 5 | default_visibility = [ 6 | ":testing_packages", 7 | "//tensorflow_federated/cc/core/impl:impl_packages", 8 | "//tensorflow_federated/cc/simulation:simulation_packages", 9 | ], 10 | ) 11 | 12 | package_group( 13 | name = "testing_packages", 14 | packages = [ 15 | "//tensorflow_federated/cc/testing/...", 16 | ], 17 | ) 18 | 19 | licenses(["notice"]) 20 | 21 | cc_library( 22 | name = "oss_test_main", 23 | testonly = 1, 24 | srcs = ["oss_test_main.cc"], 25 | visibility = ["//visibility:public"], 26 | deps = [ 27 | "@com_google_absl//absl/flags:parse", 28 | "@com_google_googletest//:gtest", 29 | ], 30 | ) 31 | 32 | cc_library( 33 | name = "protobuf_matchers", 34 | testonly = True, 35 | hdrs = ["protobuf_matchers.h"], 36 | visibility = ["//visibility:public"], 37 | deps = [ 38 | "@com_google_absl//absl/memory", 39 | "@com_google_absl//absl/strings", 40 | "@com_google_absl//absl/strings:str_format", 41 | "@com_google_absl//absl/strings:string_view", 42 | "@com_google_googletest//:gtest", 43 | ], 44 | ) 45 | 46 | cc_library( 47 | name = "status_matchers", 48 | testonly = True, 49 | srcs = ["status_matchers.cc"], 50 | hdrs = ["status_matchers.h"], 51 | visibility = ["//visibility:public"], 52 | deps = [ 53 | "@com_github_grpc_grpc//:grpc++", 54 | "@com_google_absl//absl/status", 55 | "@com_google_absl//absl/status:statusor", 56 | "@com_google_absl//absl/strings", 57 | "@com_google_absl//absl/strings:string_view", 58 | "@com_google_googletest//:gtest", 59 | ], 60 | ) 61 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/core/tensor_data.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "tensorflow_federated/cc/core/impl/aggregation/core/tensor_data.h" 18 | 19 | #include 20 | 21 | #include "tensorflow_federated/cc/core/impl/aggregation/base/monitoring.h" 22 | 23 | namespace tensorflow_federated { 24 | namespace aggregation { 25 | 26 | bool TensorData::IsAligned(const void* data, size_t alignment_size) { 27 | return (reinterpret_cast(data) % alignment_size) == 0; 28 | } 29 | 30 | Status TensorData::CheckValid(size_t value_size, size_t alignment_size) const { 31 | TFF_CHECK(value_size > 0); 32 | 33 | if (byte_size() > 0) { 34 | if ((byte_size() % value_size) != 0) { 35 | return TFF_STATUS(FAILED_PRECONDITION) 36 | << "TensorData: byte_size() must be a multiple of value_size " 37 | << value_size; 38 | } 39 | 40 | if (!IsAligned(data(), alignment_size)) { 41 | return TFF_STATUS(FAILED_PRECONDITION) 42 | << "TensorData: data() address is not aligned by alignment_size " 43 | << alignment_size; 44 | } 45 | } 46 | 47 | return TFF_STATUS(OK); 48 | } 49 | 50 | } // namespace aggregation 51 | } // namespace tensorflow_federated 52 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/value_validation.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_VALUE_VALIDATION_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_VALUE_VALIDATION_H_ 18 | 19 | #include 20 | 21 | #include "absl/status/statusor.h" 22 | #include "tensorflow_federated/cc/core/impl/executors/federated_intrinsics.h" 23 | #include "tensorflow_federated/proto/v0/executor.pb.h" 24 | 25 | namespace tensorflow_federated { 26 | 27 | // The kind of an incoming validated federated value. 28 | enum class FederatedKind { 29 | SERVER, 30 | CLIENTS, 31 | CLIENTS_ALL_EQUAL, 32 | }; 33 | 34 | // Ensures that preconditions on federated values necessary for memory safety 35 | // in value accesses are upheld. 36 | // 37 | // In particular, checks that `all_equal` corresponds to 1, and that 38 | // server-placed values are `all_equal`. 39 | absl::StatusOr ValidateFederated( 40 | uint32_t num_clients, const v0::Value_Federated& federated); 41 | 42 | } // namespace tensorflow_federated 43 | 44 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_VALUE_VALIDATION_H_ 45 | -------------------------------------------------------------------------------- /tensorflow_federated/python/analytics/histogram_processing.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """A set of utilities functions for histogram processing.""" 15 | 16 | import tensorflow as tf 17 | 18 | 19 | def threshold_histogram(histogram_keys, histogram_values, threshold): 20 | """Thresholds a histogram by values. 21 | 22 | Args: 23 | histogram_keys: A rank-1 Tensor containing a list of strings representing 24 | the keys in the input histogram. 25 | histogram_values: A rank-1 Tensor containing a list of floats representing 26 | the values for each key in `histogram_keys`. 27 | threshold: A float tensor to threshold the input histogram. The (key, value) 28 | pairs where value is less than `threshold` are filtered out in the 29 | returned histogram. 30 | 31 | Returns: 32 | A tuple (histogram_keys_thresholded, histogram_values_thresholded) of the 33 | histogram after being thresholded. 34 | """ 35 | histogram_mask = tf.math.greater_equal(histogram_values, threshold) 36 | histogram_keys_thresholded = tf.boolean_mask(histogram_keys, histogram_mask) 37 | histogram_values_thresholded = tf.boolean_mask( 38 | histogram_values, histogram_mask 39 | ) 40 | 41 | return histogram_keys_thresholded, histogram_values_thresholded 42 | -------------------------------------------------------------------------------- /tools/python_package/BUILD: -------------------------------------------------------------------------------- 1 | package( 2 | default_applicable_licenses = ["//:package_license"], 3 | default_visibility = ["//visibility:private"], 4 | ) 5 | 6 | package_group( 7 | name = "python_package_tool", 8 | packages = ["//tools/python_package/..."], 9 | ) 10 | 11 | licenses(["notice"]) 12 | 13 | # The data labels are needed otherwise Bazel will not know about the 14 | # `__init__.py` files; this is because the modules are imported directory, 15 | # rather than the packages, so in the Bazel dependency hierarchy, nothing is 16 | # dependent on the `__init__.py` files. 17 | sh_binary( 18 | name = "build_python_package", 19 | srcs = ["build_python_package.sh"], 20 | data = [ 21 | "//:LICENSE", 22 | "//:README.md", 23 | "//:pyproject_toml", 24 | "//tensorflow_federated", 25 | "//tensorflow_federated/data:worker_binary", 26 | "//tensorflow_federated/proto", 27 | "//tensorflow_federated/proto/v0", 28 | "//tensorflow_federated/python", 29 | "//tensorflow_federated/python/analytics/hierarchical_histogram", 30 | "//tensorflow_federated/python/common_libs", 31 | "//tensorflow_federated/python/core", 32 | "//tensorflow_federated/python/core/environments", 33 | "//tensorflow_federated/python/core/environments/tensorflow_backend", 34 | "//tensorflow_federated/python/core/environments/tensorflow_frontend", 35 | "//tensorflow_federated/python/core/impl", 36 | "//tensorflow_federated/python/core/impl/compiler", 37 | "//tensorflow_federated/python/core/impl/execution_contexts", 38 | "//tensorflow_federated/python/core/impl/executor_stacks", 39 | "//tensorflow_federated/python/core/impl/executors", 40 | ], 41 | ) 42 | 43 | sh_binary( 44 | name = "test_python_package", 45 | srcs = ["test_python_package.sh"], 46 | ) 47 | -------------------------------------------------------------------------------- /docs/tutorials/README.md: -------------------------------------------------------------------------------- 1 | # TensorFlow Federated Tutorials 2 | 3 | ## Running the TFF Tutorials in Jupyter Notebooks 4 | 5 | By design [asyncio](https://docs.python.org/3/library/asyncio.html) does not 6 | allow its event loop to be nested. This presents a practical problem: When in an 7 | environment where the event loop is already running it’s impossible to run tasks 8 | and wait for the result. Trying to do so will give the error `“RuntimeError: 9 | This event loop is already running”`. 10 | 11 | This issue is present when running the TFF tutorials in Google Colab or Jupyter 12 | notebooks. 13 | 14 | The fix is to: 15 | 16 | 1. Install [nest-asyncio](https://pypi.org/project/nest-asyncio/) by either: 17 | 18 | * `pip` install the package from within the notebook. 19 | 20 | ```python 21 | !pip install --quiet --upgrade --user nest-asyncio 22 | ``` 23 | 24 | * `pip` install the package in the same virtual environment that the 25 | IPython kernel is installed. 26 | 27 | ```python 28 | # Create a virtual environment 29 | python3 -m venv "venv" 30 | source "venv/bin/activate" 31 | pip install --upgrade "pip" 32 | 33 | # Install the required Python package 34 | pip install --upgrade \ 35 | tensorflow-federated \ 36 | nest-asyncio 37 | 38 | # Install Jupyter 39 | pip install --upgrade jupyter 40 | 41 | # Build IPython kernel 42 | python -m ipykernel install \ 43 | --user \ 44 | --name tff_kernel \ 45 | --display-name "TFF Kernel" 46 | ``` 47 | 48 | 1. Use `nest-asyncio` to patch `asyncio`. 49 | 50 | ```python 51 | import nest_asyncio 52 | nest_asyncio.apply() 53 | ``` 54 | 55 | See the [nest-asyncio](https://pypi.org/project/nest-asyncio/) Python package 56 | for more information. 57 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/core/agg_core.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package tensorflow_federated.aggregation; 4 | 5 | import "tensorflow_federated/cc/core/impl/aggregation/core/tensor.proto"; 6 | 7 | // Internal state representation of an AggVectorAggregator. 8 | message AggVectorAggregatorState { 9 | uint64 num_inputs = 1; 10 | bytes vector_data = 2; 11 | } 12 | 13 | // Internal state representation of a FederatedMeanAggregator. 14 | message FederatedMeanAggregatorState { 15 | uint64 num_inputs = 1; 16 | bytes weights_sum = 2; 17 | bytes weighted_values_sum = 3; 18 | } 19 | 20 | // Internal state representation of a OneDimGroupingAggregator. 21 | message OneDimGroupingAggregatorState { 22 | uint64 num_inputs = 1; 23 | bytes vector_data = 2; 24 | } 25 | 26 | // Internal state representation of a GroupByAggregator. 27 | message GroupByAggregatorState { 28 | uint64 num_inputs = 1; 29 | // TODO: b/335276781 - Consider storing key combiner state more efficiently 30 | // such that string keys that appear in multiple combinations are only stored 31 | // once. 32 | repeated TensorProto keys = 2; 33 | repeated OneDimGroupingAggregatorState nested_aggregators = 3; 34 | // The number of contributors to each combination of keys. It should be of the 35 | // same length as each of the tensors in keys. 36 | repeated uint64 counter_of_contributors = 4; 37 | // We can add a random number of zeroes to mask the influence of any single 38 | // client on this state. 39 | repeated bytes padding = 5; 40 | } 41 | 42 | // Internal state representation of a DPQuantileAggregator. 43 | message DPQuantileAggregatorState { 44 | uint64 num_inputs = 1; 45 | uint64 reservoir_sampling_count = 2; 46 | bytes buffer = 3; 47 | } 48 | 49 | message DPTensorAggregatorBundleState { 50 | uint64 num_inputs = 1; 51 | repeated bytes nested_serialized_states = 2; 52 | } 53 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/protocol/simple_aggregation/cancelable_callback.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_SIMPLE_AGGREGATION_CANCELABLE_CALLBACK_H_ 18 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_SIMPLE_AGGREGATION_CANCELABLE_CALLBACK_H_ 19 | 20 | #include 21 | #include 22 | 23 | #include "absl/time/time.h" 24 | #include "tensorflow_federated/cc/core/impl/aggregation/base/clock.h" 25 | 26 | namespace tensorflow_federated::aggregation { 27 | 28 | // Abstract interface for an object that can be canceled. 29 | class Cancelable { 30 | public: 31 | virtual ~Cancelable() = default; 32 | virtual void Cancel() = 0; 33 | }; 34 | 35 | using CancelationToken = std::shared_ptr; 36 | 37 | // Schedules a delayed callback that can be canceled by calling Cancel method on 38 | // the CancelationToken. 39 | CancelationToken ScheduleCallback(Clock* clock, absl::Duration delay, 40 | std::function callback); 41 | 42 | } // namespace tensorflow_federated::aggregation 43 | 44 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_SIMPLE_AGGREGATION_CANCELABLE_CALLBACK_H_ 45 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/protocol/config_converter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2023 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_CONFIG_CONVERTER_H_ 18 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_CONFIG_CONVERTER_H_ 19 | 20 | #include 21 | 22 | #include "absl/status/status.h" 23 | #include "tensorflow_federated/cc/core/impl/aggregation/base/monitoring.h" 24 | #include "tensorflow_federated/cc/core/impl/aggregation/core/intrinsic.h" 25 | #include "tensorflow_federated/cc/core/impl/aggregation/protocol/configuration.pb.h" 26 | 27 | namespace tensorflow_federated { 28 | namespace aggregation { 29 | 30 | // Validates the Configuration. 31 | // Returns INVALID_ARGUMENT if the configuration is invalid. 32 | absl::Status ValidateConfiguration(const Configuration& configuration); 33 | 34 | // Parses a Configuration proto into a vector of Intrinsic structs to 35 | // represent the aggregation intrinsic independently from the proto. 36 | StatusOr> ParseFromConfig(const Configuration& config); 37 | 38 | } // namespace aggregation 39 | } // namespace tensorflow_federated 40 | 41 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_AGGREGATION_PROTOCOL_CONFIG_CONVERTER_H_ 42 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/base/simulated_clock.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "tensorflow_federated/cc/core/impl/aggregation/base/simulated_clock.h" 18 | 19 | #include "absl/synchronization/mutex.h" 20 | #include "absl/time/time.h" 21 | 22 | namespace tensorflow_federated { 23 | 24 | absl::Time SimulatedClock::Now() { 25 | absl::MutexLock lock(mutex()); 26 | return NowLocked(); 27 | } 28 | 29 | absl::Time SimulatedClock::NowLocked() { 30 | mutex()->AssertHeld(); 31 | return now_; 32 | } 33 | 34 | void SimulatedClock::Sleep(absl::Duration d) { 35 | absl::Time current; 36 | { 37 | absl::MutexLock lock(mutex()); 38 | current = now_; 39 | } 40 | absl::Time deadline = current + d; 41 | while (true) { 42 | { 43 | absl::MutexLock lock(mutex()); 44 | current = now_; 45 | } 46 | if (current >= deadline) { 47 | return; 48 | } 49 | } 50 | } 51 | 52 | void SimulatedClock::SetTime(absl::Time t) { 53 | { 54 | absl::MutexLock lock(mutex()); 55 | now_ = t; 56 | } 57 | DispatchWakeups(); 58 | } 59 | 60 | void SimulatedClock::AdvanceTime(absl::Duration d) { 61 | { 62 | absl::MutexLock lock(mutex()); 63 | now_ += d; 64 | } 65 | DispatchWakeups(); 66 | } 67 | 68 | } // namespace tensorflow_federated 69 | -------------------------------------------------------------------------------- /third_party/federated_language/README.md: -------------------------------------------------------------------------------- 1 | # Federated Language Patches 2 | 3 | 1. Clone the repository. 4 | 5 | ```shell 6 | $ git clone https://github.com/google-parfait/federated-language.git "/tmp/federated-language" 7 | $ cd "/tmp/federated-language" 8 | ``` 9 | 10 | 1. Checkout the commit. 11 | 12 | ```shell 13 | $ git checkout 14 | ``` 15 | 16 | 1. Create the `python_deps` patch. 17 | 18 | Make the changes. 19 | 20 | ```shell 21 | $ buildozer 'remove deps @federated_language_pypi//absl_py' //...:* 22 | $ buildozer 'remove deps @federated_language_pypi//attrs' //...:* 23 | $ buildozer 'remove deps @federated_language_pypi//dm_tree' //...:* 24 | $ buildozer 'remove deps @federated_language_pypi//ml_dtypes' //...:* 25 | $ buildozer 'remove deps @federated_language_pypi//numpy' //...:* 26 | $ buildozer 'remove deps @federated_language_pypi//typing_extensions' //...:* 27 | ``` 28 | 29 | Confirm no more changes are required. 30 | 31 | ```shell 32 | $ find "." -type f -print0 | xargs -0 grep "@federated_language_pypi" 33 | ``` 34 | 35 | Create the patch. 36 | 37 | ```shell 38 | $ git diff --no-prefix \ 39 | > "/tensorflow_federated/third_party/federated_language/python_deps.patch" 40 | ``` 41 | 42 | 1. Create the `structure_visibility` patch. 43 | 44 | Note: This patch requires the previous patch to be applied. 45 | 46 | Stage the changes from the previous step. 47 | 48 | ```shell 49 | $ git add . 50 | ``` 51 | 52 | Make the changes. 53 | 54 | ```shell 55 | $ buildozer 'add visibility //visibility:public' //federated_language/common_libs:structure 56 | ``` 57 | 58 | Create the patch. 59 | 60 | ```shell 61 | $ git diff --no-prefix \ 62 | > "/tensorflow_federated/third_party/federated_language/structure_visibility.patch" 63 | ``` 64 | -------------------------------------------------------------------------------- /examples/learning/federated_program/vizier/vizier_service_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023, The TensorFlow Federated Authors. 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 | import os.path 16 | from unittest import mock 17 | 18 | from absl.testing import absltest 19 | 20 | from exampleslearning.federated_program.vizier import vizier_service 21 | 22 | from vizier.service import clients 23 | from vizier.service import pyvizier 24 | 25 | _STUDY_CONFIG_PATH = os.path.join( 26 | os.path.dirname(__file__), 27 | 'study_spec.textproto', 28 | ) 29 | 30 | 31 | class VizierServiceTest(absltest.TestCase): 32 | 33 | def test_create_study_config_returns_result(self): 34 | study_config = vizier_service.create_study_config(_STUDY_CONFIG_PATH) 35 | self.assertIsInstance(study_config, pyvizier.StudyConfig) 36 | 37 | def test_create_study_returns_result(self): 38 | study_config = vizier_service.create_study_config(_STUDY_CONFIG_PATH) 39 | 40 | with mock.patch.object( 41 | clients, 'Study', autospec=True, spec_set=True 42 | ) as mock_study: 43 | vizier_service.create_study( 44 | study_config=study_config, name='test', owner='owner' 45 | ) 46 | mock_study.from_study_config.assert_called_once_with( 47 | config=study_config, owner='owner', study_id='test' 48 | ) 49 | 50 | 51 | if __name__ == '__main__': 52 | absltest.main() 53 | -------------------------------------------------------------------------------- /tensorflow_federated/python/analytics/hierarchical_histogram/build_tree_from_leaf_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020, The TensorFlow Federated Authors. 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 | """Tests for utils.""" 15 | 16 | from absl.testing import parameterized 17 | import tensorflow as tf 18 | 19 | from tensorflow_federated.python.analytics.hierarchical_histogram import build_tree_from_leaf 20 | 21 | 22 | class UtilsTest(tf.test.TestCase, parameterized.TestCase): 23 | 24 | @parameterized.named_parameters( 25 | ( 26 | 'binary_no_padding', 27 | [1.0, 1.0, 1.0, 1.0], 28 | 2, 29 | [[4.0], [2.0, 2.0], [1.0, 1.0, 1.0, 1.0]], 30 | ), 31 | ( 32 | 'binary_padding', 33 | [1.0, 1.0, 1.0], 34 | 2, 35 | [[3.0], [2.0, 1.0], [1.0, 1.0, 1.0, 0.0]], 36 | ), 37 | ('ternary_no_padding', [1.0, 1.0, 1.0], 3, [[3.0], [1.0, 1.0, 1.0]]), 38 | ('ternary_padding', [1.0, 1.0], 3, [[2.0], [1.0, 1.0, 0.0]]), 39 | ) 40 | def test_create_hierarchical_hist( 41 | self, histogram, arity, expected_hierarchical_histogram 42 | ): 43 | hierarchical_histogram = build_tree_from_leaf.create_hierarchical_histogram( 44 | histogram, arity 45 | ) 46 | self.assertAllClose(hierarchical_histogram, expected_hierarchical_histogram) 47 | 48 | 49 | if __name__ == '__main__': 50 | tf.test.main() 51 | -------------------------------------------------------------------------------- /tensorflow_federated/python/analytics/hierarchical_histogram/build_tree_from_leaf.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020, The TensorFlow Federated Authors. 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 | """Utilities for hierarchical histogram.""" 15 | 16 | import math 17 | import numpy as np 18 | 19 | 20 | def create_hierarchical_histogram(histogram, arity: int): 21 | """Converts a histogram to its hierarchical representation. 22 | 23 | Args: 24 | histogram: An array_like representing the input histogram. 25 | arity: The branching factor of the hierarchical histogram. 26 | 27 | Returns: 28 | A list of 1-D lists. Each inner list represents one layer of the 29 | hierarchical histogram. 30 | """ 31 | if arity < 2: 32 | raise ValueError(f'Arity should be at least 2.arity={arity} is given.') 33 | 34 | depth = math.ceil(math.log(len(histogram), arity)) + 1 35 | size_ = arity ** (depth - 1) 36 | histogram = np.pad( 37 | histogram, (0, size_ - len(histogram)), 'constant', constant_values=(0, 0) 38 | ).tolist() 39 | 40 | def _shrink_histogram(histogram): 41 | return np.sum((np.reshape(histogram, (-1, arity))), axis=1).tolist() 42 | 43 | hierarchical_histogram = [histogram] 44 | for _ in range(depth - 1): 45 | hierarchical_histogram = [ 46 | _shrink_histogram(hierarchical_histogram[0]) 47 | ] + hierarchical_histogram 48 | 49 | return hierarchical_histogram 50 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/framework/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018, The TensorFlow Federated Authors. 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 | """Libraries for extending the TensorFlow Federated core library.""" 15 | 16 | # pylint: disable=g-importing-member 17 | from tensorflow_federated.python.core.impl.compiler.transformations import to_call_dominant 18 | from tensorflow_federated.python.core.impl.execution_contexts.mergeable_comp_execution_context import MergeableCompExecutionContext 19 | from tensorflow_federated.python.core.impl.execution_contexts.mergeable_comp_execution_context import MergeableCompForm 20 | from tensorflow_federated.python.core.impl.executor_stacks.executor_factory import local_cpp_executor_factory 21 | from tensorflow_federated.python.core.impl.executor_stacks.python_executor_stacks import ResourceManagingExecutorFactory 22 | from tensorflow_federated.python.core.impl.executors.remote_executor import RemoteExecutor 23 | from tensorflow_federated.python.core.impl.executors.remote_executor_grpc_stub import RemoteExecutorGrpcStub 24 | from tensorflow_federated.python.core.impl.executors.remote_executor_stub import RemoteExecutorStub 25 | from tensorflow_federated.python.core.impl.executors.value_serialization import deserialize_value 26 | from tensorflow_federated.python.core.impl.executors.value_serialization import serialize_value 27 | # pylint: enable=g-importing-member 28 | -------------------------------------------------------------------------------- /docs/collaborations/notes/2022-10-13.md: -------------------------------------------------------------------------------- 1 | # Notes from the SIG Federated meeting on 10/13/2022 2 | 3 | * Deep dive on TFF/OpenMined integration and logical partitioning, presented 4 | by Teo Milea 5 | * Questions: 6 | * Who audits whether a computation on the domain server is OK. 7 | * Several mechanism being considered. 8 | * On one end of the spectrum, we expect to support manual auditing and 9 | approval of submitted requests by the data owner. 10 | * On the other end of the spectrum, we aim to automated the process, 11 | e.g, by requiring DP, introducing privacy budget (max epsilon, 12 | delta), static analysis of model code to check for presence of 13 | filesystems ops, etc. 14 | * Boundary between TFF and PySyft and what’s new in PySyTFF, 15 | architecturally 16 | * Pointer to architectural diagram on 17 | [slides 28-33 in this deck from July 21](https://docs.google.com/presentation/d/105ZTOQ7PvUuZWyDZtCgIyaH10kQaXbiG27D2ML7aqio/edit?resourcekey=0-5NLe9CAcrnyWmB6BB2kt6g#slide=id.g13e7c73d85f_0_455) 18 | * TFF’s federated DSL and runtime abstractions are not the only part 19 | of the service that must run in a trusted space 20 | * Other logic that must run in trusted space, outside the control of 21 | the user includes code that constructs TFF computations, decides on 22 | the use of mechanisms such as DP, determines privacy budget, 23 | performs policy checks, drives the training loop, and decides on the 24 | release of individual artifacts (such as trained models or metrics) 25 | * PySyTFF is one example of how such logic can be layered on top of 26 | the TFF’s DSL and runtime, as a PySyft domain node service 27 | * We recommend other systems based on TFF to follow the same pattern 28 | * Conversations to continue on Discord 29 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/simulation/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:cc_binary.bzl", "cc_binary") 2 | load("@rules_cc//cc:cc_library.bzl", "cc_library") 3 | 4 | package( 5 | default_applicable_licenses = ["//:package_license"], 6 | default_visibility = [ 7 | ":simulation_packages", 8 | ":simulation_users", 9 | ], 10 | ) 11 | 12 | package_group( 13 | name = "simulation_packages", 14 | packages = ["//tensorflow_federated/cc/simulation/..."], 15 | ) 16 | 17 | package_group( 18 | name = "simulation_users", 19 | includes = [ 20 | "//tensorflow_federated/data:data_packages", 21 | "//tensorflow_federated/python/simulation:simulation_packages", 22 | "//tensorflow_federated/python/tests:tests_packages", 23 | "//tools:tools_packages", 24 | ], 25 | ) 26 | 27 | licenses(["notice"]) 28 | 29 | cc_library( 30 | name = "servers", 31 | srcs = ["servers.cc"], 32 | hdrs = ["servers.h"], 33 | deps = [ 34 | "//tensorflow_federated/cc/core/impl/executor_stacks:local_stacks", 35 | "//tensorflow_federated/cc/core/impl/executors:cardinalities", 36 | "//tensorflow_federated/cc/core/impl/executors:executor", 37 | "//tensorflow_federated/cc/core/impl/executors:executor_service", 38 | "//tensorflow_federated/cc/core/impl/executors:tensorflow_executor", 39 | "@com_github_grpc_grpc//:grpc++", 40 | "@com_google_absl//absl/log", 41 | "@com_google_absl//absl/status:statusor", 42 | "@com_google_absl//absl/strings", 43 | ], 44 | ) 45 | 46 | cc_library( 47 | name = "worker_main", 48 | srcs = ["worker_main.cc"], 49 | deps = [ 50 | ":servers", 51 | "@com_github_grpc_grpc//:grpc++", 52 | "@com_google_absl//absl/flags:flag", 53 | "@com_google_absl//absl/flags:parse", 54 | ], 55 | ) 56 | 57 | cc_binary( 58 | name = "worker_binary", 59 | linkopts = ["-lrt"], 60 | deps = [":worker_main"], 61 | ) 62 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/cardinalities.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_CARDINALITIES_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_CARDINALITIES_H_ 18 | 19 | #include 20 | 21 | #include "absl/container/btree_map.h" 22 | #include "absl/status/statusor.h" 23 | #include "absl/strings/string_view.h" 24 | 25 | namespace tensorflow_federated { 26 | 27 | // `btree_map` is used rather than `flat_hash_map` to provide ordering. 28 | // 29 | // This ensures that simple string-joining of the entries in this map will 30 | // produce a specific value for a given set of cardinalities independent of 31 | // ordering. the `ExecutorService` uses this as an optimization to provide 32 | // per-cardinality `ExecutorId`s. 33 | using CardinalityMap = absl::btree_map; 34 | const absl::string_view kClientsUri = "clients"; 35 | const absl::string_view kServerUri = "server"; 36 | 37 | // Returns the number of clients specified by the provided `cardinalities`. 38 | absl::StatusOr NumClientsFromCardinalities( 39 | const CardinalityMap& cardinalities); 40 | 41 | } // namespace tensorflow_federated 42 | 43 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_CARDINALITIES_H_ 44 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/impl/executor_stacks/executor_stack_bindings_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | from absl.testing import absltest 16 | from absl.testing import parameterized 17 | import federated_language 18 | import numpy as np 19 | 20 | from tensorflow_federated.python.core.impl.executor_stacks import executor_stack_bindings 21 | from tensorflow_federated.python.core.impl.executors import executor_bindings 22 | 23 | _TARGET_LIST = ['localhost:8000', 'localhost:8001'] 24 | _CARDINALITIES = {federated_language.CLIENTS: 5} 25 | 26 | 27 | class ExecutorStackBindingsTest(parameterized.TestCase): 28 | 29 | @parameterized.named_parameters( 30 | ('from_target_list', list), 31 | ('from_target_tuple', tuple), 32 | ('from_target_ndarray', np.array), 33 | ) 34 | def test_executor_construction_raises_no_channels_available( 35 | self, container_constructor 36 | ): 37 | with self.assertRaisesRegex(Exception, 'UNAVAILABLE'): 38 | executor_stack_bindings.create_remote_executor_stack( 39 | channels=container_constructor( 40 | [ 41 | executor_bindings.create_insecure_grpc_channel(t) 42 | for t in _TARGET_LIST 43 | ] 44 | ), 45 | cardinalities=_CARDINALITIES, 46 | ) 47 | 48 | 49 | if __name__ == '__main__': 50 | absltest.main() 51 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/impl/executors/cardinality.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """A set of utility methods for serializing Value protos using pybind11 bindings.""" 15 | 16 | from collections.abc import Mapping, Sequence 17 | 18 | import federated_language 19 | from federated_language.proto import computation_pb2 20 | 21 | from tensorflow_federated.proto.v0 import executor_pb2 22 | 23 | 24 | def serialize_cardinalities( 25 | cardinalities: Mapping[federated_language.framework.PlacementLiteral, int], 26 | ) -> list[executor_pb2.Cardinality]: 27 | serialized_cardinalities = [] 28 | for placement, cardinality in cardinalities.items(): 29 | cardinality_message = executor_pb2.Cardinality( 30 | placement=computation_pb2.Placement(uri=placement.uri), 31 | cardinality=cardinality, 32 | ) 33 | serialized_cardinalities.append(cardinality_message) 34 | return serialized_cardinalities 35 | 36 | 37 | def deserialize_cardinalities( 38 | serialized_cardinalities: Sequence[executor_pb2.Cardinality], 39 | ) -> dict[federated_language.framework.PlacementLiteral, int]: 40 | cardinalities = {} 41 | for cardinality_spec in serialized_cardinalities: 42 | literal = federated_language.framework.uri_to_placement_literal( 43 | cardinality_spec.placement.uri 44 | ) 45 | cardinalities[literal] = cardinality_spec.cardinality 46 | return cardinalities 47 | -------------------------------------------------------------------------------- /tensorflow_federated/python/tests/temperature_sensor_example.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020, The TensorFlow Federated Authors. 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 | """Simple temperature sensor example in TFF.""" 15 | 16 | import federated_language 17 | import numpy as np 18 | import tensorflow as tf 19 | import tensorflow_federated as tff 20 | 21 | 22 | @tff.tensorflow.computation( 23 | federated_language.SequenceType(np.float32), np.float32 24 | ) 25 | def count_over(ds, t): 26 | return ds.reduce( 27 | np.float32(0), lambda n, x: n + tf.cast(tf.greater(x, t), tf.float32) 28 | ) 29 | 30 | 31 | @tff.tensorflow.computation(federated_language.SequenceType(np.float32)) 32 | def count_total(ds): 33 | return ds.reduce(np.float32(0.0), lambda n, _: n + 1.0) 34 | 35 | 36 | @federated_language.federated_computation( 37 | federated_language.FederatedType( 38 | federated_language.SequenceType(np.float32), federated_language.CLIENTS 39 | ), 40 | federated_language.FederatedType(np.float32, federated_language.SERVER), 41 | ) 42 | def mean_over_threshold(temperatures, threshold): 43 | client_data = federated_language.federated_broadcast(threshold) 44 | client_data = federated_language.federated_zip([temperatures, client_data]) 45 | result_map = federated_language.federated_map(count_over, client_data) 46 | count_map = federated_language.federated_map(count_total, temperatures) 47 | return federated_language.federated_mean(result_map, count_map) 48 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/dataset_from_tensor_structures.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_DATASET_FROM_TENSOR_STRUCTURES_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_DATASET_FROM_TENSOR_STRUCTURES_H_ 18 | 19 | #include 20 | 21 | #include "absl/status/statusor.h" 22 | #include "absl/types/span.h" 23 | #include "tensorflow/core/framework/graph.pb.h" 24 | #include "tensorflow/core/framework/tensor.h" 25 | 26 | namespace tensorflow_federated { 27 | 28 | // Creates a TensorFlow dataset from a list of tensor structures and serializes 29 | // it into a single string tensor. 30 | // 31 | // Requirements: `tensor_structures` must be a list of lists of tensors. Each 32 | // sub-list represents a structure that will be yielded from the dataset, hence 33 | // all structures must have the same shape. Corresponding elements 34 | // across structures (e.g. the third element of every tensor_structures[i]) 35 | // must have the same shape and dtype. 36 | absl::StatusOr DatasetFromTensorStructures( 37 | absl::Span> tensor_structures); 38 | 39 | } // namespace tensorflow_federated 40 | 41 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_DATASET_FROM_TENSOR_STRUCTURES_H_ 42 | -------------------------------------------------------------------------------- /tensorflow_federated/python/learning/models/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Libraries for working with models in federated learning algorithms.""" 15 | 16 | from tensorflow_federated.python.learning.models.functional import functional_model_from_keras 17 | from tensorflow_federated.python.learning.models.functional import FunctionalModel 18 | from tensorflow_federated.python.learning.models.functional import model_from_functional 19 | from tensorflow_federated.python.learning.models.keras_utils import from_keras_model 20 | from tensorflow_federated.python.learning.models.model_weights import ModelWeights 21 | from tensorflow_federated.python.learning.models.model_weights import weights_type_from_model 22 | from tensorflow_federated.python.learning.models.reconstruction_model import ReconstructionDatasetSplitFn 23 | from tensorflow_federated.python.learning.models.reconstruction_model import ReconstructionModel 24 | from tensorflow_federated.python.learning.models.serialization import load 25 | from tensorflow_federated.python.learning.models.serialization import load_functional_model 26 | from tensorflow_federated.python.learning.models.serialization import save 27 | from tensorflow_federated.python.learning.models.serialization import save_functional_model 28 | from tensorflow_federated.python.learning.models.variable import BatchOutput 29 | from tensorflow_federated.python.learning.models.variable import VariableModel 30 | -------------------------------------------------------------------------------- /tools/build_docs.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019, The TensorFlow Federated Authors. 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 | """Tool to generate external API documentation for tensorflow_federated.""" 15 | 16 | import os 17 | 18 | from absl import app 19 | from absl import flags 20 | from tensorflow_docs import api_generator 21 | import tensorflow_federated as tff 22 | 23 | FLAGS = flags.FLAGS 24 | 25 | flags.DEFINE_string( 26 | 'output_dir', '/tmp/federated_api', 'Where to output the docs' 27 | ) 28 | 29 | flags.DEFINE_string( 30 | 'code_url_prefix', None, 'The url prefix for links to code.' 31 | ) 32 | 33 | flags.DEFINE_bool( 34 | 'search_hints', True, 'Include metadata search hints in the generated files' 35 | ) 36 | 37 | flags.DEFINE_string( 38 | 'site_path', 'federated/api_docs/python', 'Path prefix in the _toc.yaml' 39 | ) 40 | 41 | 42 | def main(argv): 43 | if len(argv) > 1: 44 | raise app.UsageError('Too many command-line arguments.') 45 | doc_generator = api_generator.generate_lib.DocGenerator( 46 | root_title='TensorFlow Federated', 47 | py_modules=[('tff', tff)], 48 | base_dir=os.path.dirname(tff.__file__), 49 | code_url_prefix=FLAGS.code_url_prefix, 50 | search_hints=FLAGS.search_hints, 51 | site_path=FLAGS.site_path, 52 | callbacks=[ 53 | api_generator.public_api.explicit_package_contents_filter, 54 | ], 55 | ) 56 | doc_generator.build(FLAGS.output_dir) 57 | 58 | 59 | if __name__ == '__main__': 60 | app.run(main) 61 | -------------------------------------------------------------------------------- /tensorflow_federated/python/core/impl/executors/data_conversions.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Helpers for converting Python data representations for CPP bindings.""" 15 | 16 | from collections.abc import Mapping 17 | 18 | import federated_language 19 | 20 | 21 | def convert_cardinalities_dict_to_string_keyed( 22 | cardinalities: Mapping[federated_language.framework.PlacementLiteral, int], 23 | ) -> Mapping[str, int]: 24 | """Ensures incoming cardinalities dict is formatted correctly.""" 25 | if not isinstance(cardinalities, Mapping): 26 | raise TypeError( 27 | '`cardinalities` must be a `Mapping`. Received a type: ' 28 | f'{type(cardinalities)}.' 29 | ) 30 | uri_cardinalities = {} 31 | for placement, cardinality in cardinalities.items(): 32 | if not isinstance(placement, federated_language.framework.PlacementLiteral): 33 | raise TypeError( 34 | '`cardinalities` must be a `Mapping` with ' 35 | '`PlacementLiteral` (e.g. `federated_language.CLIENTS`) keys. ' 36 | f'Received a key of type: {type(placement)}.' 37 | ) 38 | if not isinstance(cardinality, int): 39 | raise TypeError( 40 | '`cardinalities` must be a `Mapping` with `int` values. ' 41 | f'Received a value of type: {type(cardinality)} for ' 42 | f'placement {placement}.' 43 | ) 44 | uri_cardinalities[placement.uri] = cardinality 45 | return uri_cardinalities 46 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/core/tensor_slice_data.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2025 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "tensorflow_federated/cc/core/impl/aggregation/core/tensor_slice_data.h" 17 | 18 | #include 19 | #include 20 | 21 | #include "tensorflow_federated/cc/core/impl/aggregation/base/monitoring.h" 22 | #include "tensorflow_federated/cc/core/impl/aggregation/core/tensor.h" 23 | 24 | namespace tensorflow_federated { 25 | namespace aggregation { 26 | Status TensorSliceData::ReduceByteSize(size_t new_size) { 27 | if (new_size > byte_size_) { 28 | return TFF_STATUS(INVALID_ARGUMENT) 29 | << "TensorSliceData::ReduceSize: target size " << new_size 30 | << " is greater than the original size " << byte_size_; 31 | } 32 | byte_size_ = new_size; 33 | return TFF_STATUS(OK); 34 | } 35 | 36 | StatusOr TensorSliceData::Create(Tensor&& tensor, 37 | size_t byte_size, 38 | size_t byte_offset) { 39 | if (byte_offset + byte_size > tensor.data().byte_size()) { 40 | return TFF_STATUS(INVALID_ARGUMENT) 41 | << "TensorSliceData::Create: byte_offset + byte_size cannot exceed " 42 | "the tensor's size"; 43 | } 44 | return TensorSliceData(std::move(tensor), byte_size, byte_offset); 45 | } 46 | 47 | } // namespace aggregation 48 | } // namespace tensorflow_federated 49 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/streaming_remote_executor.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_STREAMING_REMOTE_EXECUTOR_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_STREAMING_REMOTE_EXECUTOR_H_ 18 | 19 | #include 20 | 21 | #include "include/grpcpp/grpcpp.h" 22 | #include "tensorflow_federated/cc/core/impl/executors/cardinalities.h" 23 | #include "tensorflow_federated/cc/core/impl/executors/executor.h" 24 | #include "tensorflow_federated/proto/v0/executor.grpc.pb.h" 25 | 26 | namespace tensorflow_federated { 27 | 28 | // Returns an executor which communicates with a remote executor service. 29 | // 30 | // This executor differs from `RemoteExecutor` by "streaming" structures of 31 | // tensors one-by-one, avoiding the 2 GB size limit of serialization protocol 32 | // buffers for very large Struct values with many intermediate sized tensors. 33 | std::shared_ptr CreateStreamingRemoteExecutor( 34 | std::shared_ptr channel, 35 | const CardinalityMap& cardinalities); 36 | std::shared_ptr CreateStreamingRemoteExecutor( 37 | std::unique_ptr stub, 38 | const CardinalityMap& cardinalities); 39 | } // namespace tensorflow_federated 40 | 41 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_STREAMING_REMOTE_EXECUTOR_H_ 42 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/aggregation/base/move_to_lambda_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "tensorflow_federated/cc/core/impl/aggregation/base/move_to_lambda.h" 18 | 19 | #include "googlemock/include/gmock/gmock.h" 20 | #include "googletest/include/gtest/gtest.h" 21 | #include "tensorflow_federated/cc/core/impl/aggregation/base/unique_value.h" 22 | 23 | namespace tensorflow_federated { 24 | 25 | using ::testing::Eq; 26 | 27 | TEST(MoveToLambda, Basic) { 28 | auto capture = MoveToLambda(UniqueValue{123}); 29 | auto lambda = [capture]() { 30 | EXPECT_TRUE(capture->has_value()) << "Should have moved the original"; 31 | return **capture; 32 | }; 33 | 34 | int returned = lambda(); 35 | EXPECT_FALSE(capture->has_value()) << "Should have moved the original"; 36 | EXPECT_THAT(returned, Eq(123)); 37 | 38 | int returned_again = lambda(); 39 | EXPECT_THAT(returned_again, Eq(123)) << "Usage shouldn't be destructive"; 40 | } 41 | 42 | TEST(MoveToLambda, Mutable) { 43 | auto capture = MoveToLambda(UniqueValue{0}); 44 | auto counter = [capture]() mutable { 45 | EXPECT_TRUE(capture->has_value()) << "Should have moved the original"; 46 | return (**capture)++; 47 | }; 48 | 49 | EXPECT_FALSE(capture->has_value()) << "Should have moved the original"; 50 | 51 | EXPECT_THAT(counter(), Eq(0)); 52 | EXPECT_THAT(counter(), Eq(1)); 53 | EXPECT_THAT(counter(), Eq(2)); 54 | } 55 | 56 | } // namespace tensorflow_federated 57 | -------------------------------------------------------------------------------- /tensorflow_federated/python/analytics/heavy_hitters/iblt/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021, The TensorFlow Federated Authors. 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 | """Libraries for computing private heavy hitters with IBLT.""" 15 | 16 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.chunkers import CharacterEncoding 17 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.chunkers import create_chunker 18 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.hyperedge_hashers import CoupledHyperEdgeHasher 19 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.hyperedge_hashers import RandomHyperEdgeHasher 20 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.iblt_clipping import ClippingIbltFactory 21 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.iblt_lib import decode_iblt_tf 22 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.iblt_lib import DEFAULT_REPETITIONS 23 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.iblt_lib import IbltDecoder 24 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.iblt_lib import IbltEncoder 25 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.iblt_tensor import decode_iblt_tensor_tf 26 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.iblt_tensor import IbltTensorDecoder 27 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.iblt_tensor import IbltTensorEncoder 28 | from tensorflow_federated.python.analytics.heavy_hitters.iblt.iblt_tff import build_iblt_computation 29 | -------------------------------------------------------------------------------- /tensorflow_federated/cc/core/impl/executors/executor_test_base.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2021, The TensorFlow Federated Authors. 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 THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_EXECUTOR_TEST_BASE_H_ 17 | #define THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_EXECUTOR_TEST_BASE_H_ 18 | 19 | #include 20 | 21 | #include "googlemock/include/gmock/gmock.h" 22 | #include "googletest/include/gtest/gtest.h" 23 | #include "tensorflow_federated/cc/core/impl/executors/executor.h" 24 | #include "tensorflow_federated/cc/testing/protobuf_matchers.h" 25 | #include "tensorflow_federated/cc/testing/status_matchers.h" 26 | #include "tensorflow_federated/proto/v0/executor.pb.h" 27 | 28 | namespace tensorflow_federated { 29 | 30 | class ExecutorTestBase : public ::testing::Test { 31 | public: 32 | ~ExecutorTestBase() override = default; 33 | 34 | protected: 35 | std::shared_ptr test_executor_; 36 | 37 | void ExpectMaterialize(ValueId id, v0::Value value) { 38 | auto result = TFF_ASSERT_OK(test_executor_->Materialize(id)); 39 | EXPECT_THAT(result, testing::EqualsProto(value)); 40 | } 41 | 42 | void ExpectCreateMaterialize(v0::Value value) { 43 | auto id = TFF_ASSERT_OK(test_executor_->CreateValue(value)); 44 | ExpectMaterialize(id, value); 45 | } 46 | }; 47 | 48 | } // namespace tensorflow_federated 49 | 50 | #endif // THIRD_PARTY_TENSORFLOW_FEDERATED_CC_CORE_IMPL_EXECUTORS_EXECUTOR_TEST_BASE_H_ 51 | --------------------------------------------------------------------------------