Skip to content

Transformer fix - #167

Merged
awni merged 10 commits into
ml-explore:mainfrom
JayYun98:transformer_fix
Dec 27, 2023
Merged

awni merged 10 commits into
ml-explore:mainfrom
JayYun98:transformer_fix

Conversation

@JayYun98

@JayYun98 JayYun98 commented Dec 14, 2023 •

Copy link
Copy Markdown
Contributor

Proposed changes

Changes

  • Changed order of layernorm and ffn according to tensorflow and pytorch implementations. (norm_first=False is default in pytroch as well as transformer)

  • Added Encoder, Decoder with Dropout

Parts want to discuss

  • Do you want to use Transformer with Dropout as main class?

  • Do you like to add ffn as function or classes?

  • Do you like to add norm_first parameter so that you could choose to do layer_norm first or later?

  • Tensorflow do not do dropout with ffn while pytorch do. Which one should I refer to?

  • I guess you did not add dropout since you would like to benchmark transformer without dropout for all jax, torch, tensorflow, mlx. I also had trouble adding dropout for jax.

Testing

  • All Encoder, Decoder (w and w/o dropout) are tested with mlx-exmaples/transformer_lm

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

@awni

awni commented Dec 16, 2023

Copy link
Copy Markdown
Member

Do you want to use Transformer with Dropout as main class?

Let's just make dropout a kwarg parameter to the Transformer* which defaults to 0. Don't need to implement seperate classes.

Do you like to add ffn as function or classes?

Let's keep it the way it is implemented in __call__

Do you like to add norm_first parameter so that you could choose to do layer_norm first or later?

I think we should have this. And maybe default it to True as that seems to be a more common setup?

Tensorflow do not do dropout with ffn while pytorch do. Which one should I refer to?

Seems reasonable to follow PyTorch in that case. Add a comment about it when describing the dropout argument.

@awni awni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! I responded to your questions. Please make the corresponding changes and then we can add this!

@awni

awni commented Dec 16, 2023

Copy link
Copy Markdown
Member

Also do you mind adding a docstring to the Transformer class?

@awni

awni commented Dec 19, 2023

Copy link
Copy Markdown
Member

@Jyun1998 are you still planning on updating this?

@JayYun98

Copy link
Copy Markdown
Contributor Author

@Jyun1998 are you still planning on updating this?

Yes! I will finish it within this week with your comments :)

@JayYun98

Copy link
Copy Markdown
Contributor Author

Hi @awni ! Merry Chirstmas :)

Added docstring, norm_first, and also activation to replace mx.maximum at ffn layer.

There are few points to further discuss.

  • I have setted norm_first=False, which is common according to pytorch_docstring

  • by adding norm_first, codes got way extensive long. according to pytorch_implementation, they further break down encoder/decoer_layer with private function _sa_block, _ff_block. Do you want to keep it lastest commit or use _sa_block, _ff_block?

  • default dropout ratio is 0 where Dropout call is implemented with self._p_1 == 1, so Dropout(0) will do nothing.

@awni awni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, just a couple minor comments!

Comment thread python/mlx/nn/layers/transformer.py
Comment thread python/mlx/nn/layers/transformer.py Outdated
Comment on lines +284 to +286
dropout (float): The dropout value for Transformer encoder/decoder.
activation (Callable[[Any], Any]): the activation function of encoder/decoder intermediate layer
custom_encoder (Optional[Any]): A custom encoder to replace the standard Transformer encoder.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add defaults for these as well in the docstring.

@awni

awni commented Dec 26, 2023

Copy link
Copy Markdown
Member

I have setted norm_first=False, which is common according to pytorch_docstring

Ok with me

by adding norm_first, codes got way extensive long.

I left a comment that might work for shortening this. I don't think you need separate modules for these.

default dropout ratio is 0 where Dropout call is implemented with self._p_1 == 1, so Dropout(0) will do nothing.

👍

@awni

awni commented Dec 26, 2023

Copy link
Copy Markdown
Member

Also could you rebase? There is a merge conflict

@JayYun98

JayYun98 commented Dec 26, 2023 •

Copy link
Copy Markdown
Contributor Author

@awni Thanks for the review!

  • I added defaults to doctring.

  • for the shortening the code, below will do the same but the code become quite not straight forward, so better left it as it is.

def __call__(self, x, mask):
    y = self.ln1(x) if self.norm_first else x
    y = self.attention(y, y, y, mask)
    y = self.dropout1(y)
    x = x + y  
    x = self.ln1(x) if not self.norm_first else x  

    y = self.ln2(x) if self.norm_first else x
    y = self.linear1(y)  
    y = self.activation(y)
    y = self.dropout2(y)
    y = self.linear2(y)
    x = x + y  
    x = self.ln2(x) if not self.norm_first else x  

    return x

@JayYun98

Copy link
Copy Markdown
Contributor Author

Also rebased :)

@awni awni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks for adding!! I pushed a few changes to the doc strings and included stuff in the docs as well!

@awni
awni merged commit 4417e37 into ml-explore:main Dec 27, 2023
@JayYun98

Copy link
Copy Markdown
Contributor Author

Looks great, thanks for adding!! I pushed a few changes to the doc strings and included stuff in the docs as well!

Thanks for the review :)

JayYun98 added a commit to JayYun98/mlx that referenced this pull request Jan 7, 2024
* add transformer with dropout, fix transformer ffm, layernorm order

* precommit changes

* precommit changes

* add docstring, activation, norm_first

* run precommit

* run precommit

* add doctstring

* precommit

* style nits in docs

---------

Co-authored-by: junwoo-yun <junwoo.yun@bagelcode.com>
Co-authored-by: Awni Hannun <awni@apple.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants