hankcs/HanLP · error · ValueError

Unrecognized devices {devices}

Error message

Unrecognized devices {devices}

What it means

fit_dataloader on Word2VecEmbedding always raises NotImplementedError; static pretrained embeddings have no parameters to optimize over dataloader batches, so single-batch training is unsupported.

Source

Thrown at hanlp/common/torch_component.py:563

                        except StopIteration:
                            continue
                        if on_device == device:
                            continue
                        if isinstance(device, int):
                            if on_device.index == device:
                                continue
                        if re.match(regex, name):
                            if not name:
                                name = '*'
                            flash(f'Moving module [yellow]{name}[/yellow] to [on_yellow][magenta][bold]{device}'
                                  f'[/bold][/magenta][/on_yellow]: [red]{regex}[/red]\n')
                            module.to(device)
            elif isinstance(devices, torch.device):
                if verbose:
                    flash(f'Moving model to {devices} [blink][yellow]...[/yellow][/blink]')
                self.model = self.model.to(devices)
            else:
                raise ValueError(f'Unrecognized devices {devices}')
            if verbose:
                flash('')
        else:
            if logger:
                logger.info('Using [red]CPU[/red]')

    def parallelize(self, devices: List[Union[int, torch.device]]):
        return nn.DataParallel(self.model, device_ids=devices)

    @property
    def devices(self):
        """The devices this component lives on.
        """
        if self.model is None:
            return None
        # next(parser.model.parameters()).device
        if hasattr(self.model, 'device_ids'):
            return self.model.device_ids

View on GitHub (pinned to ddb1299bdd)

Solutions

  1. Use the embedding only for forward lookups; rebuild the embedding if the vocab changed (it re-loads/re-maps vectors on load_vocabs)
  2. Train a task component instead
  3. Subclass to implement custom behavior
Defensive patterns

Strategy: type-guard

Validate before calling

assert hasattr(model, 'build_optimizer') and not isinstance(model, Word2VecEmbedding)

Type guard

def is_trainable(m): return not isinstance(m, Word2VecEmbedding)

Try / catch

try:
    model.fit_dataloader(trn, criterion, optimizer, metric, logger)
except NotImplementedError:
    logger.warning('fit_dataloader unsupported; skipping component')

Prevention

When it happens

Trigger: Calling fit_dataloader (or fit with an existing dataloader) on a Word2VecEmbedding instance.

Common situations: Custom training loops that iterate components and call fit_dataloader generically; attempts to adapt embeddings to new vocabulary via training.

Related errors


AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27). Data as JSON: /api/errors/c1a8ce058f30f8e3. Report an issue: GitHub.