in tensorflow_federated/python/core/impl/compiler/transformations.py [0:0]
def _build(comp, scope):
"""Transforms `comp` to CDF, possibly adding bindings to `scope`."""
# The structure returned by this function is a generalized version of
# call-dominant form. This function may result in the patterns specified in
# the top-level function's docstring.
if comp.is_reference():
result = scope.resolve(comp.name)
if result is None:
# If `comp.name` is only bound outside of `comp`, we can't resolve it.
return comp
return result
elif comp.is_selection():
source = _build(comp.source, scope)
if source.is_struct():
return source[comp.as_index()]
return building_blocks.Selection(source, index=comp.as_index())
elif comp.is_struct():
elements = []
for (name, value) in structure.iter_elements(comp):
value = _build(value, scope)
elements.append((name, value))
return building_blocks.Struct(elements)
elif comp.is_call():
function = _build(comp.function, scope)
argument = None if comp.argument is None else _build(comp.argument, scope)
if function.is_lambda():
if argument is not None:
scope = scope.new_child()
scope.add_local(function.parameter_name, argument)
return _build(function.result, scope)
else:
return scope.create_binding(building_blocks.Call(function, argument))
elif comp.is_lambda():
scope = scope.new_child_with_bindings()
if comp.parameter_name:
scope.add_local(
comp.parameter_name,
building_blocks.Reference(comp.parameter_name, comp.parameter_type))
result = _build(comp.result, scope)
block = scope.bindings_to_block_with_result(result)
return building_blocks.Lambda(comp.parameter_name, comp.parameter_type,
block)
elif comp.is_block():
scope = scope.new_child()
for (name, value) in comp.locals:
scope.add_local(name, _build(value, scope))
return _build(comp.result, scope)
elif (comp.is_intrinsic() or comp.is_data() or
comp.is_compiled_computation() or comp.is_placement()):
return comp
else:
raise ValueError(
f'Unrecognized computation kind\n{comp}\nin\n{global_comp}')