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

Class FlaxLLaMAForCausalLMModule

lwm/llama.py:1049–1106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1047 )
1048
1049class FlaxLLaMAForCausalLMModule(nn.Module):
1050 config: LLaMAConfig
1051 dtype: jnp.dtype = jnp.float32
1052 param_dtype: jnp.dtype=jnp.float32
1053 precision: Optional[Union[jax.lax.Precision, str]]=None
1054
1055 def setup(self):
1056 self.transformer = FlaxLLaMAModule(self.config, dtype=self.dtype)
1057 self.lm_head = nn.Dense(
1058 self.config.vocab_size,
1059 dtype=self.dtype,
1060 param_dtype=self.param_dtype,
1061 use_bias=False,
1062 kernel_init=jax.nn.initializers.normal(stddev=self.config.initializer_range),
1063 precision=self.precision,
1064 )
1065
1066 def __call__(
1067 self,
1068 input_ids,
1069 attention_mask=None,
1070 segment_ids=None,
1071 position_ids=None,
1072 deterministic: bool = True,
1073 init_cache: bool = False,
1074 output_attentions: bool = False,
1075 output_hidden_states: bool = False,
1076 return_dict: bool = True,
1077 ):
1078 batch_size, seq_length = input_ids.shape
1079 if attention_mask is None:
1080 attention_mask = jnp.ones_like(input_ids)
1081 if position_ids is None:
1082 position_ids = jnp.arange(seq_length, dtype=jnp.int32)[None].repeat(batch_size, axis=0)
1083 outputs = self.transformer(
1084 input_ids,
1085 attention_mask,
1086 segment_ids,
1087 position_ids,
1088 deterministic=deterministic,
1089 init_cache=init_cache,
1090 output_attentions=output_attentions,
1091 output_hidden_states=output_hidden_states,
1092 return_dict=return_dict,
1093 )
1094
1095 hidden_states = outputs[0]
1096
1097 if self.config.tie_word_embeddings:
1098 shared_kernel = self.transformer.variables["params"]["wte"]["embedding"].T
1099 lm_logits = self.lm_head.apply({"params": {"kernel": shared_kernel}}, hidden_states)
1100 else:
1101 lm_logits = self.lm_head(hidden_states)
1102
1103 if not return_dict:
1104 return (lm_logits,) + outputs[1:]
1105
1106 return FlaxCausalLMOutput(logits=lm_logits, hidden_states=outputs.hidden_states, attentions=outputs.attentions)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected