コード例 #1
0
ファイル: executor.py プロジェクト: peiji1981/federated
def to_representation_for_type(value, type_spec=None, backend=None):
    """Verifies or converts the `value` to executor payload matching `type_spec`.

  The following kinds of `value` are supported:

  * Computations, either `pb.Computation` or `computation_impl.ComputationImpl`.
    These are compiled and converted into `runtime.ComputationCallable`.

  * Numpy arrays and scalars, or Python scalars that are converted to Numpy.

  Args:
    value: The raw representation of a value to compare against `type_spec` and
      potentially to be converted.
    type_spec: An instance of `tff.Type`. Can be `None` for values that derive
      from `typed_object.TypedObject`.
    backend: Optional information about the backend, only required for
      computations. Must be `None` or an instance of `backend_info.BackendInfo`.

  Returns:
    Either `value` itself, or a modified version of it.

  Raises:
    TypeError: If the `value` is not compatible with `type_spec`.
    ValueError: If the arguments are incorrect (e.g., missing `backend` for a
      computation-typed `value`).
  """
    type_spec = type_utils.reconcile_value_with_type_spec(value, type_spec)
    if backend is not None:
        py_typecheck.check_type(backend, backend_info.BackendInfo)
    if isinstance(value, computation_base.Computation):
        return to_representation_for_type(
            computation_impl.ComputationImpl.get_proto(value),
            type_spec=type_spec,
            backend=backend)
    elif isinstance(value, pb.Computation):
        if backend is None:
            raise ValueError('Missing backend info for a computation.')
        module = compiler.import_tensorflow_computation(value)
        return runtime.ComputationCallable(module, backend)
    elif isinstance(type_spec, computation_types.TensorType):
        type_spec.shape.assert_is_fully_defined()
        type_analysis.check_type(value, type_spec)
        if type_spec.shape.rank == 0:
            return np.dtype(type_spec.dtype.as_numpy_dtype).type(value)
        elif type_spec.shape.rank > 0:
            return np.array(value, dtype=type_spec.dtype.as_numpy_dtype)
        else:
            raise TypeError('Unsupported tensor shape {}.'.format(
                type_spec.shape))
    else:
        raise TypeError('Unexpected type {}.'.format(type_spec))
コード例 #2
0
    def _import_compile_and_return_module_and_mlir(self, comp):
        """Testing helper that compiles `comp` and returns the compiler module.

    Args:
      comp: A computation created with `@computations.tf_computation`.

    Returns:
      A tuple consisting of the compiler module and MLIR.
    """
        comp_proto = computation_impl.ComputationImpl.get_proto(comp)
        module = compiler.import_tensorflow_computation(comp_proto)
        self.assertIsInstance(module, computation_module.ComputationModule)
        mlir = module.compiler_module.to_asm(large_element_limit=100)
        return module, mlir