コード例 #1
0
    def __call__(self, *inputs, **kwargs):
        pipeline = Pipeline.current()
        if pipeline is None:
            Pipeline._raise_no_current_pipeline("NumbaFunction")
        inputs = ops._preprocess_inputs(inputs, self._impl_name, self._device,
                                        None)
        if pipeline is None:
            Pipeline._raise_pipeline_required("NumbaFunction operator")
        if (len(inputs) > self._schema.MaxNumInput()
                or len(inputs) < self._schema.MinNumInput()):
            raise ValueError(("Operator {} expects from {} to " +
                              "{} inputs, but received {}.").format(
                                  type(self).__name__,
                                  self._schema.MinNumInput(),
                                  self._schema.MaxNumInput(), len(inputs)))
        for inp in inputs:
            if not isinstance(inp, _DataNode):
                raise TypeError((
                    "Expected inputs of type `DataNode`. Received input of type '{}'. "
                    + "Python Operators do not support Multiple Input Sets."
                ).format(type(inp).__name__))
        op_instance = ops._OperatorInstance(inputs, self, **kwargs)
        op_instance.spec.AddArg("run_fn", self.run_fn)
        op_instance.spec.AddArg("setup_fn",
                                self.setup_fn) if self.setup_fn else None
        op_instance.spec.AddArg("out_types", self.out_types)
        op_instance.spec.AddArg("in_types", self.in_types)
        op_instance.spec.AddArg("outs_ndim", self.outs_ndim)
        op_instance.spec.AddArg("ins_ndim", self.ins_ndim)
        op_instance.spec.AddArg("device", self.device)
        op_instance.spec.AddArg("batch_processing", self.batch_processing)
        if self.device == 'gpu':
            op_instance.spec.AddArg("blocks", self.blocks)
            op_instance.spec.AddArg("threads_per_block",
                                    self.threads_per_block)

        if self.num_outputs == 0:
            t_name = self._impl_name + "_id_" + str(op_instance.id) + "_sink"
            t = _DataNode(t_name, self._device, op_instance)
            pipeline.add_sink(t)
            return
        outputs = []

        for i in range(self.num_outputs):
            t_name = op_instance._name
            if self.num_outputs > 1:
                t_name += "[{}]".format(i)
            t = _DataNode(t_name, self._device, op_instance)
            op_instance.spec.AddOutput(t.name, t.device)
            op_instance.append_output(t)
            pipeline.add_sink(t)
            outputs.append(t)
        return outputs[0] if len(outputs) == 1 else outputs
コード例 #2
0
ファイル: external_source.py プロジェクト: mikechen66/DALI
    def __call__(self,
                 *,
                 source=None,
                 cycle=None,
                 name=None,
                 layout=None,
                 cuda_stream=None,
                 use_copy_kernel=None,
                 **kwargs):
        ""
        from nvidia.dali.ops import _OperatorInstance

        if source is None:
            if cycle is not None:
                if self._callback:
                    raise ValueError(
                        "The argument `cycle` can only be specified if `source` is an iterable object "
                        "or a generator function specified in this call. To cycle through an iterable specified in "
                        "`__init__`, set `cycle` there.")
                else:
                    raise ValueError(
                        "The argument `cycle` can only be specified if `source` is a "
                        "reusable iterable or a generator function.")
            callback = self._callback
        else:
            if self._callback is not None:
                raise RuntimeError(
                    "`source` already specified in constructor.")
            callback = _get_callback_from_source(source, cycle)

        if self._layout is not None:
            if layout is not None:
                raise RuntimeError(
                    "`layout` already specified in constructor.")
            else:
                layout = self._layout

        if self._cuda_stream is not None:
            if cuda_stream is not None:
                raise RuntimeError(
                    "`cuda_stream` already specified in constructor.")
            else:
                cuda_stream = self._cuda_stream

        if self._use_copy_kernel is not None:
            if use_copy_kernel is not None:
                raise RuntimeError(
                    "`use_copy_kernel` already specified in constructor.")
            else:
                use_copy_kernel = self._use_copy_kernel

        if name is None:
            name = self._name

        if name is not None and self._num_outputs is not None:
            raise RuntimeError(
                "`num_outputs` is not compatible with named `ExternalSource`")

        if self._num_outputs is not None:
            outputs = []
            kwargs = {}
            group = _ExternalSourceGroup(callback,
                                         True,
                                         cuda_stream=cuda_stream,
                                         use_copy_kernel=use_copy_kernel)
            for i in range(self._num_outputs):
                op_instance = _OperatorInstance([], self, **kwargs)
                op_instance._callback = callback
                op_instance._output_index = i
                op_instance._group = group
                if layout is not None:
                    if isinstance(layout, (list, tuple)):
                        op_instance._layout = layout[i] if i < len(
                            layout) else ""
                    else:
                        op_instance._layout = layout
                else:
                    op_instance._layout = ""

                group.append(op_instance)
                op_instance.generate_outputs()
                outputs.append(op_instance.unwrapped_outputs)

            return outputs
        else:
            if name is not None:
                kwargs["name"] = name
            op_instance = _OperatorInstance([], self, **kwargs)
            op_instance._callback = callback
            op_instance._output_index = None
            op_instance._group = _ExternalSourceGroup(
                callback,
                False, [op_instance],
                cuda_stream=cuda_stream,
                use_copy_kernel=use_copy_kernel)
            op_instance._layout = layout if layout is not None else ""
            op_instance.generate_outputs()

            return op_instance.unwrapped_outputs
コード例 #3
0
    def __call__(
            self, *, source=None, cycle=None, name=None, layout=None, cuda_stream=None,
            use_copy_kernel=None, batch=None, parallel=None, no_copy=None,
            prefetch_queue_depth=None, **kwargs):
        ""
        from nvidia.dali.ops import _OperatorInstance

        if source is None:
            if cycle is not None:
                if self._callback:
                    raise ValueError("The argument ``cycle`` can only be specified if ``source`` is an iterable object "
                        "or a generator function specified in this call. To cycle through an iterable specified in "
                        "``__init__``, set ``cycle`` there.")
                else:
                    raise ValueError("The argument ``cycle`` can only be specified if ``source`` is a "
                                     "reusable iterable or a generator function.")
            callback = self._callback
        else:
            if self._callback is not None:
                raise RuntimeError("``source`` already specified in constructor.")
            callback = _get_callback_from_source(source, cycle)

        if parallel is None:
            parallel = self._parallel or False
        elif self._parallel is not None:
            raise ValueError("The argument ``parallel`` already specified in constructor.")

        if batch is None:
            batch = self._batch
        elif self._batch is not None:
            raise ValueError("The argument ``batch`` already specified in constructor.")

        # By default parallel is False, so batch will be True
        if batch is None:
            batch = not parallel

        if prefetch_queue_depth is None:
            prefetch_queue_depth = self._prefetch_queue_depth
        elif self._prefetch_queue_depth is not None:
            raise ValueError(
                "The argument ``prefetch_queue_depth`` already specified in constructor.")

        if no_copy is None:
            no_copy = self._no_copy
        elif self._no_copy is not None:
            raise ValueError("The argument ``no_copy`` already specified in constructor.")

        if parallel:
            if prefetch_queue_depth is None:
                prefetch_queue_depth = 1
            if no_copy is None:
                no_copy = True
            if not no_copy:
                raise ValueError("The argument ``no_copy`` cannot be specified to False " +
                    " when used with ``parallel=True``.")
            if batch:
                raise ValueError("ExternalSource can be run in parallel only in per-sample " +
                    "(``batch=False``) mode.")
            if prefetch_queue_depth < 1:
                raise ValueError(
                    "``prefetch_queue_depth`` must be a positive integer, got {}.".format(
                        prefetch_queue_depth))
        else:
            if prefetch_queue_depth is not None:
                raise ValueError("The argument `prefetch_queue_depth` is valid only for " +
                    "parallel external sources (when ``parallel`` is True).")

        if self._layout is not None:
            if layout is not None:
                raise RuntimeError("``layout`` already specified in constructor.")
            else:
                layout = self._layout

        if self._cuda_stream is not None:
            if cuda_stream is not None:
                raise RuntimeError("``cuda_stream`` already specified in constructor.")
            else:
                cuda_stream = self._cuda_stream

        if self._use_copy_kernel is not None:
            if use_copy_kernel is not None:
                raise RuntimeError("``use_copy_kernel`` already specified in constructor.")
            else:
                use_copy_kernel = self._use_copy_kernel

        if name is None:
            name = self._name

        if name is not None and self._num_outputs is not None:
            raise RuntimeError("``num_outputs`` is not compatible with named ``ExternalSource``.")

        group_common_kwargs = {
            'cuda_stream': cuda_stream,
            'use_copy_kernel': use_copy_kernel,
            'batch': batch,
            'parallel': parallel,
            'prefetch_queue_depth': prefetch_queue_depth,
        }

        if self._num_outputs is not None:
            outputs = []
            kwargs = {"no_copy": no_copy}
            group = _ExternalSourceGroup(callback, True, **group_common_kwargs)
            for i in range(self._num_outputs):
                op_instance = _OperatorInstance([], self, **kwargs)
                op_instance._callback = callback
                op_instance._output_index = i
                op_instance._group = group
                if layout is not None:
                    if isinstance(layout, (list, tuple)):
                        op_instance._layout = layout[i] if i < len(layout) else ""
                    else:
                        op_instance._layout = layout
                else:
                    op_instance._layout = None

                group.append(op_instance)
                op_instance.generate_outputs()
                outputs.append(op_instance.unwrapped_outputs)

            return outputs
        else:
            if name is not None:
                kwargs["name"] = name
            if no_copy is not None:
                kwargs["no_copy"] = no_copy
            op_instance = _OperatorInstance([], self, **kwargs)
            op_instance._callback = callback
            op_instance._output_index = None
            op_instance._group = _ExternalSourceGroup(
                callback, False, [op_instance], **group_common_kwargs)
            op_instance._layout = layout
            op_instance.generate_outputs()

            return op_instance.unwrapped_outputs
コード例 #4
0
ファイル: external_source.py プロジェクト: hixio-mh/DALI
    def __call__(
            self, *, source=None, cycle=None, name=None, layout=None, dtype=None, cuda_stream=None,
            use_copy_kernel=None, batch=None, parallel=None, no_copy=None,
            prefetch_queue_depth=None, batch_info=None, **kwargs):
        ""
        from nvidia.dali.ops import _OperatorInstance

        if batch_info is None:
            batch_info = self._batch_info or False
        elif self._batch_info is not None:
            raise ValueError(
                "The argument ``batch_info`` already specified in constructor.")

        if source is None:
            if cycle is not None:
                if self._callback:
                    raise ValueError("The argument ``cycle`` can only be specified if ``source`` is an iterable object "
                        "or a generator function specified in this call. To cycle through an iterable specified in "
                        "``__init__``, set ``cycle`` there.")
                else:
                    raise ValueError("The argument ``cycle`` can only be specified if ``source`` is a "
                                     "reusable iterable or a generator function.")
            callback = self._callback
            source_desc = self._source_desc
        else:
            if self._callback is not None:
                raise RuntimeError("``source`` already specified in constructor.")
            callback, source_desc = _get_callback_from_source(source, cycle, self._batch_info)

            # Keep the metadata for Pipeline inspection
            self._source_desc = source_desc

        if parallel is None:
            parallel = self._parallel or False
        elif self._parallel is not None:
            raise ValueError("The argument ``parallel`` already specified in constructor.")

        if batch is None:
            batch = self._batch
        elif self._batch is not None:
            raise ValueError("The argument ``batch`` already specified in constructor.")

        # By default parallel is False, so batch will be True
        if batch is None:
            batch = not parallel

        if prefetch_queue_depth is None:
            prefetch_queue_depth = self._prefetch_queue_depth
        elif self._prefetch_queue_depth is not None:
            raise ValueError(
                "The argument ``prefetch_queue_depth`` already specified in constructor.")

        if no_copy is None:
            no_copy = self._no_copy
        elif self._no_copy is not None:
            raise ValueError("The argument ``no_copy`` already specified in constructor.")

        if parallel:
            if prefetch_queue_depth is None:
                prefetch_queue_depth = 1
            if no_copy is None:
                no_copy = True
            if not no_copy:
                raise ValueError("The argument ``no_copy`` cannot be specified to False " +
                    " when used with ``parallel=True``.")
            if prefetch_queue_depth < 1:
                raise ValueError(
                    "``prefetch_queue_depth`` must be a positive integer, got {}.".format(
                        prefetch_queue_depth))
            if source_desc.kind == _SourceKind.CALLABLE:
                if not source_desc.has_inputs:
                    raise TypeError(("Callable passed to External Source in parallel mode (when `parallel=True`) "
                            "must accept exactly one argument: `nvidia.dali.types.SampleInfo` "
                            "if run with `batch=False` or either `nvidia.dali.types.BatchInfo` or integer that "
                            "represents the index of the batch within the epoch if `batch=True`. "
                            "Got a callable that does not accept arguments instead."))
            elif not batch:
                what = "an iterable" if source_desc.kind == _SourceKind.ITERABLE else "a generator function"
                raise TypeError("Parallel external source with {} must be run in a batch mode "
                        "(specify `batch=True` in the external source definition and make sure "
                        "your source returns batches)".format(what))
        else:
            if prefetch_queue_depth is not None:
                raise ValueError("The argument `prefetch_queue_depth` is valid only for " +
                    "parallel external sources (when ``parallel`` is True).")

        if self._layout is not None:
            if layout is not None:
                raise RuntimeError("``layout`` already specified in constructor.")
            else:
                layout = self._layout

        if self._dtype is not None:
            if dtype is not None:
                raise RuntimeError("``dtype`` already specified in constructor.")
            else:
                dtype = self._dtype

        if self._cuda_stream is not None:
            if cuda_stream is not None:
                raise RuntimeError("``cuda_stream`` already specified in constructor.")
            else:
                cuda_stream = self._cuda_stream

        if self._use_copy_kernel is not None:
            if use_copy_kernel is not None:
                raise RuntimeError("``use_copy_kernel`` already specified in constructor.")
            else:
                use_copy_kernel = self._use_copy_kernel

        if name is None:
            name = self._name
        else:
            self._name = name

        if name is not None and self._num_outputs is not None:
            raise RuntimeError("``num_outputs`` is not compatible with named ``ExternalSource``.")

        group_common_kwargs = {
            'cuda_stream': cuda_stream,
            'use_copy_kernel': use_copy_kernel,
            'batch': batch,
            'batch_info': batch_info,
            'parallel': parallel,
            'prefetch_queue_depth': prefetch_queue_depth,
        }

        if self._num_outputs is not None:
            outputs = []
            kwargs = {"no_copy": no_copy}
            group = _ExternalSourceGroup(callback, source_desc, True, **group_common_kwargs)
            for i in range(self._num_outputs):
                if dtype is not None:
                    if isinstance(dtype, (list, tuple)):
                        kwargs['dtype'] = dtype[i] if i < len(dtype) else nvidia.dali.types.DALIDataType.NO_TYPE
                    else:
                        kwargs['dtype'] = dtype
                op_instance = _OperatorInstance([], self, **kwargs)
                op_instance._callback = callback
                op_instance._output_index = i
                op_instance._group = group
                if layout is not None:
                    if isinstance(layout, (list, tuple)):
                        op_instance._layout = layout[i] if i < len(layout) else ""
                    else:
                        op_instance._layout = layout
                else:
                    op_instance._layout = None

                op_instance._batch = batch

                group.append(op_instance)
                op_instance.generate_outputs()
                outputs.append(op_instance.unwrapped_outputs)

            return outputs
        else:
            if name is not None:
                kwargs["name"] = name
            if no_copy is not None:
                kwargs["no_copy"] = no_copy
            if dtype is not None:
                kwargs['dtype'] = dtype
            op_instance = _OperatorInstance([], self, **kwargs)
            op_instance._callback = callback
            op_instance._output_index = None
            op_instance._group = _ExternalSourceGroup(
                callback, source_desc, False, [op_instance], **group_common_kwargs)
            op_instance._layout = layout
            op_instance._batch = batch
            op_instance.generate_outputs()

            return op_instance.unwrapped_outputs