def model_downloader( predictor_type: PredictorType, bucket_provider: str, bucket_name: str, model_name: str, model_version: str, model_path: str, temp_dir: str, model_dir: str, ) -> Optional[datetime.datetime]: """ Downloads model to disk. Validates the cloud model path and the downloaded model as well. Args: predictor_type: The predictor type as implemented by the API. bucket_provider: Provider for the bucket. Can be "s3" or "gs". bucket_name: Name of the bucket where the model is stored. model_name: Name of the model. Is part of the model's local path. model_version: Version of the model. Is part of the model's local path. model_path: Model prefix of the versioned model. temp_dir: Where to temporarily store the model for validation. model_dir: The top directory of where all models are stored locally. Returns: The model's timestamp. None if the model didn't pass the validation, if it doesn't exist or if there are not enough permissions. """ logger().info( f"downloading from bucket {bucket_name}/{model_path}, model {model_name} of version {model_version}, temporarily to {temp_dir} and then finally to {model_dir}" ) if bucket_provider == "s3": client = S3(bucket_name) if bucket_provider == "gs": client = GCS(bucket_name) # validate upstream cloud model sub_paths, ts = client.search(model_path) try: validate_model_paths(sub_paths, predictor_type, model_path) except CortexException: logger().info( f"failed validating model {model_name} of version {model_version}") return None # download model to temp dir temp_dest = os.path.join(temp_dir, model_name, model_version) try: client.download_dir_contents(model_path, temp_dest) except CortexException: logger().info( f"failed downloading model {model_name} of version {model_version} to temp dir {temp_dest}" ) shutil.rmtree(temp_dest) return None # validate model model_contents = glob.glob(temp_dest + "*/**", recursive=True) model_contents = util.remove_non_empty_directory_paths(model_contents) try: validate_model_paths(model_contents, predictor_type, temp_dest) except CortexException: logger().info( f"failed validating model {model_name} of version {model_version} from temp dir" ) shutil.rmtree(temp_dest) return None # move model to dest dir model_top_dir = os.path.join(model_dir, model_name) ondisk_model_version = os.path.join(model_top_dir, model_version) logger().info( f"moving model {model_name} of version {model_version} to final dir {ondisk_model_version}" ) if os.path.isdir(ondisk_model_version): shutil.rmtree(ondisk_model_version) shutil.move(temp_dest, ondisk_model_version) return max(ts)
def find_all_cloud_models( is_dir_used: bool, models_dir: str, predictor_type: PredictorType, cloud_paths: List[str], cloud_model_names: List[str], ) -> Tuple[List[str], Dict[str, List[str]], List[str], List[List[str]], List[List[datetime.datetime]], List[str], List[str], ]: """ Get updated information on all models that are currently present on the cloud upstreams. Information on the available models, versions, last edit times, the subpaths of each model, and so on. Args: is_dir_used: Whether predictor:models:dir is used or not. models_dir: The value of predictor:models:dir in case it's present. Ignored when not required. predictor_type: The predictor type. cloud_paths: The cloud model paths as they are specified in predictor:models:path/predictor:models:paths/predictor:models:dir is used. Ignored when not required. cloud_model_names: The cloud model names as they are specified in predictor:models:paths:name when predictor:models:paths is used or the default name of the model when predictor:models:path is used. Ignored when not required. Returns: The tuple with the following elements: model_names - a list with the names of the models (i.e. bert, gpt-2, etc) and they are unique versions - a dictionary with the keys representing the model names and the values being lists of versions that each model has. For non-versioned model paths ModelVersion.NOT_PROVIDED, the list will be empty. model_paths - a list with the prefix of each model. sub_paths - a list of filepaths lists for each file of each model. timestamps - a list of timestamps lists representing the last edit time of each versioned model. bucket_providers - a list of the bucket providers for each model. Can be "s3" or "gs". bucket_names - a list of the bucket names of each model. """ # validate models stored in cloud (S3 or GS) that were specified with predictor:models:dir field if is_dir_used: if S3.is_valid_s3_path(models_dir): bucket_name, models_path = S3.deconstruct_s3_path(models_dir) client = S3(bucket_name) if GCS.is_valid_gcs_path(models_dir): bucket_name, models_path = GCS.deconstruct_gcs_path(models_dir) client = GCS(bucket_name) sub_paths, timestamps = client.search(models_path) model_paths, ooa_ids = validate_models_dir_paths( sub_paths, predictor_type, models_path) model_names = [ os.path.basename(model_path) for model_path in model_paths ] model_paths = [ model_path for model_path in model_paths if os.path.basename(model_path) in model_names ] model_paths = [ model_path + "/" * (not model_path.endswith("/")) for model_path in model_paths ] if S3.is_valid_s3_path(models_dir): bucket_providers = len(model_paths) * ["s3"] if GCS.is_valid_gcs_path(models_dir): bucket_providers = len(model_paths) * ["gs"] bucket_names = len(model_paths) * [bucket_name] sub_paths = len(model_paths) * [sub_paths] timestamps = len(model_paths) * [timestamps] # validate models stored in cloud (S3 or GS) that were specified with predictor:models:paths field if not is_dir_used: sub_paths = [] ooa_ids = [] model_paths = [] model_names = [] timestamps = [] bucket_providers = [] bucket_names = [] for idx, path in enumerate(cloud_paths): if S3.is_valid_s3_path(path): bucket_name, model_path = S3.deconstruct_s3_path(path) client = S3(bucket_name) elif GCS.is_valid_gcs_path(path): bucket_name, model_path = GCS.deconstruct_gcs_path(path) client = GCS(bucket_name) else: continue sb, model_path_ts = client.search(model_path) try: ooa_ids.append( validate_model_paths(sb, predictor_type, model_path)) except CortexException: continue model_paths.append(model_path) model_names.append(cloud_model_names[idx]) bucket_names.append(bucket_name) sub_paths += [sb] timestamps += [model_path_ts] if S3.is_valid_s3_path(path): bucket_providers.append("s3") if GCS.is_valid_gcs_path(path): bucket_providers.append("gs") # determine the detected versions for each cloud model # if the model was not versioned, then leave the version list empty versions = {} for model_path, model_name, model_ooa_ids, bucket_sub_paths in zip( model_paths, model_names, ooa_ids, sub_paths): if ModelVersion.PROVIDED not in model_ooa_ids: versions[model_name] = [] continue model_sub_paths = [ os.path.relpath(sub_path, model_path) for sub_path in bucket_sub_paths ] model_versions_paths = [ path for path in model_sub_paths if not path.startswith("../") ] model_versions = [ util.get_leftmost_part_of_path(model_version_path) for model_version_path in model_versions_paths ] model_versions = list(set(model_versions)) versions[model_name] = model_versions # pick up the max timestamp for each versioned model aux_timestamps = [] for model_path, model_name, bucket_sub_paths, sub_path_timestamps in zip( model_paths, model_names, sub_paths, timestamps): model_ts = [] if len(versions[model_name]) == 0: masks = list( map( lambda x: x.startswith(model_path + "/" * (model_path[-1] != "/")), bucket_sub_paths, )) model_ts = [max(itertools.compress(sub_path_timestamps, masks))] for version in versions[model_name]: masks = list( map( lambda x: x.startswith( os.path.join(model_path, version) + "/"), bucket_sub_paths, )) model_ts.append(max(itertools.compress(sub_path_timestamps, masks))) aux_timestamps.append(model_ts) timestamps = aux_timestamps # type: List[List[datetime.datetime]] # model_names - a list with the names of the models (i.e. bert, gpt-2, etc) and they are unique # versions - a dictionary with the keys representing the model names and the values being lists of versions that each model has. # For non-versioned model paths ModelVersion.NOT_PROVIDED, the list will be empty # model_paths - a list with the prefix of each model # sub_paths - a list of filepaths lists for each file of each model # timestamps - a list of timestamps lists representing the last edit time of each versioned model # bucket_providers - bucket providers # bucket_names - names of the buckets return model_names, versions, model_paths, sub_paths, timestamps, bucket_providers, bucket_names