code: def sample_candidates(task, task_name, model_name):
      """Randomly sample candidates for a task and save the candidates in the given directory.
  
      Parameters
      ----------
      task : IRModule
          The initial ir module used for generating the search space.
      task_name : str
          The name of the task.
      model_name : str
          The name of the model.
  
      Returns
      -------
      None
      """
      candidate_path = os.path.join(
          args.candidate_cache_dir,
          model_name,
          task_name + "_candidates.json",
      )
      workload_path = os.path.join(
          args.candidate_cache_dir,
          model_name,
          task_name + "_workload.json",
      )
      database = ms.database.JSONDatabase(
          path_workload=workload_path,
          path_tuning_record=candidate_path,
      )
      sample_init_population = tvm.get_global_func(
          "meta_schedule.SearchStrategyEvolutionarySearchSampleInitPopulation"
      )
      evolve_with_cost_model = tvm.get_global_func(
          "meta_schedule.SearchStrategyEvolutionarySearchEvolveWithCostModel"
      )
      strategy = ms.search_strategy.EvolutionarySearch(
          init_measured_ratio=0.0
      )
      target = Target(args.target)
      context = ms.TuneContext(
          mod=task,
          target=target,
          space_generator="post-order-apply",
          search_strategy=strategy,
          task_name=task_name,
      )
      context.initialize()
      context.pre_tuning(
          max_trials=args.max_trials_per_task,
          num_trials_per_iter=args.num_trials_per_iter,
          design_spaces=context.generate_design_space(),
          database=database,
          cost_model=ms.cost_model.RandomModel(),  # type: ignore
      )
  
      all_states: List[tvm.tir.Schedule] = []
      num_retry, itr = 0, 0
      states = sample_init_population(
          strategy, args.init_population_size
      )
      while (
          len(all_states) < args.num_samples_per_task
          and num_retry < args.max_retry_per_task
      ):
          states = evolve_with_cost_model(strategy, states, len(states))
          all_states += states
          if len(states) == 0:
              states = sample_init_population(
                  strategy, args.init_population_size
              )
              num_retry += 1
          else:
              num_retry = 0
          print(
              f"iter: {itr}, number of states sampled: {len(all_states)}"
          )
          itr += 1
      all_states = all_states[: args.num_samples_per_task]
  
      workload = ms.database.Workload(context.mod)
      database.commit_workload(context.mod)
      for state in all_states:
          database.commit_tuning_record(
              ms.database.TuningRecord(state.trace, workload)
          )
  
file_path: tvm/python/tvm/meta_schedule/testing/dataset_sample_candidates.py
imports: import argparse
  import glob
  import json
  import os
  from typing import List
  from tqdm import tqdm
  import tvm
  from tvm import meta_schedule
  from tvm.ir import load_json
  from tvm.target import Target
label_desc: Use image node_id 2 for calling a global function with example usage: sample_candidates(task, task_name, model_name) without return types