|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -import itertools |
16 | | - |
17 | 15 | import tensorflow as tf |
18 | 16 | import tree |
19 | 17 |
|
@@ -82,51 +80,34 @@ def wrapped_generate_function( |
82 | 80 |
|
83 | 81 | @jax.jit |
84 | 82 | def compiled_generate_function(inputs, end_token_id, state): |
85 | | - ( |
86 | | - sampler_variables, |
87 | | - trainable_variables, |
88 | | - non_trainable_variables, |
89 | | - ) = state |
90 | | - mapping = itertools.chain( |
91 | | - zip(self._sampler.variables, sampler_variables), |
92 | | - zip(self.trainable_variables, trainable_variables), |
93 | | - zip(self.non_trainable_variables, non_trainable_variables), |
94 | | - ) |
| 83 | + # The only state we update during generation is sampler state, |
| 84 | + # all weights are fixed and will not change. |
| 85 | + mapping = zip(self._sampler.variables, state) |
95 | 86 |
|
96 | 87 | with keras.StatelessScope(state_mapping=mapping) as scope: |
97 | 88 | outputs = self.generate_step(inputs, end_token_id) |
98 | 89 |
|
99 | 90 | # Get updated sampler variables from the stateless scope. |
100 | | - sampler_variables = [] |
| 91 | + state = [] |
101 | 92 | for v in self._sampler.variables: |
102 | 93 | new_v = scope.get_current_value(v) |
103 | | - sampler_variables.append(new_v if new_v is not None else v) |
104 | | - state = ( |
105 | | - sampler_variables, |
106 | | - trainable_variables, |
107 | | - non_trainable_variables, |
108 | | - ) |
| 94 | + state.append(new_v if new_v is not None else v) |
109 | 95 | return outputs, state |
110 | 96 |
|
111 | 97 | def wrapped_generate_function( |
112 | 98 | inputs, |
113 | 99 | end_token_id=None, |
114 | 100 | ): |
115 | 101 | # Create an explicit tuple of all variable state. |
116 | | - state = ( |
117 | | - self._sampler.variables, |
118 | | - self.trainable_variables, |
119 | | - self.non_trainable_variables, |
120 | | - ) |
121 | 102 | inputs = tree.map_structure(ops.convert_to_tensor, inputs) |
122 | 103 | outputs, state = compiled_generate_function( |
123 | 104 | inputs, |
124 | 105 | end_token_id, |
125 | | - state, |
| 106 | + self._sampler.variables, |
126 | 107 | ) |
127 | 108 | # Only assign the sampler variables (random seeds), as other |
128 | 109 | # model variables should never be updated in generation. |
129 | | - for ref_v, v in zip(self._sampler.variables, state[0]): |
| 110 | + for ref_v, v in zip(self._sampler.variables, state): |
130 | 111 | ref_v.assign(v) |
131 | 112 | return outputs |
132 | 113 |
|
|
0 commit comments