Convert a HuggingFace model to a custom output directory.
(args)
| 1734 | |
| 1735 | |
| 1736 | def cmd_convert(args): |
| 1737 | """Convert a HuggingFace model to a custom output directory.""" |
| 1738 | import tempfile |
| 1739 | |
| 1740 | model_id = args.model_name |
| 1741 | output_dir = args.output_dir |
| 1742 | lora_path = getattr(args, 'lora', None) |
| 1743 | |
| 1744 | if output_dir is None: |
| 1745 | output_dir = get_weights_dir(model_id) |
| 1746 | else: |
| 1747 | output_dir = Path(output_dir) |
| 1748 | |
| 1749 | cache_dir = getattr(args, 'cache_dir', None) |
| 1750 | token = getattr(args, 'token', None) |
| 1751 | |
| 1752 | temp_merged_dir = None |
| 1753 | |
| 1754 | if lora_path: |
| 1755 | merged_model, tokenizer = merge_lora_adapter(model_id, lora_path, cache_dir, token) |
| 1756 | if merged_model is None: |
| 1757 | return 1 |
| 1758 | |
| 1759 | temp_merged_dir = tempfile.mkdtemp(prefix="cactus_lora_merged_") |
| 1760 | print_color(YELLOW, f"Saving merged model to temp directory: {temp_merged_dir}") |
| 1761 | merged_model.save_pretrained(temp_merged_dir) |
| 1762 | tokenizer.save_pretrained(temp_merged_dir) |
| 1763 | |
| 1764 | lora_tok_config = Path(lora_path) / "tokenizer_config.json" |
| 1765 | if lora_tok_config.exists(): |
| 1766 | shutil.copy2(lora_tok_config, Path(temp_merged_dir) / "tokenizer_config.json") |
| 1767 | |
| 1768 | del merged_model |
| 1769 | import torch |
| 1770 | if torch.cuda.is_available(): |
| 1771 | torch.cuda.empty_cache() |
| 1772 | |
| 1773 | model_id = temp_merged_dir |
| 1774 | |
| 1775 | class DownloadArgs: |
| 1776 | pass |
| 1777 | |
| 1778 | download_args = DownloadArgs() |
| 1779 | download_args.model_id = model_id |
| 1780 | download_args.original_model_id = args.model_name |
| 1781 | download_args.precision = args.precision |
| 1782 | download_args.cache_dir = cache_dir |
| 1783 | download_args.token = token |
| 1784 | download_args.reconvert = True |
| 1785 | |
| 1786 | original_get_weights = get_weights_dir |
| 1787 | |
| 1788 | def custom_weights_dir(mid): |
| 1789 | return output_dir |
| 1790 | |
| 1791 | import src.cli as cli_module |
| 1792 | cli_module.get_weights_dir = custom_weights_dir |
| 1793 |
no test coverage detected