Conversation
|
Seems you are using me but didn't get OPENAI_API_KEY seted in Variables/Secrets for this repo. you could follow readme for more information |
| num_parts = 0 | ||
|
|
||
| for filename in os.listdir(dir_model): | ||
| if filename.startswith("pytorch_model-"): | ||
| num_parts += 1 | ||
|
|
||
| num_parts = sum( | ||
| 1 | ||
| for filename in os.listdir(dir_model) | ||
| if filename.startswith("pytorch_model-") | ||
| ) | ||
| if num_parts > 0: | ||
| print("gguf: found " + str(num_parts) + " model parts") | ||
| print(f"gguf: found {str(num_parts)} model parts") |
There was a problem hiding this comment.
Function count_model_parts refactored with the following changes:
- Convert for loop into call to sum() (
sum-comprehension) - Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation)
| num_parts = 0 | ||
| for filename in os.listdir(dir_model): | ||
| if filename.endswith(prefix): | ||
| num_parts += 1 | ||
|
|
||
| return num_parts | ||
| return sum( | ||
| 1 for filename in os.listdir(dir_model) if filename.endswith(prefix) | ||
| ) |
There was a problem hiding this comment.
Function Model.count_model_parts refactored with the following changes:
- Convert for loop into call to sum() (
sum-comprehension) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| self.gguf_writer.add_rope_dimension_count(hidden_size // head_count) | ||
| self.gguf_writer.add_head_count(head_count) | ||
| self.gguf_writer.add_rope_dimension_count(hidden_size // head_count_kv) | ||
| self.gguf_writer.add_head_count(head_count_kv) |
There was a problem hiding this comment.
Function PersimmonModel.set_gguf_parameters refactored with the following changes:
- Use previously assigned local variable (
use-assigned-variable)
| num_parts = 0 | ||
| for filename in os.listdir(dir_model): | ||
| if filename.endswith(prefix): | ||
| num_parts += 1 | ||
|
|
||
| return num_parts | ||
| return sum( | ||
| 1 for filename in os.listdir(dir_model) if filename.endswith(prefix) | ||
| ) |
There was a problem hiding this comment.
Function Model.count_model_parts refactored with the following changes:
- Convert for loop into call to sum() (
sum-comprehension) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if arch == "RWForCausalLM" or arch == "LlamaForCausalLM": | ||
| if arch in ["RWForCausalLM", "LlamaForCausalLM"]: |
There was a problem hiding this comment.
Function Model._get_model_architecture refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
| added_tokens = dict( | ||
| (item['content'], item['id']) | ||
| added_tokens = { | ||
| item['content']: item['id'] | ||
| for item in tokenizer_json.get('added_tokens', []) | ||
| # Added tokens here can be duplicates of the main vocabulary. | ||
| if item['content'] not in self.bpe_tokenizer ) | ||
| if item['content'] not in self.bpe_tokenizer | ||
| } |
There was a problem hiding this comment.
Function BpeVocab.__init__ refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension)
| score = -1000.0 | ||
| for text in self.added_tokens_list: | ||
| score = -1000.0 |
There was a problem hiding this comment.
Function BpeVocab.added_tokens refactored with the following changes:
- Hoist statements out of for/while loops (
hoist-statement-from-loop)
| score = -1000.0 | ||
| for text in self.added_tokens_list: | ||
| score = -1000.0 |
There was a problem hiding this comment.
Function SentencePieceVocab.added_tokens refactored with the following changes:
- Hoist statements out of for/while loops (
hoist-statement-from-loop)
|
|
||
| def merge_multifile_models(models_plus: list[ModelPlus]) -> ModelPlus: | ||
| formats = set(mp.format for mp in models_plus) | ||
| formats = {mp.format for mp in models_plus} |
There was a problem hiding this comment.
Function merge_multifile_models refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension)
| return LazyTensor(load, lazy_tensor.shape, lazy_tensor.data_type, f'permute({n_head}, {n_head_kv}) ' + lazy_tensor.description) | ||
|
|
||
| return LazyTensor( | ||
| load, | ||
| lazy_tensor.shape, | ||
| lazy_tensor.data_type, | ||
| f'permute({n_head}, {n_head_kv}) {lazy_tensor.description}', | ||
| ) |
There was a problem hiding this comment.
Function permute_lazy refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
|
|
||
| s = lazy_tensor.shape.copy() | ||
| s[0] = s[0] // 3 | ||
| return LazyTensor(load, s, lazy_tensor.data_type, f'permute({n_head}, {n_head_kv}) ' + lazy_tensor.description) | ||
| return LazyTensor( | ||
| load, | ||
| s, | ||
| lazy_tensor.data_type, | ||
| f'permute({n_head}, {n_head_kv}) {lazy_tensor.description}', | ||
| ) |
There was a problem hiding this comment.
Function permute_part_lazy refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
|
|
||
| s = lazy_tensor.shape.copy() | ||
| s[0] = s[0] // 3 | ||
| return LazyTensor(load, s, lazy_tensor.data_type, 'part ' + lazy_tensor.description) | ||
| return LazyTensor( | ||
| load, s, lazy_tensor.data_type, f'part {lazy_tensor.description}' | ||
| ) |
There was a problem hiding this comment.
Function part_lazy refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| if params.n_vocab != vocab.vocab_size: | ||
| assert isinstance(vocab, BpeVocab) or isinstance(vocab, SentencePieceVocab) | ||
| if params.n_vocab == vocab.vocab_size_base: | ||
| print("Ignoring added_tokens.json since model matches vocab size without it.") | ||
| vocab.added_tokens_list = [] | ||
| vocab.vocab_size = vocab.vocab_size_base | ||
| return | ||
| msg = f"Vocab size mismatch (model has {params.n_vocab}, but {vocab.fname_tokenizer}" | ||
| if vocab.fname_added_tokens is not None: | ||
| msg += f" combined with {vocab.fname_added_tokens}" | ||
| msg += f" has {vocab.vocab_size})." | ||
| if vocab.vocab_size < params.n_vocab < vocab.vocab_size + 20 and vocab.fname_added_tokens is None: | ||
| msg += f" Most likely you are missing added_tokens.json (should be in {vocab.fname_tokenizer.parent})." | ||
| raise Exception(msg) | ||
| if params.n_vocab == vocab.vocab_size: | ||
| return | ||
| assert isinstance(vocab, (BpeVocab, SentencePieceVocab)) | ||
| if params.n_vocab == vocab.vocab_size_base: | ||
| print("Ignoring added_tokens.json since model matches vocab size without it.") | ||
| vocab.added_tokens_list = [] | ||
| vocab.vocab_size = vocab.vocab_size_base | ||
| return | ||
| msg = f"Vocab size mismatch (model has {params.n_vocab}, but {vocab.fname_tokenizer}" | ||
| if vocab.fname_added_tokens is not None: | ||
| msg += f" combined with {vocab.fname_added_tokens}" | ||
| msg += f" has {vocab.vocab_size})." | ||
| if vocab.vocab_size < params.n_vocab < vocab.vocab_size + 20 and vocab.fname_added_tokens is None: | ||
| msg += f" Most likely you are missing added_tokens.json (should be in {vocab.fname_tokenizer.parent})." | ||
| raise Exception(msg) |
There was a problem hiding this comment.
Function check_vocab_size refactored with the following changes:
- Add guard clause (
last-if-guard) - Merge isinstance calls (
merge-isinstance)
| if not isinstance(dt, QuantizedDataType): | ||
| return arr | ||
| return dt.quantize(arr) | ||
| return arr if not isinstance(dt, QuantizedDataType) else dt.quantize(arr) |
There was a problem hiding this comment.
Function OutputFile.maybe_do_quantize refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| wq_type = model[gguf.TENSOR_NAMES[gguf.MODEL_TENSOR.ATTN_Q].format(bid=0)+".weight"].data_type | ||
| wq_type = model[ | ||
| f"{gguf.TENSOR_NAMES[gguf.MODEL_TENSOR.ATTN_Q].format(bid=0)}.weight" | ||
| ].data_type |
There was a problem hiding this comment.
Function pick_output_type refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| self.past = struct.unpack('<i', bytes(data[offset:offset + 4]))[0]; offset += 4 | ||
| self.lbfgs_m = struct.unpack('<i', bytes(data[offset:offset + 4]))[0]; offset += 4 | ||
| self.nx = struct.unpack('N', bytes(data[offset:offset + 8]))[0]; offset += 8 | ||
| self.iter = struct.unpack('<i', bytes(data[offset:offset + 4]))[0]; offset += 4 | ||
| self.just_initialized = bool(struct.unpack('<i', bytes(data[offset:offset + 4]))[0]); offset += 4 | ||
| self.past = struct.unpack('<i', bytes(data[offset:offset + 4]))[0] | ||
| offset += 4 | ||
| self.lbfgs_m = struct.unpack('<i', bytes(data[offset:offset + 4]))[0] | ||
| offset += 4 | ||
| self.nx = struct.unpack('N', bytes(data[offset:offset + 8]))[0] | ||
| offset += 8 | ||
| self.iter = struct.unpack('<i', bytes(data[offset:offset + 4]))[0] | ||
| offset += 4 | ||
| self.just_initialized = bool(struct.unpack('<i', bytes(data[offset:offset + 4]))[0]) | ||
| offset += 4 |
There was a problem hiding this comment.
Function OptimizationContext.load refactored with the following changes:
- Replace unneeded comprehension with generator [×2] (
comprehension-to-generator)
| magic = bytes(reversed(data[offset:offset + 4])); offset += 4 | ||
| magic = bytes(reversed(data[offset:offset + 4])) | ||
| offset += 4 | ||
| if magic != b'ggcl': | ||
| raise ValueError(f"File header magic indicates, that this is no finetune-lora checkpoint file. Expected 'ggcl', Got '{str(magic)}'") | ||
| raise ValueError( | ||
| f"File header magic indicates, that this is no finetune-lora checkpoint file. Expected 'ggcl', Got '{magic}'" | ||
| ) | ||
|
|
||
| self.version = struct.unpack('<I', bytes(data[offset:offset + 4]))[0]; offset += 4 | ||
| self.version = struct.unpack('<I', bytes(data[offset:offset + 4]))[0] | ||
| offset += 4 | ||
| if self.version != 0: | ||
| raise ValueError('Invalid version of checkpoint file') | ||
|
|
||
| self.train_its = struct.unpack('<I', bytes(data[offset:offset + 4]))[0]; offset += 4 | ||
| self.train_samples = struct.unpack('<I', bytes(data[offset:offset + 4]))[0]; offset += 4 | ||
| self.train_tokens = struct.unpack('<I', bytes(data[offset:offset + 4]))[0]; offset += 4 | ||
| self.train_its = struct.unpack('<I', bytes(data[offset:offset + 4]))[0] | ||
| offset += 4 | ||
| self.train_samples = struct.unpack('<I', bytes(data[offset:offset + 4]))[0] | ||
| offset += 4 | ||
| self.train_tokens = struct.unpack('<I', bytes(data[offset:offset + 4]))[0] | ||
| offset += 4 |
There was a problem hiding this comment.
Function LoraCheckpoint.load refactored with the following changes:
- Remove unnecessary calls to
str()from formatted values in f-strings (remove-str-from-fstring)
| filename = os.fsdecode(listing) | ||
| if filename.endswith(".txt"): | ||
| file = open("./examples/jeopardy/results/" + filename, "rt") | ||
| file = open(f"./examples/jeopardy/results/{filename}", "rt") |
There was a problem hiding this comment.
Function calculatecorrect refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation) - Iterate over files directly rather than using readlines() (
use-file-iterator)
| if name in ( | ||
| if name in { | ||
| "logit_scale", | ||
| "text_model.embeddings.position_ids", | ||
| "vision_model.embeddings.position_ids", | ||
| ): | ||
| }: | ||
| return True | ||
|
|
||
| if has_llava and name in ["visual_projection.weight", "vision_model.post_layernorm.weight", "vision_model.post_layernorm.bias"]: | ||
| if has_llava and name in { | ||
| "visual_projection.weight", | ||
| "vision_model.post_layernorm.weight", | ||
| "vision_model.post_layernorm.bias", | ||
| }: | ||
| return True | ||
|
|
||
| if name.startswith("v") and not has_vision: | ||
| return True | ||
|
|
||
| if name.startswith("t") and not has_text: | ||
| return True | ||
|
|
||
| return False | ||
| return bool(name.startswith("t") and not has_text) |
There was a problem hiding this comment.
Function should_skip_tensor refactored with the following changes:
- Use set when checking membership of a collection of literals [×2] (
collection-into-set) - Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp) - Simplify boolean if expression (
boolean-if-exp-identity)
| with open(dir_model + "/vocab.json", "r", encoding="utf-8") as f: | ||
| with open(f"{dir_model}/vocab.json", "r", encoding="utf-8") as f: | ||
| vocab = json.load(f) | ||
| tokens = [key for key in vocab] | ||
| tokens = list(vocab) | ||
|
|
||
| with open(dir_model + "/config.json", "r", encoding="utf-8") as f: | ||
| with open(f"{dir_model}/config.json", "r", encoding="utf-8") as f: |
There was a problem hiding this comment.
Lines 100-250 refactored with the following changes:
- Use f-string instead of string concatenation [×3] (
use-fstring-for-concatenation) - Replace identity comprehension with call to collection constructor (
identity-comprehension) - Move setting of default value for variable into
elsebranch (introduce-default-else) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif) - Replace if statement with if expression (
assign-if-exp)
| # BakLLaVA models contain CLIP tensors in it | ||
| clip_tensors = [k for k, v in checkpoint.items() if k.startswith("model.vision_tower")] | ||
| if len(clip_tensors) > 0: | ||
| if clip_tensors := [ | ||
| k for k, v in checkpoint.items() if k.startswith("model.vision_tower") | ||
| ]: |
There was a problem hiding this comment.
Lines 26-28 refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Simplify sequence length comparison (
simplify-len-comparison)
This removes the following comments ( why? ):
# BakLLaVA models contain CLIP tensors in it
| if json[key] == None: | ||
| return False | ||
| return True | ||
| return json[key] is not None |
There was a problem hiding this comment.
Function is_present refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp) - Use x is None rather than x == None (
none-compare) - Simplify boolean if expression (
boolean-if-exp-identity) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast)
| postData["prompt"] = body["prompt"] | ||
| postData = { | ||
| "prompt": convert_chat(body["messages"]) if chat else body["prompt"] | ||
| } |
There was a problem hiding this comment.
Function make_postData refactored with the following changes:
- Merge dictionary assignment with declaration (
merge-dict-assign) - Replace if statement with if expression [×2] (
assign-if-exp)
| self.nbytes = 0 | ||
| else: | ||
| self.nbytes = int(np.product(self.ne)) * 4 | ||
| self.nbytes = 0 if len(self.ne) == 0 else int(np.product(self.ne)) * 4 |
There was a problem hiding this comment.
Function Tensor.__init__ refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| nd = struct.unpack('<I', bytes(data[offset:offset + 4]))[0]; offset += 4 | ||
| namelen = struct.unpack('<I', bytes(data[offset:offset + 4]))[0]; offset += 4 | ||
| dtype = struct.unpack('<I', bytes(data[offset:offset + 4]))[0]; offset += 4 | ||
| nd = struct.unpack('<I', bytes(data[offset:offset + 4]))[0] | ||
| offset += 4 | ||
| namelen = struct.unpack('<I', bytes(data[offset:offset + 4]))[0] | ||
| offset += 4 | ||
| dtype = struct.unpack('<I', bytes(data[offset:offset + 4]))[0] | ||
| offset += 4 | ||
|
|
||
| assert(nd == len(self.ne)) | ||
| ne = [] | ||
| for d in range(nd): | ||
| n = struct.unpack('<I', bytes(data[offset:offset + 4]))[0]; offset += 4 | ||
| for _ in range(nd): | ||
| n = struct.unpack('<I', bytes(data[offset:offset + 4]))[0] | ||
| offset += 4 |
There was a problem hiding this comment.
Function Tensor.load refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| for i in range(layer): | ||
| for _ in range(layer): | ||
| values += [0.0] | ||
| c = np.array(values, dtype=float) | ||
| c = matrix(c) | ||
|
|
||
| # Setting capacity and neuron count per batch | ||
| CAP = capacity | ||
| CAP = int(CAP / batch) | ||
| neuron = int(neuron / batch) | ||
| coeff = [] | ||
| h = [] | ||
|
|
||
| # Constraint 1: Total neuron activation constraint | ||
| lst = [] | ||
| for i in range(neuron * layer): | ||
| lst.append(1) | ||
| for i in range(layer): | ||
| lst.append(0) | ||
| coeff.append(lst) | ||
| h.append(CAP) | ||
|
|
||
| CAP //= batch | ||
| neuron //= batch | ||
| lst = [1 for _ in range(neuron * layer)] | ||
| lst.extend(0 for _ in range(layer)) | ||
| coeff = [lst] | ||
| h = [CAP] | ||
| # Constraint 2: Threshold constraint for GPU split per layer | ||
| for i in range(layer): | ||
| lst = [0] * (neuron * layer + layer) | ||
| for j in range(neuron): | ||
| lst[i * neuron + j] = -1 | ||
| lst[neuron * layer + i] = int(threshold / batch) | ||
| lst[neuron * layer + i] = threshold // batch |
There was a problem hiding this comment.
Function solve_gpu_split refactored with the following changes:
- Replace unused for index with underscore [×3] (
for-index-underscore) - Replace assignment with augmented assignment [×2] (
aug-assign) - Simplify division expressions [×3] (
simplify-division) - Convert for loop into list comprehension [×2] (
list-comprehension) - Move assignment closer to its usage within a block [×2] (
move-assign-in-block) - Merge append into list declaration [×2] (
merge-list-append) - Inline variable that is immediately returned (
inline-immediately-returned-variable) - Replace a for append loop with list extend (
for-append-to-extend)
This removes the following comments ( why? ):
# Constraint 1: Total neuron activation constraint
| while True: | ||
| n = f.readinto(mv) | ||
| if not n: | ||
| break | ||
| file_hash.update(mv[:n]) | ||
| if n := f.readinto(mv): | ||
| file_hash.update(mv[:n]) | ||
|
|
There was a problem hiding this comment.
Function sha256sum refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Lift code into else after jump in control flow (
reintroduce-else) - Swap if/else branches (
swap-if-else-branches)
| if file_hash == hash_value: | ||
| valid_checksum = "V" | ||
| file_missing = "" | ||
| else: | ||
| valid_checksum = "" | ||
| file_missing = "" | ||
| valid_checksum = "V" if file_hash == hash_value else "" | ||
| file_missing = "" |
There was a problem hiding this comment.
Lines 57-62 refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp) - Hoist repeated code outside conditional statement (
hoist-statement-from-if)
| k = text.replace('\n', '\\n') | ||
| k = k.replace('\t', '\\t') | ||
| k = '"' + k + '"' | ||
| k = f'"{k}"' |
There was a problem hiding this comment.
Lines 59-73 refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Use f-string instead of string concatenation [×3] (
use-fstring-for-concatenation)
| dir_tokenizer = args.dir_tokenizer | ||
|
|
||
| tokenizer = SentencePieceProcessor(dir_tokenizer + '/tokenizer.model') | ||
| tokenizer = SentencePieceProcessor(f'{dir_tokenizer}/tokenizer.model') |
There was a problem hiding this comment.
Lines 16-83 refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Use f-string instead of string concatenation [×16] (
use-fstring-for-concatenation)
This removes the following comments ( why? ):
# '_Hello'
# 'Hello'
# '_'
# ' Hello'
# ' Hello Hello'
# 'Hello Hello'
Branch
mainrefactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
mainbranch, then run:Help us improve this pull request!