-
Notifications
You must be signed in to change notification settings - Fork 95
feat(cache): replace Git-based hub cache with HF Storage Buckets #1102
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
base: review/hf-buckets-cache
Are you sure you want to change the base?
Changes from all commits
2ff2184
069a2b2
5f5d285
70cefaf
8ce3a16
c39c252
e6160e8
117479e
d894880
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -27,17 +27,13 @@ | |||||||||||||||||||||||||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||||||||||||||||||||||||||
|
dacorvo marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||
| from transformers import PreTrainedModel | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| from optimum.neuron.cache.entries.multi_model import MultiModelCacheEntry | ||||||||||||||||||||||||||||||||||||||||||||||||
| from optimum.neuron.cache.entries.single_model import SingleModelCacheEntry | ||||||||||||||||||||||||||||||||||||||||||||||||
| from optimum.neuron.cache.traced import cache_traced_neuron_artifacts | ||||||||||||||||||||||||||||||||||||||||||||||||
| from optimum.neuron.utils import ( | ||||||||||||||||||||||||||||||||||||||||||||||||
| DiffusersPretrainedConfig, | ||||||||||||||||||||||||||||||||||||||||||||||||
| is_neuronx_available, | ||||||||||||||||||||||||||||||||||||||||||||||||
| store_compilation_config, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| from ...exporters.error_utils import OutputMatchError, ShapeError | ||||||||||||||||||||||||||||||||||||||||||||||||
| from ...neuron.utils.cache_utils import get_model_name_or_path | ||||||||||||||||||||||||||||||||||||||||||||||||
| from ...neuron.utils.system import get_neuron_major | ||||||||||||||||||||||||||||||||||||||||||||||||
| from ...neuron.utils.version_utils import get_neuronxcc_version | ||||||||||||||||||||||||||||||||||||||||||||||||
| from ...utils import ( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -346,7 +342,6 @@ def export_models( | |||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| failed_models = [] | ||||||||||||||||||||||||||||||||||||||||||||||||
| total_compilation_time = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
| compile_configs = {} | ||||||||||||||||||||||||||||||||||||||||||||||||
| for i, model_name in enumerate(models_and_neuron_configs.keys()): | ||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info(f"***** Compiling {model_name} *****") | ||||||||||||||||||||||||||||||||||||||||||||||||
| submodel, sub_neuron_config = models_and_neuron_configs[model_name] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -372,6 +367,9 @@ def export_models( | |||||||||||||||||||||||||||||||||||||||||||||||
| compiler_workdir=compiler_workdir, | ||||||||||||||||||||||||||||||||||||||||||||||||
| inline_weights_to_neff=inline_weights_to_neff, | ||||||||||||||||||||||||||||||||||||||||||||||||
| optlevel=optlevel, | ||||||||||||||||||||||||||||||||||||||||||||||||
| model_name_or_path=model_name_or_path, | ||||||||||||||||||||||||||||||||||||||||||||||||
| task=task, | ||||||||||||||||||||||||||||||||||||||||||||||||
| disable_neuron_cache=disable_neuron_cache, | ||||||||||||||||||||||||||||||||||||||||||||||||
| **compiler_kwargs, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
| compilation_time = time.time() - start_time | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -411,26 +409,9 @@ def export_models( | |||||||||||||||||||||||||||||||||||||||||||||||
| output_hidden_states=getattr(sub_neuron_config, "output_hidden_states", False), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
| model_config.save_pretrained(output_path.parent) | ||||||||||||||||||||||||||||||||||||||||||||||||
| compile_configs[model_name] = model_config | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info(f"[Total compilation Time] {np.round(total_compilation_time, 2)} seconds.") | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| # cache neuronx model | ||||||||||||||||||||||||||||||||||||||||||||||||
| if not disable_neuron_cache and is_neuronx_available(): | ||||||||||||||||||||||||||||||||||||||||||||||||
| model_id = get_model_name_or_path(model_config) if model_name_or_path is None else model_name_or_path | ||||||||||||||||||||||||||||||||||||||||||||||||
| if len(compile_configs) == 1: | ||||||||||||||||||||||||||||||||||||||||||||||||
| # FIXME: this is overly complicated just to pass the config | ||||||||||||||||||||||||||||||||||||||||||||||||
| cache_config = list(compile_configs.values())[0] | ||||||||||||||||||||||||||||||||||||||||||||||||
| cache_entry = SingleModelCacheEntry(model_id=model_id, task=task, config=cache_config) | ||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||
| cache_entry = MultiModelCacheEntry(model_id=model_id, configs=compile_configs) | ||||||||||||||||||||||||||||||||||||||||||||||||
| except NotImplementedError: | ||||||||||||||||||||||||||||||||||||||||||||||||
| logger.warning(f"Cache indexing is not supported for {model_id}.") | ||||||||||||||||||||||||||||||||||||||||||||||||
| cache_entry = None | ||||||||||||||||||||||||||||||||||||||||||||||||
| if cache_entry is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||
| cache_traced_neuron_artifacts(neuron_dir=output_dir, cache_entry=cache_entry) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| # remove models failed to export | ||||||||||||||||||||||||||||||||||||||||||||||||
| for i, model_name in failed_models: | ||||||||||||||||||||||||||||||||||||||||||||||||
| output_file_names.pop(model_name) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -451,6 +432,9 @@ def export( | |||||||||||||||||||||||||||||||||||||||||||||||
| auto_cast_type: str = "bf16", | ||||||||||||||||||||||||||||||||||||||||||||||||
| disable_fast_relayout: bool = False, | ||||||||||||||||||||||||||||||||||||||||||||||||
| disable_fallback: bool = False, | ||||||||||||||||||||||||||||||||||||||||||||||||
| model_name_or_path: str | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||
| task: str | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||
| disable_neuron_cache: bool = False, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> tuple[list[str], list[str]]: | ||||||||||||||||||||||||||||||||||||||||||||||||
| if is_neuronx_available(): | ||||||||||||||||||||||||||||||||||||||||||||||||
| return export_neuronx( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -463,6 +447,9 @@ def export( | |||||||||||||||||||||||||||||||||||||||||||||||
| instance_type=instance_type, | ||||||||||||||||||||||||||||||||||||||||||||||||
| auto_cast=auto_cast, | ||||||||||||||||||||||||||||||||||||||||||||||||
| auto_cast_type=auto_cast_type, | ||||||||||||||||||||||||||||||||||||||||||||||||
| model_name_or_path=model_name_or_path, | ||||||||||||||||||||||||||||||||||||||||||||||||
| task=task, | ||||||||||||||||||||||||||||||||||||||||||||||||
| disable_neuron_cache=disable_neuron_cache, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||
| raise RuntimeError( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -480,6 +467,9 @@ def export_neuronx( | |||||||||||||||||||||||||||||||||||||||||||||||
| optlevel: str = "2", | ||||||||||||||||||||||||||||||||||||||||||||||||
| auto_cast: str | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||
| auto_cast_type: str = "bf16", | ||||||||||||||||||||||||||||||||||||||||||||||||
| model_name_or_path: str | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||
| task: str | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||
| disable_neuron_cache: bool = False, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> tuple[list[str], list[str]]: | ||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||
| Exports a PyTorch model to a serialized TorchScript module compiled by neuronx-cc compiler. | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -559,18 +549,27 @@ def export_neuronx( | |||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
| inline_weights_to_neff = True | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| # Start trace | ||||||||||||||||||||||||||||||||||||||||||||||||
| trace_neuronx( | ||||||||||||||||||||||||||||||||||||||||||||||||
| model=checked_model, | ||||||||||||||||||||||||||||||||||||||||||||||||
| config=config, | ||||||||||||||||||||||||||||||||||||||||||||||||
| dummy_inputs=dummy_inputs_tuple, | ||||||||||||||||||||||||||||||||||||||||||||||||
| compiler_args=compiler_args, | ||||||||||||||||||||||||||||||||||||||||||||||||
| output=output, | ||||||||||||||||||||||||||||||||||||||||||||||||
| tensor_parallel_size=config.tensor_parallel_size, | ||||||||||||||||||||||||||||||||||||||||||||||||
| aliases=aliases, | ||||||||||||||||||||||||||||||||||||||||||||||||
| inline_weights_to_neff=inline_weights_to_neff, | ||||||||||||||||||||||||||||||||||||||||||||||||
| compiler_workdir=compiler_workdir, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
| # Start trace (wrapped in cache context for NEFF fetch/sync) | ||||||||||||||||||||||||||||||||||||||||||||||||
| def _do_trace(): | ||||||||||||||||||||||||||||||||||||||||||||||||
| trace_neuronx( | ||||||||||||||||||||||||||||||||||||||||||||||||
| model=checked_model, | ||||||||||||||||||||||||||||||||||||||||||||||||
| config=config, | ||||||||||||||||||||||||||||||||||||||||||||||||
| dummy_inputs=dummy_inputs_tuple, | ||||||||||||||||||||||||||||||||||||||||||||||||
| compiler_args=compiler_args, | ||||||||||||||||||||||||||||||||||||||||||||||||
| output=output, | ||||||||||||||||||||||||||||||||||||||||||||||||
| tensor_parallel_size=config.tensor_parallel_size, | ||||||||||||||||||||||||||||||||||||||||||||||||
| aliases=aliases, | ||||||||||||||||||||||||||||||||||||||||||||||||
| inline_weights_to_neff=inline_weights_to_neff, | ||||||||||||||||||||||||||||||||||||||||||||||||
| compiler_workdir=compiler_workdir, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if not disable_neuron_cache and model_name_or_path: | ||||||||||||||||||||||||||||||||||||||||||||||||
| from optimum.neuron.cache.hub_cache import hub_neuronx_cache | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| with hub_neuronx_cache(model_id=model_name_or_path, task=task): | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
| with hub_neuronx_cache(model_id=model_name_or_path, task=task): | |
| with hub_neuronx_cache(model_id=model_name_or_path, task=task) as ctx: | |
| # Populate export_config on the cache context so that successful compiles | |
| # can be recorded and later used for advisory cache lookups. | |
| export_config: dict[str, Any] = { | |
| "compiler_args": compiler_args, | |
| } | |
| # Add compiler metadata when available. | |
| if "NEURON_COMPILER_TYPE" in globals(): | |
| export_config["compiler_type"] = NEURON_COMPILER_TYPE | |
| if "NEURON_COMPILER_VERSION" in globals(): | |
| export_config["compiler_version"] = NEURON_COMPILER_VERSION | |
| # Include runtime information and a serializable view of the Neuron config. | |
| export_config["neuron_runtime"] = f"neuronx-{get_neuron_major()}" | |
| if hasattr(config, "to_dict"): | |
| export_config["neuron_config"] = config.to_dict() | |
| elif hasattr(config, "__dict__"): | |
| export_config["neuron_config"] = { | |
| k: v for k, v in config.__dict__.items() if not k.startswith("_") | |
| } | |
| # Only set export_config if the context supports it. | |
| if hasattr(ctx, "export_config"): | |
| ctx.export_config = export_config |
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.
CreateCacheBucketCommand is described as creating a bucket (and exposes a --public flag), but the implementation only pings the local bucket server and writes the bucket ID to HF_HOME. This is misleading UX and makes --public a no-op. Either actually create/verify the bucket via the server (e.g. proxy a create_repo(repo_type="bucket") operation) or rename the command/help to reflect that it only sets configuration.