class_docstr: DJL build logic for ModelBuilder()
docstr: Placeholder docstring
docstr_tok: ["Placeholder","docstring"]
code: def _tune_for_hf_djl(self, max_tuning_duration: int = 1800):
      """Placeholder docstring"""
      if self.mode != Mode.LOCAL_CONTAINER:
          logger.warning(
              "Tuning is only a %s capability. Returning original model.",
              Mode.LOCAL_CONTAINER,
          )
          return self.pysdk_model
  
      self.overwrite_props_from_file = False
  
      admissible_tensor_parallel_degrees = (
          _get_admissible_tensor_parallel_degrees(self.hf_model_config)
      )
      admissible_dtypes = _get_admissible_dtypes()
  
      benchmark_results = {}
      best_tuned_combination = None
      timeout = datetime.now() + timedelta(seconds=max_tuning_duration)
      for tensor_parallel_degree in admissible_tensor_parallel_degrees:
          if datetime.now() > timeout:
              logger.info(
                  "Max tuning duration reached. Tuning stopped."
              )
              break
  
          dtype_passes = 0
          for dtype in admissible_dtypes:
              logger.info(
                  "Trying tensor parallel degree: %s, dtype: %s...",
                  tensor_parallel_degree,
                  dtype,
              )
  
              self._default_tensor_parallel_degree = (
                  tensor_parallel_degree
              )
              self._default_data_type = dtype
              self.pysdk_model = self._create_djl_model()
  
              try:
                  predictor = self.pysdk_model.deploy(
                      model_data_download_timeout=max_tuning_duration
                  )
  
                  (
                      avg_latency,
                      p90,
                      avg_tokens_per_second,
                  ) = _serial_benchmark(
                      predictor, self.schema_builder.sample_input
                  )
                  (
                      throughput_per_second,
                      standard_deviation,
                  ) = _concurrent_benchmark(
                      predictor, self.schema_builder.sample_input
                  )
  
                  serving_properties = (
                      self.pysdk_model.generate_serving_properties()
                  )
                  logger.info(
                      "Average latency: %s, throughput/s: %s for configuration: %s",
                      avg_latency,
                      throughput_per_second,
                      serving_properties,
                  )
                  benchmark_results[avg_latency] = [
                      serving_properties,
                      p90,
                      avg_tokens_per_second,
                      throughput_per_second,
                      standard_deviation,
                  ]
  
                  if not best_tuned_combination:
                      best_tuned_combination = [
                          avg_latency,
                          tensor_parallel_degree,
                          dtype,
                          p90,
                          avg_tokens_per_second,
                          throughput_per_second,
                          standard_deviation,
                      ]
                  else:
                      tuned_configuration = [
                          avg_latency,
                          tensor_parallel_degree,
                          dtype,
                          p90,
                          avg_tokens_per_second,
                          throughput_per_second,
                          standard_deviation,
                      ]
                      if _more_performant(
                          best_tuned_combination, tuned_configuration
                      ):
                          best_tuned_combination = tuned_configuration
              except LocalDeepPingException as e:
                  logger.warning(
                      "Deployment unsuccessful with tensor parallel degree: %s. dtype: %s. "
                      "Failed to invoke the model server: %s",
                      tensor_parallel_degree,
                      dtype,
                      str(e),
                  )
                  break
              except LocalModelOutOfMemoryException as e:
                  logger.warning(
                      "Deployment unsuccessful with tensor parallel degree: %s, dtype: %s. "
                      "Out of memory when loading the model: %s",
                      tensor_parallel_degree,
                      dtype,
                      str(e),
                  )
                  break
              except LocalModelInvocationException as e:
                  logger.warning(
                      "Deployment unsuccessful with tensor parallel degree: %s, dtype: %s. "
                      "Failed to invoke the model server: %s"
                      "Please check that model server configurations are as expected "
                      "(Ex. serialization, deserialization, content_type, accept).",
                      tensor_parallel_degree,
                      dtype,
                      str(e),
                  )
                  break
              except LocalModelLoadException as e:
                  logger.warning(
                      "Deployment unsuccessful with tensor parallel degree: %s, dtype: %s. "
                      "Failed to load the model: %s.",
                      tensor_parallel_degree,
                      dtype,
                      str(e),
                  )
                  break
              except Exception:  # pylint: disable=W0703
                  logger.exception(
                      "Deployment unsuccessful with tensor parallel degree: %s, dtype: %s "
                      "with uncovered exception",
                      tensor_parallel_degree,
                      dtype,
                  )
                  break
              dtype_passes += 1
          if dtype_passes == 0:
              logger.info(
                  "Lowest admissible tensor parallel degree: %s and highest dtype: "
                  "%s combination has been attempted. Tuning stopped.",
                  tensor_parallel_degree,
                  dtype,
              )
              break
  
      if best_tuned_combination:
          self._default_tensor_parallel_degree = best_tuned_combination[
              1
          ]
          self._default_data_type = best_tuned_combination[2]
          self.pysdk_model = self._create_djl_model()
  
          _pretty_print_results(benchmark_results)
          logger.info(
              "Model Configuration: %s was most performant with avg latency: %s, "
              "p90 latency: %s, average tokens per second: %s, throughput/s: %s, "
              "standard deviation of request %s",
              self.pysdk_model.generate_serving_properties(),
              best_tuned_combination[0],
              best_tuned_combination[3],
              best_tuned_combination[4],
              best_tuned_combination[5],
              best_tuned_combination[6],
          )
      else:
          (
              self._default_tensor_parallel_degree,
              self._default_data_type,
              _,
              self._default_max_tokens,
              self._default_max_new_tokens,
          ) = _set_serve_properties(
              self.hf_model_config, self.schema_builder
          )
          self.schema_builder.sample_input["parameters"][
              "max_new_tokens"
          ] = self._default_max_new_tokens
          self.pysdk_model = self._create_djl_model()
  
          logger.debug(
              "Failed to gather any tuning results. "
              "Please inspect the stack trace emitted from live logging for more details. "
              "Falling back to default serving.properties: %s",
              self.pysdk_model.generate_serving_properties(),
          )
  
      prepare_for_djl_serving(
          model_path=self.model_path,
          model=self.pysdk_model,
          dependencies=self.dependencies,
          overwrite_props_from_file=self.overwrite_props_from_file,
      )
      self.overwrite_props_from_file = True
  
      return self.pysdk_model
  
imports: from __future__ import absolute_import
  import logging
  from typing import Type
  from abc import ABC, abstractmethod
  from pathlib import Path
  from datetime import datetime, timedelta
  from sagemaker.model import Model
  from sagemaker.serve.utils.exceptions import LocalDeepPingException, LocalModelLoadException, LocalModelOutOfMemoryException, LocalModelInvocationException
  from sagemaker.serve.utils.tuning import _serial_benchmark, _concurrent_benchmark, _more_performant, _pretty_print_results
  from sagemaker.serve.model_server.djl_serving.utils import _auto_detect_engine, _set_serve_properties, _get_admissible_tensor_parallel_degrees, _get_admissible_dtypes, _get_default_tensor_parallel_degree
  from sagemaker.serve.utils.local_hardware import _get_nb_instance, _get_ram_usage_mb, _get_gpu_info, _get_gpu_info_fallback
  from sagemaker.serve.model_server.djl_serving.prepare import prepare_for_djl_serving, _create_dir_structure
  from sagemaker.serve.utils.predictors import DjlLocalModePredictor
  from sagemaker.serve.utils.types import ModelServer, _DjlEngine
  from sagemaker.serve.mode.function_pointers import Mode
  from sagemaker.serve.utils.telemetry_logger import _capture_telemetry
  from sagemaker.djl_inference.model import DeepSpeedModel, FasterTransformerModel, HuggingFaceAccelerateModel
  from sagemaker.base_predictor import PredictorBase
label_desc: Use image node_id 9 for calling the DJL obj's underlying member method code with example usage: obj._tune_for_hf_djl(max_tuning_duration) and returns: self, self