# Buggy code
def from_config(
    cls,
    config: Union[Dict[str, Any], Config] = {},
    *,
    vocab: Union[Vocab, bool] = True,
    disable: Iterable[str] = SimpleFrozenList(),
    exclude: Iterable[str] = SimpleFrozenList(),
    meta: Dict[str, Any] = SimpleFrozenDict(),
    auto_fill: bool = True,
    validate: bool = True,
) -> "Language":
    """Create the nlp object from a loaded config. Will set up the tokenizer
    and language data, add pipeline components etc. If no config is provided,
    the default config of the given language is used.

    config (Dict[str, Any] / Config): The loaded config.
    vocab (Vocab): A Vocab object. If True, a vocab is created.
    disable (Iterable[str]): Names of pipeline components to disable.
        Disabled pipes will be loaded but they won't be run unless you
        explicitly enable them by calling nlp.enable_pipe.
    exclude (Iterable[str]): Names of pipeline components to exclude.
        Excluded components won't be loaded.
    meta (Dict[str, Any]): Meta overrides for nlp.meta.
    auto_fill (bool): Automatically fill in missing values in config based
        on defaults and function argument annotations.
    validate (bool): Validate the component config and arguments against
        the types expected by the factory.
    RETURNS (Language): The initialized Language class.

    DOCS: https://spacy.io/api/language#from_config
    """
    if auto_fill:
        config = Config(cls.default_config, section_order=CONFIG_SECTION_ORDER).merge(
            config
        )
    if "nlp" not in config:
        raise ValueError(Errors.E985.format(config=config))
    config_lang = config["nlp"].get("lang")
    if config_lang is not None and config_lang != cls.lang:
        raise ValueError(
            Errors.E958.format(
                bad_lang_code=config["nlp"]["lang"],
                lang_code=cls.lang,
                lang=util.get_object_name(cls),
            )
        )
    config["nlp"]["lang"] = cls.lang
    # This isn't very elegant, but we remove the [components] block here to prevent
    # it from getting resolved (causes problems because we expect to pass in
    # the nlp and name args for each component). If we're auto-filling, we're
    # using the nlp.config with all defaults.
    config = util.copy_config(config)
    orig_pipeline = config.pop("components", {})
    orig_pretraining = config.pop("pretraining", None)
    config["components"] = {}
    if auto_fill:
        filled = registry.fill(config, validate=validate, schema=ConfigSchema)
    else:
        filled = config
    filled["components"] = orig_pipeline
    config["components"] = orig_pipeline
    if orig_pretraining is not None:
        filled["pretraining"] = orig_pretraining
        config["pretraining"] = orig_pretraining
    resolved_nlp = registry.resolve(
        filled["nlp"], validate=validate, schema=ConfigSchemaNlp
    )
    create_tokenizer = resolved_nlp["tokenizer"]
    before_creation = resolved_nlp["before_creation"]
    after_creation = resolved_nlp["after_creation"]
    after_pipeline_creation = resolved_nlp["after_pipeline_creation"]
    lang_cls = cls
    if before_creation is not None:
        lang_cls = before_creation(cls)
        if (
            not isinstance(lang_cls, type)
            or not issubclass(lang_cls, cls)
            or lang_cls is not cls
        ):
            raise ValueError(Errors.E943.format(value=type(lang_cls)))
    # Note that we don't load vectors here, instead they get loaded explicitly
    # inside stuff like the spacy train function. If we loaded them here,
    # then we would load them twice at runtime: once when we make from config,
    # and then again when we load from disk.
    nlp = lang_cls(vocab=vocab, create_tokenizer=create_tokenizer, meta=meta)
    if after_creation is not None:
        nlp = after_creation(nlp)
        if not isinstance(nlp, cls):
            raise ValueError(Errors.E942.format(name="creation", value=type(nlp)))
    # To create the components we need to use the final interpolated config
    # so all values are available (if component configs use variables).
    # Later we replace the component config with the raw config again.
    interpolated = filled.interpolate() if not filled.is_interpolated else filled
    pipeline = interpolated.get("components", {})
    sourced = util.get_sourced_components(interpolated)
    # If components are loaded from a source (existing models), we cache
    # them here so they're only loaded once
    source_nlps = {}
    for pipe_name in config["nlp"]["pipeline"]:
        if pipe_name not in pipeline:
            opts = ", ".join(pipeline.keys())
            raise ValueError(Errors.E956.format(name=pipe_name, opts=opts))
        pipe_cfg = util.copy_config(pipeline[pipe_name])
        raw_config = Config(filled["components"][pipe_name])
        if pipe_name not in exclude:
            if "factory" not in pipe_cfg and "source" not in pipe_cfg:
                err = Errors.E984.format(name=pipe_name, config=pipe_cfg)
                raise ValueError(err)
            if "factory" in pipe_cfg:
                factory = pipe_cfg.pop("factory")
                # The pipe name (key in the config) here is the unique name
                # of the component, not necessarily the factory
                nlp.add_pipe(
                    factory,
                    name=pipe_name,
                    config=pipe_cfg,
                    validate=validate,
                    raw_config=raw_config,
                )
            else:
                model = pipe_cfg["source"]
                if model not in source_nlps:
                    # We only need the components here and we need to init
                    # model with the same vocab as the current nlp object
                    source_nlps[model] = util.load_model(model, vocab=nlp.vocab)
                source_name = pipe_cfg.get("component", pipe_name)
                nlp.add_pipe(source_name, source=source_nlps[model], name=pipe_name)
    disabled_pipes = [*config["nlp"]["disabled"], *disable]
    nlp._disabled = set(p for p in disabled_pipes if p not in exclude)
    nlp.batch_size = config["nlp"]["batch_size"]
    nlp.config = filled if auto_fill else config
    if after_pipeline_creation is not None:
        nlp = after_pipeline_creation(nlp)
        if not isinstance(nlp, cls):
            raise ValueError(
                Errors.E942.format(name="pipeline_creation", value=type(nlp))
            )
    # Detect components with listeners that are not frozen consistently
    for name, proc in nlp.pipeline:
        if getattr(proc, "listening_components", None):  # e.g. tok2vec/transformer
            for listener in proc.listening_components:
                # If it's a component sourced from another pipeline, we check if
                # the tok2vec listeners should be replaced with standalone tok2vec
                # models (e.g. so component can be frozen without its performance
                # degrading when other components/tok2vec are updated)
                paths = sourced.get(listener, {}).get("replace_listeners", [])
                if paths:
                    nlp.replace_listeners(name, listener, paths)
    return nlp


# Traceback (TypeError)
TypeError                                 Traceback (most recent call last)
<ipython-input-12-66e94dc9d1fd> in <module>
1 sent = 'Hello World'
----> 2 doc = nlp(sent)


~/anaconda3/envs/acl/lib/python3.8/site-packages/spacy/language.py in __call__(self, text, disable, component_cfg)
992                 raise ValueError(Errors.E109.format(name=name)) from e
993             except Exception as e:
--> 994                 error_handler(name, proc, [doc], e)
995             if doc is None:
996                 raise ValueError(Errors.E005.format(name=name))

~/anaconda3/envs/acl/lib/python3.8/site-packages/spacy/util.py in raise_error(proc_name, proc, docs, e)
1493
1494 def raise_error(proc_name, proc, docs, e):
-> 1495     raise e
1496
1497

~/anaconda3/envs/acl/lib/python3.8/site-packages/spacy/language.py in __call__(self, text, disable, component_cfg)
987                 error_handler = proc.get_error_handler()
988             try:
--> 989                 doc = proc(doc, **component_cfg.get(name, {}))
990             except KeyError as e:
991                 # This typically happens if a component is not initialized

~/anaconda3/envs/acl/lib/python3.8/site-packages/spacy/pipeline/trainable_pipe.pyx in spacy.pipeline.trainable_pipe.TrainablePipe.__call__()

~/anaconda3/envs/acl/lib/python3.8/site-packages/spacy/util.py in raise_error(proc_name, proc, docs, e)
1493
1494 def raise_error(proc_name, proc, docs, e):
-> 1495     raise e
1496
1497

~/anaconda3/envs/acl/lib/python3.8/site-packages/spacy/pipeline/trainable_pipe.pyx in spacy.pipeline.trainable_pipe.TrainablePipe.__call__()

~/anaconda3/envs/acl/lib/python3.8/site-packages/spacy/pipeline/tagger.pyx in spacy.pipeline.tagger.Tagger.predict()

~/anaconda3/envs/acl/lib/python3.8/site-packages/thinc/model.py in predict(self, X)
310         only the output, instead of the `(output, callback)` tuple.
311         """
--> 312         return self._func(self, X, is_train=False)[0]
313
314     def finish_update(self, optimizer: Optimizer) -> None:

~/anaconda3/envs/acl/lib/python3.8/site-packages/thinc/layers/chain.py in forward(model, X, is_train)
52     callbacks = []
53     for layer in model.layers:
---> 54         Y, inc_layer_grad = layer(X, is_train=is_train)
55         callbacks.append(inc_layer_grad)
56         X = Y

~/anaconda3/envs/acl/lib/python3.8/site-packages/thinc/model.py in __call__(self, X, is_train)
286         """Call the model's `forward` function, returning the output and a
287         callback to compute the gradients via backpropagation."""
--> 288         return self._func(self, X, is_train=is_train)
289
290     def initialize(self, X: Optional[InT] = None, Y: Optional[OutT] = None) -> "Model":

~/anaconda3/envs/acl/lib/python3.8/site-packages/thinc/layers/chain.py in forward(model, X, is_train)
52     callbacks = []
53     for layer in model.layers:
---> 54         Y, inc_layer_grad = layer(X, is_train=is_train)
55         callbacks.append(inc_layer_grad)
56         X = Y

~/anaconda3/envs/acl/lib/python3.8/site-packages/thinc/model.py in __call__(self, X, is_train)
286         """Call the model's `forward` function, returning the output and a
287         callback to compute the gradients via backpropagation."""
--> 288         return self._func(self, X, is_train=is_train)
289
290     def initialize(self, X: Optional[InT] = None, Y: Optional[OutT] = None) -> "Model":

~/anaconda3/envs/acl/lib/python3.8/site-packages/spacy_transformers/layers/trfs2arrays.py in forward(model, trf_datas, is_train)
26             src = model.ops.reshape2f(trf_data.tensors[t_i], -1, trf_data.width)
27             dst, get_d_src = apply_alignment(model.ops, trf_data.align, src)
---> 28             output, get_d_dst = pooling(dst, is_train)
29             outputs.append(output)
30             backprops.append((get_d_dst, get_d_src))

~/anaconda3/envs/acl/lib/python3.8/site-packages/thinc/model.py in __call__(self, X, is_train)
286         """Call the model's `forward` function, returning the output and a
287         callback to compute the gradients via backpropagation."""
--> 288         return self._func(self, X, is_train=is_train)
289
290     def initialize(self, X: Optional[InT] = None, Y: Optional[OutT] = None) -> "Model":

~/anaconda3/envs/acl/lib/python3.8/site-packages/thinc/layers/reduce_mean.py in forward(model, Xr, is_train)
16
17 def forward(model: Model[InT, OutT], Xr: InT, is_train: bool) -> Tuple[OutT, Callable]:
---> 18     Y = model.ops.reduce_mean(cast(Floats2d, Xr.data), Xr.lengths)
19     lengths = Xr.lengths
20

~/anaconda3/envs/acl/lib/python3.8/site-packages/thinc/backends/numpy_ops.pyx in thinc.backends.numpy_ops.NumpyOps.reduce_mean()

~/anaconda3/envs/acl/lib/python3.8/site-packages/thinc/backends/numpy_ops.cpython-38-x86_64-linux-gnu.so in View.MemoryView.memoryview_cwrapper()

~/anaconda3/envs/acl/lib/python3.8/site-packages/thinc/backends/numpy_ops.cpython-38-x86_64-linux-gnu.so in View.MemoryView.memoryview.__cinit__()

TypeError: a bytes-like object is required, not 'cupy.core.core.ndarray'

# Fixed code
def from_config(
    cls,
    config: Union[Dict[str, Any], Config] = {},
    *,
    vocab: Union[Vocab, bool] = True,
    disable: Iterable[str] = SimpleFrozenList(),
    exclude: Iterable[str] = SimpleFrozenList(),
    meta: Dict[str, Any] = SimpleFrozenDict(),
    auto_fill: bool = True,
    validate: bool = True,
) -> "Language":
    """Create the nlp object from a loaded config. Will set up the tokenizer
    and language data, add pipeline components etc. If no config is provided,
    the default config of the given language is used.

    config (Dict[str, Any] / Config): The loaded config.
    vocab (Vocab): A Vocab object. If True, a vocab is created.
    disable (Iterable[str]): Names of pipeline components to disable.
        Disabled pipes will be loaded but they won't be run unless you
        explicitly enable them by calling nlp.enable_pipe.
    exclude (Iterable[str]): Names of pipeline components to exclude.
        Excluded components won't be loaded.
    meta (Dict[str, Any]): Meta overrides for nlp.meta.
    auto_fill (bool): Automatically fill in missing values in config based
        on defaults and function argument annotations.
    validate (bool): Validate the component config and arguments against
        the types expected by the factory.
    RETURNS (Language): The initialized Language class.

    DOCS: https://spacy.io/api/language#from_config
    """
    if auto_fill:
        config = Config(cls.default_config, section_order=CONFIG_SECTION_ORDER).merge(
            config
        )
    if "nlp" not in config:
        raise ValueError(Errors.E985.format(config=config))
    config_lang = config["nlp"].get("lang")
    if config_lang is not None and config_lang != cls.lang:
        raise ValueError(
            Errors.E958.format(
                bad_lang_code=config["nlp"]["lang"],
                lang_code=cls.lang,
                lang=util.get_object_name(cls),
            )
        )
    config["nlp"]["lang"] = cls.lang
    # This isn't very elegant, but we remove the [components] block here to prevent
    # it from getting resolved (causes problems because we expect to pass in
    # the nlp and name args for each component). If we're auto-filling, we're
    # using the nlp.config with all defaults.
    config = util.copy_config(config)
    orig_pipeline = config.pop("components", {})
    orig_pretraining = config.pop("pretraining", None)
    config["components"] = {}
    if auto_fill:
        filled = registry.fill(config, validate=validate, schema=ConfigSchema)
    else:
        filled = config
    filled["components"] = orig_pipeline
    config["components"] = orig_pipeline
    if orig_pretraining is not None:
        filled["pretraining"] = orig_pretraining
        config["pretraining"] = orig_pretraining
    resolved_nlp = registry.resolve(
        filled["nlp"], validate=validate, schema=ConfigSchemaNlp
    )
    create_tokenizer = resolved_nlp["tokenizer"]
    before_creation = resolved_nlp["before_creation"]
    after_creation = resolved_nlp["after_creation"]
    after_pipeline_creation = resolved_nlp["after_pipeline_creation"]
    lang_cls = cls
    if before_creation is not None:
        lang_cls = before_creation(cls)
        if (
            not isinstance(lang_cls, type)
            or not issubclass(lang_cls, cls)
            or lang_cls is not cls
        ):
            raise ValueError(Errors.E943.format(value=type(lang_cls)))

    # Warn about require_gpu usage in jupyter notebook
    warn_if_jupyter_cupy()

    # Note that we don't load vectors here, instead they get loaded explicitly
    # inside stuff like the spacy train function. If we loaded them here,
    # then we would load them twice at runtime: once when we make from config,
    # and then again when we load from disk.
    nlp = lang_cls(vocab=vocab, create_tokenizer=create_tokenizer, meta=meta)
    if after_creation is not None:
        nlp = after_creation(nlp)
        if not isinstance(nlp, cls):
            raise ValueError(Errors.E942.format(name="creation", value=type(nlp)))
    # To create the components we need to use the final interpolated config
    # so all values are available (if component configs use variables).
    # Later we replace the component config with the raw config again.
    interpolated = filled.interpolate() if not filled.is_interpolated else filled
    pipeline = interpolated.get("components", {})
    sourced = util.get_sourced_components(interpolated)
    # If components are loaded from a source (existing models), we cache
    # them here so they're only loaded once
    source_nlps = {}
    for pipe_name in config["nlp"]["pipeline"]:
        if pipe_name not in pipeline:
            opts = ", ".join(pipeline.keys())
            raise ValueError(Errors.E956.format(name=pipe_name, opts=opts))
        pipe_cfg = util.copy_config(pipeline[pipe_name])
        raw_config = Config(filled["components"][pipe_name])
        if pipe_name not in exclude:
            if "factory" not in pipe_cfg and "source" not in pipe_cfg:
                err = Errors.E984.format(name=pipe_name, config=pipe_cfg)
                raise ValueError(err)
            if "factory" in pipe_cfg:
                factory = pipe_cfg.pop("factory")
                # The pipe name (key in the config) here is the unique name
                # of the component, not necessarily the factory
                nlp.add_pipe(
                    factory,
                    name=pipe_name,
                    config=pipe_cfg,
                    validate=validate,
                    raw_config=raw_config,
                )
            else:
                model = pipe_cfg["source"]
                if model not in source_nlps:
                    # We only need the components here and we need to init
                    # model with the same vocab as the current nlp object
                    source_nlps[model] = util.load_model(model, vocab=nlp.vocab)
                source_name = pipe_cfg.get("component", pipe_name)
                nlp.add_pipe(source_name, source=source_nlps[model], name=pipe_name)
    disabled_pipes = [*config["nlp"]["disabled"], *disable]
    nlp._disabled = set(p for p in disabled_pipes if p not in exclude)
    nlp.batch_size = config["nlp"]["batch_size"]
    nlp.config = filled if auto_fill else config
    if after_pipeline_creation is not None:
        nlp = after_pipeline_creation(nlp)
        if not isinstance(nlp, cls):
            raise ValueError(
                Errors.E942.format(name="pipeline_creation", value=type(nlp))
            )
    # Detect components with listeners that are not frozen consistently
    for name, proc in nlp.pipeline:
        if getattr(proc, "listening_components", None):  # e.g. tok2vec/transformer
            for listener in proc.listening_components:
                # If it's a component sourced from another pipeline, we check if
                # the tok2vec listeners should be replaced with standalone tok2vec
                # models (e.g. so component can be frozen without its performance
                # degrading when other components/tok2vec are updated)
                paths = sourced.get(listener, {}).get("replace_listeners", [])
                if paths:
                    nlp.replace_listeners(name, listener, paths)
    return nlp