Convnext flax - #21485
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. |
|
@sanchit-gandhi this PR is also ready for your review in case it was missed. And again, thank you so much for taking the time to review the PR. |
|
@sanchit-gandhi Reminder incase my previous message got missed! Also the #21472 ( previous reviews implemented ) and #21867 PR are awaiting review. Thanks! |
sanchit-gandhi
left a comment
There was a problem hiding this comment.
Looks great already @Shubhamai! Think we just need to propagate some of the changes from Flax ResNet / RegNet into this PR, then we're good to go 🚀
| The initial value for the layer scale. | ||
| drop_path_rate (`float`, *optional*, defaults to 0.0): | ||
| The drop rate for stochastic depth. | ||
| image_size (`int`, *optional*, defaults to 224): |
There was a problem hiding this comment.
Think we can just remove this from the docstring (see #21472 (comment))
| dtype: jnp.dtype = jnp.float32 | ||
|
|
||
| @nn.compact | ||
| def __call__(self, hidden_states: jnp.ndarray, train: bool = False) -> jnp.ndarray: |
There was a problem hiding this comment.
We can also swap train for deterministic (see #21472 (comment))
There was a problem hiding this comment.
Ah, missed it there, should be fixed in recent commit
| shape = (hidden_states.shape[0],) + (1,) * ( | ||
| hidden_states.ndim - 1 | ||
| ) # work with diff dim tensors, not just 2D ConvNets | ||
| random_tensor = keep_prob + random.uniform(random.PRNGKey(0), shape, dtype=self.dtype) |
There was a problem hiding this comment.
We probably need to pass a PRNG key here (rather than always starting from seed 0), see
transformers/src/transformers/models/bert/modeling_flax_bert.py
Lines 364 to 366 in 90247d3
There was a problem hiding this comment.
Replaced random.PRNGKey(0) with self.make_rng("dropout")
|
|
||
| def __call__(self, pixel_values: jnp.ndarray) -> jnp.ndarray: | ||
| num_channels = pixel_values.shape[-1] | ||
| if num_channels != self.config.num_channels: |
There was a problem hiding this comment.
Same here regarding the config and num_channels
There was a problem hiding this comment.
I assume you mean removing the if statement and num_channels in the config file. If yes, then the changed should be made in the recent commit
There was a problem hiding this comment.
Sorry we shouldn't remove num_channels from the config as this will break the PyTorch code!
| ) | ||
| return_dict = return_dict if return_dict is not None else self.config.use_return_dict | ||
|
|
||
| if pixel_values is None: |
There was a problem hiding this comment.
This won't play very nicely with JIT since we have control flow on our input (see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#control-flow)
Think we can safely assume here that pixel_values will not be None (otherwise the next line self.embeddings(pixel_values) will complain anyway that pixel_values is None), and so can remove this check.
There was a problem hiding this comment.
Agree, should be removed in the recent commit
| >>> image = Image.open(requests.get(url, stream=True).raw) | ||
|
|
||
| >>> image_processor = AutoImageProcessor.from_pretrained("facebook/convnext-tiny-224") | ||
| >>> model = FlaxConvNextModel.from_pretrained("facebook/convnext-tiny-224") |
There was a problem hiding this comment.
Cool! Have we uploaded the Flax weights already?
There was a problem hiding this comment.
Just made the PR https://huggingface.co/facebook/convnext-tiny-224/discussions/3
| from transformers import AutoFeatureExtractor | ||
|
|
||
|
|
||
| class FlaxConvNextModelTester(unittest.TestCase): |
There was a problem hiding this comment.
Would be awesome to update the tester according to the Flax ResNet model tester :)
|
|
||
| @slow | ||
| def test_inference_image_classification_head(self): | ||
| model = FlaxConvNextForImageClassification.from_pretrained("Shubhamai/convnext-tiny-224") |
There was a problem hiding this comment.
Feel free to open a PR for the Flax weights on the Facebook org!
There was a problem hiding this comment.
|
|
||
| def __init__( | ||
| self, | ||
| num_channels=3, |
There was a problem hiding this comment.
We can't remove config.num_channels since this will break the PyTorch modelling code! https://github.com/huggingface/transformers/blob/40082d598b756c4f1fb048c571ed95c31f05a69c/src/transformers/models/convnext/modeling_convnext.py#LL137C2-L137C2
There was a problem hiding this comment.
Hi, I should have mentioned the review changes weren't fully complete, I am aware of the error due to failed tests, I will fix it asap ( probably on this weekend )
There was a problem hiding this comment.
Cool! Sounds good! Feel free to ping me when it's ready and I'll get you a final review :)
|
|
||
| def __call__(self, pixel_values: jnp.ndarray) -> jnp.ndarray: | ||
| num_channels = pixel_values.shape[-1] | ||
| if num_channels != self.config.num_channels: |
There was a problem hiding this comment.
Sorry we shouldn't remove num_channels from the config as this will break the PyTorch code!
| layer_norm_eps=1e-12, | ||
| layer_scale_init_value=1e-6, | ||
| drop_path_rate=0.0, | ||
| image_size=224, |
There was a problem hiding this comment.
In general there shouldn't be a need to change the config file at all for a Flax port
|
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread. Please note that issues that do not follow the contributing guidelines are likely to be ignored. |
Flax Implementation of
facebook/convnext-tiny-224Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Flax: @sanchit-gandhi
TODO
Last Updated : 10 Feb, 2023
ci/circleci: tests_flaxactions.test_attention_outputstest.