Decoder only language modeling in inference mode.
| 26 | |
| 27 | @gin.configurable |
| 28 | class DecoderOnlyLanguageModelGenerate(models.DecoderOnlyLanguageModel): |
| 29 | """Decoder only language modeling in inference mode.""" |
| 30 | |
| 31 | decoder_factory = decoder_stack.DecoderStackGenerate |
| 32 | |
| 33 | num_heads: int = gin.REQUIRED |
| 34 | head_size: int = gin.REQUIRED |
| 35 | |
| 36 | def get_fake_input(self) -> dict[str, Any]: |
| 37 | fake_input_dict = super().get_fake_input() |
| 38 | b = self.task_config.batch_size |
| 39 | n = self.num_heads |
| 40 | h = self.head_size |
| 41 | fake_input_dict.update({ |
| 42 | 'dstate': tuple( |
| 43 | [{ |
| 44 | 'current_index': jnp.array([0] * b, dtype=jnp.int32), |
| 45 | 'keys': jnp.zeros((b, 2048, n, h), dtype=jnp.bfloat16), |
| 46 | 'values': jnp.zeros((b, 2048, n, h), dtype=jnp.bfloat16), |
| 47 | 'recurrent_kvq': None, |
| 48 | 'relative_position_bias': jnp.zeros( |
| 49 | (b, n, 1, 1024), dtype=jnp.bfloat16 |
| 50 | ), |
| 51 | }] |
| 52 | * 12 |
| 53 | ), |
| 54 | 'eos': jnp.zeros([1024], dtype=jnp.bfloat16), |
| 55 | 'mask': jnp.ones([1024], dtype=jnp.bfloat16), |
| 56 | 'length': 1, |
| 57 | 'temperature': 1.0, |
| 58 | }) |
| 59 | return fake_input_dict |
| 60 | |
| 61 | def __call__(self, inputs: ...) -> tuple[Any, dict[str, Any]]: |
| 62 | # Make sure this code is not used on untested cases. |
| 63 | if self.mode not in ['init', 'beam_search']: |
| 64 | raise ValueError(f'{type(self)} cannot do mode {self.mode}') |
| 65 | if self.decoder.supports_generate(): |
| 66 | raise ValueError(f'{type(self)}.decoder cannot supports_generate()') |
| 67 | |
| 68 | self.decoder( |
| 69 | input_tokens=inputs['targets'][:, 0:1], |
| 70 | target_tokens=None, |
| 71 | start_of_sequence=inputs['start_of_sequence'], |
| 72 | ) |
| 73 | |
| 74 | b = inputs['targets'].shape[0] |
| 75 | no_start_of_seq = jnp.array([False] * b, dtype=jnp.bool_) |
| 76 | |
| 77 | # This fn is used in both beam_search or topk_sampling. |
| 78 | def tokens_to_logits_fn( |
| 79 | input_token: jnp.ndarray, dstate: tuple[dict[str, jnp.ndarray], ...] |
| 80 | ) -> tuple[jnp.ndarray, tuple[dict[str, jnp.ndarray], ...]]: |
| 81 | (logits, dstate, _) = self.decoder( |
| 82 | input_tokens=input_token, |
| 83 | target_tokens=None, |
| 84 | start_of_sequence=no_start_of_seq, |
| 85 | decoder_state=dstate, |
nothing calls this directly
no outgoing calls
no test coverage detected