-
Notifications
You must be signed in to change notification settings - Fork 777
Add Model Surgery HOWTO #1059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Model Surgery HOWTO #1059
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3077e13
Add Model Surgery HOWTO
marcvanzee 7073952
Update docs/howtos/model_surgery.rst
marcvanzee 5dffa46
Update docs/howtos/model_surgery.rst
marcvanzee e845add
Update docs/howtos/model_surgery.rst
marcvanzee c55ce7f
Update docs/howtos/model_surgery.rst
marcvanzee 88584bf
Incorporate changes
marcvanzee 74215c5
Merge branch 'howto-checkpoints' of https://github.com/marcvanzee/fla…
marcvanzee 497b15e
Merge branch 'master' of https://github.com/google/flax into howto-ch…
marcvanzee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| Model Surgery | ||
| ============================== | ||
|
|
||
| .. testsetup:: | ||
|
|
||
| import functools | ||
| import numpy as np | ||
| import jax | ||
| from jax import lax, random, numpy as jnp | ||
| import flax | ||
| from flax import optim, traverse_util | ||
|
|
||
| from flax import linen as nn | ||
| from flax.core import unfreeze, freeze | ||
|
|
||
| We will show how to get a flat dict of all the tensors, and then go back to a | ||
| nested, frozen dict. This will be demonstrated for both Flax modules and optimizers. | ||
|
|
||
| Surgery with Flax Modules | ||
| -------------------------------- | ||
|
|
||
| Let's create a small convolutional neural network model for our demo. | ||
|
|
||
| .. testcode:: | ||
|
|
||
| class CNN(nn.Module): | ||
| @nn.compact | ||
| def __call__(self, x): | ||
| x = nn.Conv(features=32, kernel_size=(3, 3))(x) | ||
| x = nn.relu(x) | ||
| x = nn.avg_pool(x, window_shape=(2, 2), strides=(2, 2)) | ||
| x = nn.Conv(features=64, kernel_size=(3, 3))(x) | ||
| x = nn.relu(x) | ||
| x = nn.avg_pool(x, window_shape=(2, 2), strides=(2, 2)) | ||
| x = x.reshape((x.shape[0], -1)) | ||
| x = nn.Dense(features=256)(x) | ||
| x = nn.relu(x) | ||
| x = nn.Dense(features=10)(x) | ||
| x = nn.log_softmax(x) | ||
| return x | ||
|
|
||
| def get_initial_params(rng): | ||
| init_shape = jnp.ones((1, 28, 28, 1), jnp.float32) | ||
| initial_params = CNN().init(rng, init_shape)['params'] | ||
| return initial_params | ||
|
|
||
| key = random.PRNGKey(0) | ||
| params = get_initial_params(key) | ||
|
|
||
| print(jax.tree_map(jnp.shape, params)) | ||
|
|
||
| .. testoutput:: | ||
|
|
||
| FrozenDict({ | ||
| Conv_0: { | ||
| bias: (32,), | ||
| kernel: (3, 3, 1, 32), | ||
| }, | ||
| Conv_1: { | ||
| bias: (64,), | ||
| kernel: (3, 3, 32, 64), | ||
| }, | ||
| Dense_0: { | ||
| bias: (256,), | ||
| kernel: (3136, 256), | ||
| }, | ||
| Dense_1: { | ||
| bias: (10,), | ||
| kernel: (256, 10), | ||
| }, | ||
| }) | ||
|
|
||
|
|
||
| Next, get a flat dict for doing model surgery as follows: | ||
|
|
||
| .. testcode:: | ||
|
|
||
| # Unfreeze params to normal dict. | ||
| params = unfreeze(params) | ||
| # Get flattened-key: value list. | ||
| flat_params = {'/'.join(k): v for k, v in traverse_util.flatten_dict(params).items()} | ||
| print(jax.tree_map(jnp.shape, flat_params)) | ||
|
|
||
| .. testoutput:: | ||
| :options: +NORMALIZE_WHITESPACE | ||
|
|
||
| {'Conv_0/bias': (32,), | ||
| 'Conv_0/kernel': (3, 3, 1, 32), | ||
| 'Conv_1/bias': (64,), | ||
| 'Conv_1/kernel': (3, 3, 32, 64), | ||
| 'Dense_0/bias': (256,), | ||
| 'Dense_0/kernel': (3136, 256), | ||
| 'Dense_1/bias': (10,), | ||
| 'Dense_1/kernel': (256, 10)} | ||
|
|
||
| After doing whatever you want, unflatten back: | ||
|
|
||
| .. testcode:: | ||
|
|
||
| # Unflatten. | ||
| unflat_params = traverse_util.unflatten_dict({tuple(k.split('/')): v for k, v in flat_params.items()}) | ||
| # Refreeze. | ||
| unflat_params = freeze(unflat_params) | ||
| print(jax.tree_map(jnp.shape, unflat_params)) | ||
|
|
||
| .. testoutput:: | ||
| :options: +NORMALIZE_WHITESPACE | ||
|
|
||
| FrozenDict({ | ||
| Conv_0: { | ||
| bias: (32,), | ||
| kernel: (3, 3, 1, 32), | ||
| }, | ||
| Conv_1: { | ||
| bias: (64,), | ||
| kernel: (3, 3, 32, 64), | ||
| }, | ||
| Dense_0: { | ||
| bias: (256,), | ||
| kernel: (3136, 256), | ||
| }, | ||
| Dense_1: { | ||
| bias: (10,), | ||
| kernel: (256, 10), | ||
| }, | ||
| }) | ||
|
|
||
| Surgery with Optimizers | ||
| -------------------------------- | ||
|
|
||
| If you're loading from a flax optimizer, all of the variables that should be | ||
| optimized live in ``optimizer.target``. | ||
|
|
||
| .. testcode:: | ||
|
|
||
| opt_def = optim.Adam(1.0) | ||
| opt = opt_def.create(params) | ||
|
|
||
| # Get optimizer state and target vars by: | ||
| opt_state = opt.state_dict() | ||
| print(jax.tree_map(jnp.shape, opt_state)) | ||
|
|
||
| .. testoutput:: | ||
| :options: +NORMALIZE_WHITESPACE | ||
|
|
||
| {'state': {'param_states': {'Conv_0': {'bias': {'grad_ema': (32,), | ||
| 'grad_sq_ema': (32,)}, | ||
| 'kernel': {'grad_ema': (3, 3, 1, 32), 'grad_sq_ema': (3, 3, 1, 32)}}, | ||
| 'Conv_1': {'bias': {'grad_ema': (64,), 'grad_sq_ema': (64,)}, | ||
| 'kernel': {'grad_ema': (3, 3, 32, 64), 'grad_sq_ema': (3, 3, 32, 64)}}, | ||
| 'Dense_0': {'bias': {'grad_ema': (256,), 'grad_sq_ema': (256,)}, | ||
| 'kernel': {'grad_ema': (3136, 256), 'grad_sq_ema': (3136, 256)}}, | ||
| 'Dense_1': {'bias': {'grad_ema': (10,), 'grad_sq_ema': (10,)}, | ||
| 'kernel': {'grad_ema': (256, 10), 'grad_sq_ema': (256, 10)}}}, | ||
| 'step': ()}, | ||
| 'target': {'Conv_0': {'bias': (32,), 'kernel': (3, 3, 1, 32)}, | ||
| 'Conv_1': {'bias': (64,), 'kernel': (3, 3, 32, 64)}, | ||
| 'Dense_0': {'bias': (256,), 'kernel': (3136, 256)}, | ||
| 'Dense_1': {'bias': (10,), 'kernel': (256, 10)}}} | ||
|
|
||
| .. testcode:: | ||
|
|
||
| # Get flattened-key:: value list. | ||
| flat_opt_state = {'/'.join(k): v for k, v in traverse_util.flatten_dict(opt_state).items()} | ||
| print(jax.tree_map(jnp.shape, flat_opt_state)) | ||
|
|
||
| .. testoutput:: | ||
| :options: +NORMALIZE_WHITESPACE | ||
|
|
||
| {'state/param_states/Conv_0/bias/grad_ema': (32,), | ||
| 'state/param_states/Conv_0/bias/grad_sq_ema': (32,), | ||
| 'state/param_states/Conv_0/kernel/grad_ema': (3, 3, 1, 32), | ||
| 'state/param_states/Conv_0/kernel/grad_sq_ema': (3, 3, 1, 32), | ||
| 'state/param_states/Conv_1/bias/grad_ema': (64,), | ||
| 'state/param_states/Conv_1/bias/grad_sq_ema': (64,), | ||
| 'state/param_states/Conv_1/kernel/grad_ema': (3, 3, 32, 64), | ||
| 'state/param_states/Conv_1/kernel/grad_sq_ema': (3, 3, 32, 64), | ||
| 'state/param_states/Dense_0/bias/grad_ema': (256,), | ||
| 'state/param_states/Dense_0/bias/grad_sq_ema': (256,), | ||
| 'state/param_states/Dense_0/kernel/grad_ema': (3136, 256), | ||
| 'state/param_states/Dense_0/kernel/grad_sq_ema': (3136, 256), | ||
| 'state/param_states/Dense_1/bias/grad_ema': (10,), | ||
| 'state/param_states/Dense_1/bias/grad_sq_ema': (10,), | ||
| 'state/param_states/Dense_1/kernel/grad_ema': (256, 10), | ||
| 'state/param_states/Dense_1/kernel/grad_sq_ema': (256, 10), | ||
| 'state/step': (), | ||
| 'target/Conv_0/bias': (32,), | ||
| 'target/Conv_0/kernel': (3, 3, 1, 32), | ||
| 'target/Conv_1/bias': (64,), | ||
| 'target/Conv_1/kernel': (3, 3, 32, 64), | ||
| 'target/Dense_0/bias': (256,), | ||
| 'target/Dense_0/kernel': (3136, 256), | ||
| 'target/Dense_1/bias': (10,), | ||
| 'target/Dense_1/kernel': (256, 10)} | ||
|
|
||
| .. testcode:: | ||
|
|
||
| # Unflatten | ||
| unflat_opt_state = traverse_util.unflatten_dict({tuple(k.split('/')): v for k, v in flat_opt_state.items()}) | ||
| print(jax.tree_map(jnp.shape, unflat_opt_state)) | ||
|
|
||
| .. testoutput:: | ||
| :options: +NORMALIZE_WHITESPACE | ||
|
|
||
| {'state': {'param_states': {'Conv_0': {'bias': {'grad_ema': (32,), | ||
| 'grad_sq_ema': (32,)}, | ||
| 'kernel': {'grad_ema': (3, 3, 1, 32), 'grad_sq_ema': (3, 3, 1, 32)}}, | ||
| 'Conv_1': {'bias': {'grad_ema': (64,), 'grad_sq_ema': (64,)}, | ||
| 'kernel': {'grad_ema': (3, 3, 32, 64), 'grad_sq_ema': (3, 3, 32, 64)}}, | ||
| 'Dense_0': {'bias': {'grad_ema': (256,), 'grad_sq_ema': (256,)}, | ||
| 'kernel': {'grad_ema': (3136, 256), 'grad_sq_ema': (3136, 256)}}, | ||
| 'Dense_1': {'bias': {'grad_ema': (10,), 'grad_sq_ema': (10,)}, | ||
| 'kernel': {'grad_ema': (256, 10), 'grad_sq_ema': (256, 10)}}}, | ||
| 'step': ()}, | ||
| 'target': {'Conv_0': {'bias': (32,), 'kernel': (3, 3, 1, 32)}, | ||
| 'Conv_1': {'bias': (64,), 'kernel': (3, 3, 32, 64)}, | ||
| 'Dense_0': {'bias': (256,), 'kernel': (3136, 256)}, | ||
| 'Dense_1': {'bias': (10,), 'kernel': (256, 10)}}} | ||
|
|
||
| We can restore the optimizer object from the nested-dict state. The restored | ||
| state must agree with the shape of the existing object as a sort of "structural | ||
| unit test". | ||
|
|
||
| .. testcode:: | ||
|
|
||
| restored_opt = opt.restore_state(unflat_opt_state) | ||
| print(jax.tree_map(jnp.shape, restored_opt)) | ||
|
|
||
| .. testoutput:: | ||
| :options: +NORMALIZE_WHITESPACE, +ELLIPSIS | ||
|
|
||
| Optimizer(optimizer_def=<flax.optim.adam.Adam object at ...>, state=OptimizerState(step=(), param_states={'Conv_0': {'bias': _AdamParamState(grad_ema=(32,), grad_sq_ema=(32,)), 'kernel': _AdamParamState(grad_ema=(3, 3, 1, 32), grad_sq_ema=(3, 3, 1, 32))}, 'Conv_1': {'bias': _AdamParamState(grad_ema=(64,), grad_sq_ema=(64,)), 'kernel': _AdamParamState(grad_ema=(3, 3, 32, 64), grad_sq_ema=(3, 3, 32, 64))}, 'Dense_0': {'bias': _AdamParamState(grad_ema=(256,), grad_sq_ema=(256,)), 'kernel': _AdamParamState(grad_ema=(3136, 256), grad_sq_ema=(3136, 256))}, 'Dense_1': {'bias': _AdamParamState(grad_ema=(10,), grad_sq_ema=(10,)), 'kernel': _AdamParamState(grad_ema=(256, 10), grad_sq_ema=(256, 10))}}), target={'Conv_0': {'bias': (32,), 'kernel': (3, 3, 1, 32)}, 'Conv_1': {'bias': (64,), 'kernel': (3, 3, 32, 64)}, 'Dense_0': {'bias': (256,), 'kernel': (3136, 256)}, 'Dense_1': {'bias': (10,), 'kernel': (256, 10)}}) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.