# Using huggingface generate() with custom model

**URL:** <https://discuss.huggingface.co/t/using-huggingface-generate-with-custom-model/52251>\
**Category:** 🤗Transformers\
**Created:** [August 26, 2023, 10:40am UTC](https://discuss.huggingface.co/t/using-huggingface-generate-with-custom-model/52251 "2023-08-26T10:40:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThangLD201](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@ThangLD201](https://discuss.huggingface.co/u/ThangLD201)\
**Post date:** [August 26, 2023, 10:40am UTC](https://discuss.huggingface.co/t/using-huggingface-generate-with-custom-model/52251/1 "2023-08-26T10:40:40Z")

</div>

Hi there, I’m wondering if there is an elegant way to use huggingface’s generate() function given a customized model.

Currently, I’m using the MBartForConditionalGeneration class and I want to change how the inputs are processed inside the forward function e.g. adding stuffs such as variational inference from a prior distribution. I intend to subclass the MBartForConditionalGeneration with additional properties and methods to accomplish this. Is this the right way or is there a better approach out there ?

Thanks in advance !

---

<div class="post-metadata">

**Author:** ![dblakely](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@dblakely](https://discuss.huggingface.co/u/dblakely)\
**Post date:** [August 26, 2023, 11:52am UTC](https://discuss.huggingface.co/t/using-huggingface-generate-with-custom-model/52251/2 "2023-08-26T11:52:54Z")

</div>

If your custom class ultimately just changes how the logits are computed, then I think what you’ve said is the right approach. You’ll just need to make sure the forward method’s outputs match the same format as the normal model.

On the other hand, if you need a custom sampling strategy, you’ll likely also need to override and modify the [sample](https://github.com/huggingface/transformers/blob/960807f62e53676723ab8281019219864ef3db4d/src/transformers/generation/utils.py#L2541) method or create a custom `LogitsWarper`.
