Args: model: A nn.Module state: A pytree of the current state of the model data: Batch of data to apply to the model dropout_rng: A key to use to generate rng for dropout Returns: new_state: Same format as state. metrics: Dictionary of model metrics such as loss, train
(model, config, state_mesh_shardings, params_shardings, state, data, dropout_rng)
| 220 | |
| 221 | |
| 222 | def train_step(model, config, state_mesh_shardings, params_shardings, state, data, dropout_rng): |
| 223 | """ |
| 224 | |
| 225 | Args: |
| 226 | model: A nn.Module |
| 227 | state: A pytree of the current state of the model |
| 228 | data: Batch of data to apply to the model |
| 229 | dropout_rng: A key to use to generate rng for dropout |
| 230 | |
| 231 | Returns: |
| 232 | new_state: Same format as state. |
| 233 | metrics: Dictionary of model metrics such as loss, training rate, etc. |
| 234 | rng2: A new rng key that can be used in future calls. |
| 235 | |
| 236 | """ |
| 237 | reference_params, reference_params_sharding, extra_dpo_args, _loss_fn = ( |
| 238 | [], |
| 239 | [], |
| 240 | [], |
| 241 | loss_fn, |
| 242 | ) |
| 243 | if config.use_dpo: |
| 244 | state, reference_params = _split_dpo_state(state) |
| 245 | state_mesh_shardings, reference_params_sharding = _split_dpo_state(state_mesh_shardings) |
| 246 | extra_dpo_args = [reference_params] |
| 247 | _loss_fn = dpo_loss_fn |
| 248 | |
| 249 | params = state.params |
| 250 | |
| 251 | if config.gradient_accumulation_steps > 1: |
| 252 | loss, aux, raw_grads = gradient_accumulation_loss_and_grad( |
| 253 | _loss_fn, |
| 254 | config, |
| 255 | model, |
| 256 | params, |
| 257 | params_shardings, |
| 258 | data, |
| 259 | dropout_rng, |
| 260 | extra_dpo_args, |
| 261 | ) |
| 262 | else: |
| 263 | if config.optimizer_memory_host_offload: |
| 264 | if config.use_dpo: |
| 265 | reference_params = jax.device_put( |
| 266 | reference_params, |
| 267 | max_utils.with_memory_kind(reference_params_sharding, "device"), |
| 268 | ) |
| 269 | extra_dpo_args = [reference_params] |
| 270 | if config.shard_optimizer_over_data: |
| 271 | params = jax.tree.map( |
| 272 | functools.partial(sharding.maybe_shard_with_name, shard_mode=config.shard_mode), |
| 273 | params, |
| 274 | params_shardings, |
| 275 | ) |
| 276 | grad_func = jax.value_and_grad(_loss_fn, argnums=4, has_aux=True) |
| 277 | (loss, aux), raw_grads = grad_func(model, config, data, dropout_rng, params, *extra_dpo_args, is_train=True) |
| 278 | |
| 279 | raw_grads = jax.tree_util.tree_map( |
nothing calls this directly
no test coverage detected