# 模型推理时验证model.generate\(\)功能报错RuntimeError A model class needs to define a \`prepare inputs fordgeneration\` method in order to use .generate\(\)\`

**1. 系统环境**

硬件环境\(Ascend/GPU/CPU\): Ascend

软件环境: MindSpore 版本: 2.2

执行模式： 不限

**2. 报错信息**

2.1 问题描述

模型推理时验证model.generate\(\)功能报错。

```
Traceback (most recent call last):   
File "predict dx.py". line 26, in <module> 
result = model.generate(input_ids=inputs["input_ids"], max_length=model.config.seq_length)
File "/home/wizardcoder/1_wiz ardcoder-mindformers7mindformers/generation/text_generator.py", line, in generate**model kwargs,
File "/home/wizardcoder/1 wizardcoder-mindformers/mindformers/generation/text_generator.py", line 329, in _forward     
input ids. **model kwargs
File. "/home/wizardcoder/1 wizardcoder-mindformers /mindformers/generation/text generator.py", line 58, in prepare inputs for generation
"A model class needs to-define a `prepare inputs for aeneration*
RuntimeError A model class needs to define a `prepare inputs fordgeneration` method in order to use .generate()`
```

**3. 根因分析**

分析代码可知（见text\_generator.py第48行），如果需要使用model.generate\(\)功能，必须要重写prepare\_inputs\_for\_generation\(\)方法。

```
class GeneratorMixin:
    """Generator For the nlp models"""
    def __init__(self):
        pass
    # pylint: disable=W0613
    def prepare_inputs_for_generation(self, input_ids, **kwargs):
        """
        prepare inputs for generation.
        A model class needs to define a `prepare_inputs_for_generation` method
        in orderto use `.generate()
        Raises:
            RuntimeError: Not implemented in model but call `.generate()`
        """
        raise RuntimeError(
            "A model class needs to define a `prepare_inputs_for_generation`"
            " method in order to use `.generate()`."
        )
```

**4. 解决方案**

需要在待迁移模型中实现这个功能方法，参照GPT-2的写法，wizardcoder在WizardCoderLMHeadModel类中实现了这个功能。

```
def prepare_inputs_for_generation(self, input_ids, **kwargs):
    input_ids = {"input_ids":Tensor(input_ids, mstype.int32)}
    # input_ids = {"input_ids": input_ids}
 
    Return input_ids
```

