MCPcopy Create free account
hub / github.com/LargeWorldModel/LWM / init_cache

Method init_cache

lwm/llama.py:806–824  ·  view source on GitHub ↗

r""" Args: batch_size (`int`): batch_size used for fast auto-regressive decoding. Defines the batch size of the initialized cache. max_length (`int`): maximum possible length for auto-regressive decoding. Defines the sequence length of

(self, batch_size, max_length)

Source from the content-addressed store, hash-verified

804 return random_params
805
806 def init_cache(self, batch_size, max_length):
807 r"""
808 Args:
809 batch_size (`int`):
810 batch_size used for fast auto-regressive decoding. Defines the batch size of the initialized cache.
811 max_length (`int`):
812 maximum possible length for auto-regressive decoding. Defines the sequence length of the initialized
813 cache.
814 """
815 # init input variables to retrieve cache
816 input_ids = jnp.ones((batch_size, max_length))
817 attention_mask = jnp.ones_like(input_ids)
818 segment_ids = None
819 position_ids = jnp.broadcast_to(jnp.arange(jnp.atleast_2d(input_ids).shape[-1]), input_ids.shape)
820
821 init_variables = self.module.init(
822 jax.random.PRNGKey(0), input_ids, attention_mask, segment_ids, position_ids, return_dict=False, init_cache=True
823 )
824 return init_variables["cache"]
825
826 @add_start_docstrings_to_model_forward("")
827 def __call__(

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected