-
Notifications
You must be signed in to change notification settings - Fork 18
Add gemma support #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
|
|
||
| # Sharding config for gemma | ||
| # "replicated" to signify "replicated". | ||
| # Integer signify axis to shard: 0 <= shard axis < rank | ||
|
|
||
| freqs_cis : null # torch.complex64 (16384, 128) | ||
| layers.*.self_attn.wo.weight : 1 # 1, -1] # torch.float32 (2048, 2048) | ||
| layers.*.self_attn.wq.weight : 0 # -1, 1] # torch.float32 (2048, 2048) | ||
| layers.*.self_attn.wk.weight : 0 # -1, 1] # torch.float32 (256, 2048) | ||
| layers.*.self_attn.wv.weight : 0 # -1, 1] # torch.float32 (256, 2048) | ||
| layers.*.mlp.gate_proj.weight : 0 # -1, 1] # torch.float32 (16384, 2048) | ||
| layers.*.mlp.gate_proj.bias : 0 # -1] # torch.float32 (16384,) | ||
| layers.*.mlp.up_proj.weight : 0 # -1, 1] # torch.float32 (16384, 2048) | ||
| layers.*.mlp.up_proj.bias : 0 # -1] # torch.float32 (16384,) | ||
| layers.*.mlp.down_proj.weight : 1 # 1, -1] # torch.float32 (2048, 16384) | ||
| layers.*.mlp.down_proj.bias : null # torch.float32 (2048,) | ||
| layers.*.input_layernorm.weight : null # torch.float32 (2048,) | ||
| layers.*.post_attention_layernorm.weight : null # torch.float32 (2048,) | ||
| norm.weight : null # torch.float32 (2048,) | ||
| embedder.weight : 1 # # 1, -1] # torch.float32 (256000, 2048) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
|
|
||
| # Sharding config for llama-2 | ||
| # Sharding should either be an int between 0 and rank - 1 | ||
| # signifying the axis to shard or -1 / null signifying replicated | ||
|
|
||
|
|
||
| freqs_cis : -1 # torch.complex64 (2048, 64) | ||
| tok_embeddings.weight : 1 # torch.float32 (32000, 4096) | ||
| layers.*.attention.wo.weight : 1 # torch.int8 (4096, 4096) | ||
| layers.*.attention.wo.weight_scaler : 0 # torch.bfloat16 (4096,) | ||
| layers.*.attention.wq.weight : 0 # torch.int8 (4096, 4096) | ||
| layers.*.attention.wq.weight_scaler : 0 # torch.bfloat16 (4096,) | ||
| layers.*.attention.wk.weight : 0 # torch.int8 (4096, 4096) | ||
| layers.*.attention.wk.weight_scaler : 0 # torch.bfloat16 (4096,) | ||
| layers.*.attention.wv.weight : 0 # torch.int8 (4096, 4096) | ||
| layers.*.attention.wv.weight_scaler : 0 # torch.bfloat16 (4096,) | ||
| layers.*.feed_forward.w1.weight : 0 # torch.float32 (11008, 4096) | ||
| layers.*.feed_forward.w2.weight : 1 # torch.float32 (4096, 11008) | ||
| layers.*.feed_forward.w3.weight : 0 # torch.float32 (11008, 4096) | ||
| layers.*.attention_norm.weight : -1 # torch.float32 (4096,) | ||
| layers.*.ffn_norm.weight : -1 # torch.float32 (4096,) | ||
| norm.weight : -1 # torch.float32 (4096,) | ||
| output.weight : 0 # torch.float32 (32000, 4096) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
| from typing import Tuple, Dict | ||
|
|
||
| import dataclasses | ||
| import yaml | ||
|
|
||
| import jax | ||
| import jax.sharding as jsharding | ||
| from jax.experimental import mesh_utils | ||
|
|
@@ -71,6 +73,8 @@ class JetEngineEnvironmentData: | |
| # If Ture, use bfloat16 as dtype. If False, use float32 as dtype | ||
| bf16_enable: bool = True | ||
|
|
||
| sharding_config_path: str = "" | ||
|
|
||
|
|
||
| # pylint: disable-next=all | ||
| class JetEngineEnvironment: | ||
|
|
@@ -100,6 +104,15 @@ def __init__(self, data: JetEngineEnvironmentData): | |
| self.cache_sharding = jsharding.NamedSharding( | ||
| self._mesh, P(*cache_sharding) | ||
| ) | ||
| self._load_sharding_config() | ||
|
|
||
| def _load_sharding_config(self): | ||
| """Load sharding config""" | ||
| if self._data.sharding_config_path: | ||
| with open(self._data.sharding_config_path, encoding="utf-8") as f: | ||
| self._sharding_config = yaml.safe_load(f) | ||
| else: | ||
| self._sharding_config = {} | ||
|
|
||
| def __getattr__(self, name): | ||
| return getattr(self._data, name) | ||
|
|
@@ -150,3 +163,35 @@ def make_caches_generate(self): | |
| ) | ||
| ) | ||
| return caches | ||
|
|
||
| def sharding_by_name(self, name): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, it's more clear than previous hardcode one. |
||
| """Create sharding specified in the config.""" | ||
| if name in self._sharding_config: | ||
| return self.sharding_by_axis(self._sharding_config[name]) | ||
|
|
||
| name = process_sharding_name(name) | ||
| if name in self._sharding_config: | ||
| return self.sharding_by_axis(self._sharding_config[name]) | ||
|
|
||
| raise RuntimeError("Sharding for name: ", name, " not specified") | ||
|
|
||
|
|
||
| def process_sharding_name(name): | ||
| """Replace integers in param name with *. | ||
|
|
||
| Presumably all layers should have the same sharding. | ||
| """ | ||
|
|
||
| def is_integer(t): | ||
| try: | ||
| int(t) | ||
| return True | ||
| # pylint: disable-next=all | ||
| except: # noqa: E722 | ||
| return False | ||
|
|
||
| tokens = name.split(".") | ||
| for i, t in enumerate(tokens): | ||
| if is_integer(t): | ||
| tokens[i] = "*" | ||
| return ".".join(tokens) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,57 +132,53 @@ def repeat_kv(x: torch.Tensor, n_rep: int) -> torch.Tensor: | |
| class Attention(nn.Module): | ||
| """Attention module.""" | ||
|
|
||
| def __init__(self, args, env): | ||
| def __init__(self, n_heads, n_kv_heads, head_dim, hidden_size, device, env): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, it's nice to see the layers is decoupled with args. |
||
| super().__init__() | ||
|
|
||
| self.n_kv_heads = ( | ||
| args.n_heads if args.n_kv_heads is None else args.n_kv_heads | ||
| ) | ||
| self.n_local_heads = args.n_heads | ||
| self.n_local_kv_heads = self.n_kv_heads | ||
| self.n_rep = self.n_local_heads // self.n_local_kv_heads | ||
| self.head_dim = args.dim // args.n_heads | ||
| self.max_seq_len = args.max_seq_len | ||
| self.n_heads = args.n_heads | ||
|
|
||
| self.n_heads = n_heads | ||
| self.n_kv_heads = n_kv_heads | ||
| self.head_dim = head_dim | ||
| self.n_rep = self.n_heads // self.n_kv_heads | ||
| self.env = env | ||
| self.hidden_size = hidden_size | ||
|
|
||
| LinearLayer = WeightOnlyInt8Linear if args.quantize else nn.Linear | ||
| LinearLayer = ( | ||
| WeightOnlyInt8Linear if env.enable_weight_quantization else nn.Linear | ||
| ) | ||
|
|
||
| self.wo = LinearLayer( | ||
| args.n_heads * self.head_dim, | ||
| args.dim, | ||
| n_heads * self.head_dim, | ||
| hidden_size, | ||
| bias=False, | ||
| device=args.device, | ||
| device=device, | ||
| ) | ||
| self.q_size = args.n_heads * self.head_dim | ||
| self.q_size = n_heads * self.head_dim | ||
| self.kv_size = self.n_kv_heads * self.head_dim | ||
| if self.env.qkv_fusion: | ||
| self._register_load_state_dict_pre_hook(self.load_hook) | ||
| self.wqkv = LinearLayer( | ||
| args.dim, | ||
| (args.n_heads + 2 * self.n_kv_heads) * self.head_dim, | ||
| hidden_size, | ||
| (n_heads + 2 * self.n_kv_heads) * self.head_dim, | ||
| bias=False, | ||
| device=args.device, | ||
| device=device, | ||
| ) | ||
| else: | ||
| self.wq = LinearLayer( | ||
| args.dim, | ||
| args.n_heads * self.head_dim, | ||
| hidden_size, | ||
| n_heads * self.head_dim, | ||
| bias=False, | ||
| device=args.device, | ||
| device=device, | ||
| ) | ||
| self.wk = LinearLayer( | ||
| args.dim, | ||
| hidden_size, | ||
| self.n_kv_heads * self.head_dim, | ||
| bias=False, | ||
| device=args.device, | ||
| device=device, | ||
| ) | ||
| self.wv = LinearLayer( | ||
| args.dim, | ||
| hidden_size, | ||
| self.n_kv_heads * self.head_dim, | ||
| bias=False, | ||
| device=args.device, | ||
| device=device, | ||
| ) | ||
|
|
||
| def load_hook(self, state_dict, prefix, *args): | ||
|
|
@@ -210,9 +206,9 @@ def forward( | |
| ) | ||
| else: | ||
| xq, xk, xv = self.wq(x), self.wk(x), self.wv(x) | ||
| xq = xq.view(bsz, seqlen, self.n_local_heads, self.head_dim) | ||
| xk = xk.view(bsz, seqlen, self.n_local_kv_heads, self.head_dim) | ||
| xv = xv.view(bsz, seqlen, self.n_local_kv_heads, self.head_dim) | ||
| xq = xq.view(bsz, seqlen, self.n_heads, self.head_dim) | ||
| xk = xk.view(bsz, seqlen, self.n_kv_heads, self.head_dim) | ||
| xv = xv.view(bsz, seqlen, self.n_kv_heads, self.head_dim) | ||
|
|
||
| self.env.apply_sharding(xq, axis=2) | ||
| self.env.apply_sharding(xk, axis=2) | ||
|
|
@@ -262,7 +258,8 @@ def forward( | |
| self.env.apply_sharding(output, axis=1) | ||
| output = output.transpose(-3, -2).contiguous().view(bsz, seqlen, -1) | ||
| self.env.apply_sharding(output, axis=2) | ||
| return self.wo(output) | ||
| output = self.wo(output) | ||
| return output | ||
| else: | ||
| with jax.named_scope("attn_insert_cache"): | ||
| keys, values, k_scaler, v_scaler = cache.update(xk, xv) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright 2024 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we keep consistency on replicated sharding? If either null or -1 is fine, shall we just keep -1 in our code base (use null in gemma, but -1 in llama)?