def test_with_single_abstract_type_and_tuple_type(self): t1 = self.func_with_param(computation_types.AbstractType('T1')) t2 = self.func_with_param(computation_types.StructType([tf.int32])) type_analysis.check_concrete_instance_of(t2, t1)
def test_raises_with_conflicting_names(self): t1 = computation_types.StructType([tf.int32] * 2) t2 = computation_types.StructType([('a', tf.int32), ('b', tf.int32)]) with self.assertRaises(type_analysis.MismatchedStructureError): type_analysis.check_concrete_instance_of(t2, t1)
def test_with_single_abstract_type_and_tensor_type(self): t1 = computation_types.AbstractType('T1') t2 = computation_types.TensorType(tf.int32) type_analysis.check_concrete_instance_of(t2, t1)
def test_raises_with_abstract_type_in_first_and_second_argument(self): t1 = computation_types.AbstractType('T1') t2 = computation_types.AbstractType('T2') with self.assertRaises(type_analysis.NotConcreteTypeError): type_analysis.check_concrete_instance_of(t2, t1)
def test_raises_different_structures(self): with self.assertRaises(type_analysis.MismatchedStructureError): type_analysis.check_concrete_instance_of( computation_types.TensorType(tf.int32), computation_types.StructType([tf.int32]))
def test_raises_with_abstract_type_as_first_arg(self): t1 = computation_types.AbstractType('T1') t2 = computation_types.TensorType(tf.int32) with self.assertRaises(type_analysis.NotConcreteTypeError): type_analysis.check_concrete_instance_of(t1, t2)
def test_raises_with_int_second_argument(self): with self.assertRaises(TypeError): type_analysis.check_concrete_instance_of( computation_types.TensorType(tf.int32), 1)
async def create_value( self, value: Any, type_spec: Any = None) -> executor_value_base.ExecutorValue: """Creates an embedded value from the given `value` and `type_spec`. The kinds of supported `value`s are: * An instance of `intrinsic_defs.IntrinsicDef`. * An instance of `placement_literals.PlacementLiteral`. * An instance of `pb.Computation` if of one of the following kinds: intrinsic, lambda, tensorflow, or xla. * A Python `list` if `type_spec` is a federated type. Note: The `value` must be a list even if it is of an `all_equal` type or if there is only a single participant associated with the given placement. * A Python value if `type_spec` is a non-functional, non-federated type. Args: value: An object to embed in the executor, one of the supported types defined by above. type_spec: An optional type convertible to instance of `tff.Type` via `tff.to_type`, the type of `value`. Returns: An instance of `executor_value_base.ExecutorValue` representing a value embedded in the `FederatingExecutor` using a particular `FederatingStrategy`. Raises: TypeError: If the `value` and `type_spec` do not match. ValueError: If `value` is not a kind supported by the `FederatingExecutor`. """ type_spec = computation_types.to_type(type_spec) if isinstance(value, intrinsic_defs.IntrinsicDef): type_analysis.check_concrete_instance_of(type_spec, value.type_signature) return self._strategy.ingest_value(value, type_spec) elif isinstance(value, placement_literals.PlacementLiteral): if type_spec is None: type_spec = computation_types.PlacementType() type_spec.check_placement() return self._strategy.ingest_value(value, type_spec) elif isinstance(value, computation_impl.ComputationImpl): return await self.create_value( computation_impl.ComputationImpl.get_proto(value), executor_utils.reconcile_value_with_type_spec(value, type_spec)) elif isinstance(value, pb.Computation): deserialized_type = type_serialization.deserialize_type(value.type) if type_spec is None: type_spec = deserialized_type else: type_spec.check_assignable_from(deserialized_type) which_computation = value.WhichOneof('computation') if which_computation in ['lambda', 'tensorflow', 'xla']: return self._strategy.ingest_value(value, type_spec) elif which_computation == 'intrinsic': if value.intrinsic.uri in FederatingExecutor._FORWARDED_INTRINSICS: return self._strategy.ingest_value(value, type_spec) intrinsic_def = intrinsic_defs.uri_to_intrinsic_def(value.intrinsic.uri) if intrinsic_def is None: raise ValueError('Encountered an unrecognized intrinsic "{}".'.format( value.intrinsic.uri)) return await self.create_value(intrinsic_def, type_spec) else: raise ValueError( 'Unsupported computation building block of type "{}".'.format( which_computation)) elif type_spec is not None and type_spec.is_federated(): return await self._strategy.compute_federated_value(value, type_spec) else: result = await self._unplaced_executor.create_value(value, type_spec) return self._strategy.ingest_value(result, type_spec)
def test_succeeds_single_function_type(self): t1 = computation_types.FunctionType(*[computation_types.AbstractType('T')] * 2) t2 = computation_types.FunctionType(tf.int32, tf.int32) type_analysis.check_concrete_instance_of(t2, t1)
def test_succeeds_abstract_type_under_sequence_type(self): t1 = self.func_with_param( computation_types.SequenceType(computation_types.AbstractType('T'))) t2 = self.func_with_param(computation_types.SequenceType(tf.int32)) type_analysis.check_concrete_instance_of(t2, t1)
def test_raises_with_different_lengths(self): t1 = computation_types.StructType([tf.int32] * 2) t2 = computation_types.StructType([tf.int32]) with self.assertRaises(type_analysis.MismatchedStructureError): type_analysis.check_concrete_instance_of(t2, t1)