Skip to content

Additoinal losses - #336

Merged
awni merged 106 commits into
ml-explore:mainfrom
JayYun98:additoinal_losses
Jan 8, 2024
Merged

awni merged 106 commits into
ml-explore:mainfrom
JayYun98:additoinal_losses

Conversation

@JayYun98

@JayYun98 JayYun98 commented Jan 1, 2024

Copy link
Copy Markdown
Contributor

Proposed changes

Added commonly used losses with tests

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 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 contribution!!

We already have a hinge and huber loss already. The focal, dice, and contrastive are in #324. Please remove all of them and rebase to resolve conflicts.

If you want to add the cosine loss in this PR that would be great.

@JayYun98

JayYun98 commented Jan 2, 2024

Copy link
Copy Markdown
Contributor Author

Hi @awni , rebased and make cos loss to remain on this pr.

Also commented on #324 based on my implementation.

Thanks :)

@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! I left a couple comments, please address then let me know when ready for re-review.

Comment thread python/tests/test_nn.py Outdated

if __name__ == "__main__":
unittest.main()

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.

Nit: can you remove that extra line

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updated :)

@awni

awni commented Jan 3, 2024

Copy link
Copy Markdown
Member

Hey @Jyun1998 sorry but somehow my main comment did not get included. I must have not pushed the save button by accident.

Basically I'm wondering what reference you use for this loss as it looks quite different than the similarly named cosine similarity loss in PyTorch ?

For example there is a target and a margin both of which I do not expect. Unless there is a good reason for the difference, I would suggest we follow the PyTorch implementation as a reference.

@awni

awni commented Jan 5, 2024

Copy link
Copy Markdown
Member

@Jyun1998 are you still planning to follow up on this?

@JayYun98

JayYun98 commented Jan 7, 2024

Copy link
Copy Markdown
Contributor Author

@Jyun1998 are you still planning to follow up on this?

https://github.com/keras-team/keras/blob/v2.14.0/keras/losses.py#L1162-L1236

Hi awni, according to common tensorflow and pytorch implementaiton, the functions are composed of doing l2 norm to each embedding and returns the negative of dot product of both embedding.

My codes also do so, and margin-based loss is applying if it's necessary :)

@awni

awni commented Jan 7, 2024

Copy link
Copy Markdown
Member

@Jyun1998 got it. We should keep it simple until we see that we need more features. Could you follow the PyTorch cosine similarity loss? I think that one covers the most common case (margin / targets are are niche and possibly never needed so we don't want to add them to the API until we are sure they are necessary)

@JayYun98

JayYun98 commented Jan 7, 2024 •

Copy link
Copy Markdown
Contributor Author

@Jyun1998 got it. We should keep it simple until we see that we need more features. Could you follow the PyTorch cosine similarity loss? I think that one covers the most common case (margin / targets are are niche and possibly never needed so we don't want to add them to the API until we are sure they are necessary)

I also agree. Even though there's also margin for pytorch F.cosine_similarity, it defaultly do not use it.

There's only slight change needed for changes and I will test the function and commit asap. Thanks for the review

@awni

awni commented Jan 7, 2024

Copy link
Copy Markdown
Member

Even though there's also margin for pytorch F.cosine_similarity

I don't see the margin in the docs? Is it in the source code?

Screenshot 2024-01-07 at 6 30 32 AM

@JayYun98

JayYun98 commented Jan 7, 2024

Copy link
Copy Markdown
Contributor Author

Even though there's also margin for pytorch F.cosine_similarity

I don't see the margin in the docs? Is it in the source code?

Screenshot 2024-01-07 at 6 30 32 AM

https://pytorch.org/docs/stable/generated/torch.nn.CosineEmbeddingLoss.html#torch.nn.CosineEmbeddingLoss

@awni

awni commented Jan 7, 2024

Copy link
Copy Markdown
Member

I see, thanks. Yes let's go with the plain cosine similarity for now. Thank you!

@awni

awni commented Jan 7, 2024

Copy link
Copy Markdown
Member

Also could you rebase and resolve conflicts?

@JayYun98

JayYun98 commented Jan 7, 2024 •

Copy link
Copy Markdown
Contributor Author

Also could you rebase and resolve conflicts?

Am I correct that losses test codes are gone?


nvm found new losses test file

JayYun98 and others added 3 commits January 8, 2024 00:24
angeloskath and others added 16 commits January 8, 2024 00:28
* Added scale for RoPE

* Ran pre-commit

* Added RoPE scaling test

* Added docstring for scale parameter

* Modified docstrings
* feat: Add Dropout3d layer to nn.layers

* acknowledgement

* Add dropout tests to test_nn.py

* run pre-commit

* Add activation functions and dropout3d ops

* Add dropout tests for bfloat16 and float16
* spelling: accumulates

Signed-off-by: Josh Soref <[email protected]>

* spelling: across

Signed-off-by: Josh Soref <[email protected]>

* spelling: additional

Signed-off-by: Josh Soref <[email protected]>

* spelling: against

Signed-off-by: Josh Soref <[email protected]>

* spelling: among

Signed-off-by: Josh Soref <[email protected]>

* spelling: array

Signed-off-by: Josh Soref <[email protected]>

* spelling: at least

Signed-off-by: Josh Soref <[email protected]>

* spelling: available

Signed-off-by: Josh Soref <[email protected]>

* spelling: axes

Signed-off-by: Josh Soref <[email protected]>

* spelling: basically

Signed-off-by: Josh Soref <[email protected]>

* spelling: bfloat

Signed-off-by: Josh Soref <[email protected]>

* spelling: bounds

Signed-off-by: Josh Soref <[email protected]>

* spelling: broadcast

Signed-off-by: Josh Soref <[email protected]>

* spelling: buffer

Signed-off-by: Josh Soref <[email protected]>

* spelling: class

Signed-off-by: Josh Soref <[email protected]>

* spelling: coefficients

Signed-off-by: Josh Soref <[email protected]>

* spelling: collision

Signed-off-by: Josh Soref <[email protected]>

* spelling: combinations

Signed-off-by: Josh Soref <[email protected]>

* spelling: committing

Signed-off-by: Josh Soref <[email protected]>

* spelling: computation

Signed-off-by: Josh Soref <[email protected]>

* spelling: consider

Signed-off-by: Josh Soref <[email protected]>

* spelling: constructing

Signed-off-by: Josh Soref <[email protected]>

* spelling: conversions

Signed-off-by: Josh Soref <[email protected]>

* spelling: correctly

Signed-off-by: Josh Soref <[email protected]>

* spelling: corresponding

Signed-off-by: Josh Soref <[email protected]>

* spelling: declaration

Signed-off-by: Josh Soref <[email protected]>

* spelling: default

Signed-off-by: Josh Soref <[email protected]>

* spelling: dependency

Signed-off-by: Josh Soref <[email protected]>

* spelling: destination

Signed-off-by: Josh Soref <[email protected]>

* spelling: destructor

Signed-off-by: Josh Soref <[email protected]>

* spelling: dimensions

Signed-off-by: Josh Soref <[email protected]>

* spelling: divided

Signed-off-by: Josh Soref <[email protected]>

* spelling: element-wise

Signed-off-by: Josh Soref <[email protected]>

* spelling: elements

Signed-off-by: Josh Soref <[email protected]>

* spelling: endianness

Signed-off-by: Josh Soref <[email protected]>

* spelling: equivalent

Signed-off-by: Josh Soref <[email protected]>

* spelling: explicitly

Signed-off-by: Josh Soref <[email protected]>

* spelling: github

Signed-off-by: Josh Soref <[email protected]>

* spelling: indices

Signed-off-by: Josh Soref <[email protected]>

* spelling: irregularly

Signed-off-by: Josh Soref <[email protected]>

* spelling: memory

Signed-off-by: Josh Soref <[email protected]>

* spelling: metallib

Signed-off-by: Josh Soref <[email protected]>

* spelling: negative

Signed-off-by: Josh Soref <[email protected]>

* spelling: notable

Signed-off-by: Josh Soref <[email protected]>

* spelling: optional

Signed-off-by: Josh Soref <[email protected]>

* spelling: otherwise

Signed-off-by: Josh Soref <[email protected]>

* spelling: overridden

Signed-off-by: Josh Soref <[email protected]>

* spelling: partially

Signed-off-by: Josh Soref <[email protected]>

* spelling: partition

Signed-off-by: Josh Soref <[email protected]>

* spelling: perform

Signed-off-by: Josh Soref <[email protected]>

* spelling: perturbations

Signed-off-by: Josh Soref <[email protected]>

* spelling: positively

Signed-off-by: Josh Soref <[email protected]>

* spelling: primitive

Signed-off-by: Josh Soref <[email protected]>

* spelling: repeat

Signed-off-by: Josh Soref <[email protected]>

* spelling: repeats

Signed-off-by: Josh Soref <[email protected]>

* spelling: respect

Signed-off-by: Josh Soref <[email protected]>

* spelling: respectively

Signed-off-by: Josh Soref <[email protected]>

* spelling: result

Signed-off-by: Josh Soref <[email protected]>

* spelling: rounding

Signed-off-by: Josh Soref <[email protected]>

* spelling: separate

Signed-off-by: Josh Soref <[email protected]>

* spelling: skipping

Signed-off-by: Josh Soref <[email protected]>

* spelling: structure

Signed-off-by: Josh Soref <[email protected]>

* spelling: the

Signed-off-by: Josh Soref <[email protected]>

* spelling: transpose

Signed-off-by: Josh Soref <[email protected]>

* spelling: unnecessary

Signed-off-by: Josh Soref <[email protected]>

* spelling: unneeded

Signed-off-by: Josh Soref <[email protected]>

* spelling: unsupported

Signed-off-by: Josh Soref <[email protected]>

---------

Signed-off-by: Josh Soref <[email protected]>
add cos sim
cos sim test
@JayYun98

JayYun98 commented Jan 7, 2024

Copy link
Copy Markdown
Contributor Author

Also could you rebase and resolve conflicts?

Could you check? Thanks :)

@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.

A few more changes please.

Comment thread python/mlx/nn/losses.py Resolved
Comment thread python/mlx/nn/losses.py Resolved
Comment thread python/mlx/nn/losses.py Resolved
Comment thread python/mlx/nn/losses.py Resolved
Comment thread python/mlx/nn/losses.py Resolved
Comment thread python/mlx/nn/losses.py Resolved
Comment thread python/tests/test_losses.py Resolved

@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 the contribution @Jyun1998 !

@awni
awni merged commit 0b8aedd into ml-explore:main Jan 8, 2024
@awni awni mentioned this pull request Jan 23, 2024
4 tasks done
zcbenz pushed a commit to Ritabanm/mlx that referenced this pull request Sep 30, 2026
feat: `ARRAY_API_TESTS_MODULE` for runtime-defined xp
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.