code: def bert_score(
      preds: Union[str, Sequence[str], Dict[str, Tensor]],
      target: Union[str, Sequence[str], Dict[str, Tensor]],
      model_name_or_path: Optional[str] = None,
      num_layers: Optional[int] = None,
      all_layers: bool = False,
      model: Optional[Module] = None,
      user_tokenizer: Any = None,
      user_forward_fn: Optional[
          Callable[[Module, Dict[str, Tensor]], Tensor]
      ] = None,
      verbose: bool = False,
      idf: bool = False,
      device: Optional[Union[str, torch.device]] = None,
      max_length: int = 512,
      batch_size: int = 64,
      num_threads: int = 0,
      return_hash: bool = False,
      lang: str = "en",
      rescale_with_baseline: bool = False,
      baseline_path: Optional[str] = None,
      baseline_url: Optional[str] = None,
  ) -> Dict[str, Union[Tensor, List[float], str]]:
      """`Bert_score Evaluating Text Generation`_ for text similirity matching.
  
      This metric leverages the pre-trained contextual embeddings from BERT and matches words in candidate and reference
      sentences by cosine similarity. It has been shown to correlate with human judgment on sentence-level and
      system-level evaluation. Moreover, BERTScore computes precision, recall, and F1 measure, which can be useful for
      evaluating different language generation tasks.
  
      This implementation follows the original implementation from `BERT_score`_.
  
      Args:
          preds: Either an iterable of predicted sentences or a ``Dict[input_ids, attention_mask]``.
          target: Either an iterable of target sentences or a  ``Dict[input_ids, attention_mask]``.
          model_name_or_path: A name or a model path used to load ``transformers`` pretrained model.
          num_layers: A layer of representation to use.
          all_layers:
              An indication of whether the representation from all model's layers should be used.
              If ``all_layers = True``, the argument ``num_layers`` is ignored.
          model: A user's own model.
          user_tokenizer:
              A user's own tokenizer used with the own model. This must be an instance with the ``__call__`` method.
              This method must take an iterable of sentences (``List[str]``) and must return a python dictionary
              containing ``"input_ids"`` and ``"attention_mask"`` represented by :class:`~torch.Tensor`.
              It is up to the user's model of whether ``"input_ids"`` is a :class:`~torch.Tensor` of input ids
              or embedding vectors. his tokenizer must prepend an equivalent of ``[CLS]`` token and append an equivalent
              of ``[SEP]`` token as `transformers` tokenizer does.
          user_forward_fn:
              A user's own forward function used in a combination with ``user_model``.
              This function must take ``user_model`` and a python dictionary of containing ``"input_ids"``
              and ``"attention_mask"`` represented by :class:`~torch.Tensor` as an input and return the model's output
              represented by the single :class:`~torch.Tensor`.
          verbose: An indication of whether a progress bar to be displayed during the embeddings' calculation.
          idf: An indication of whether normalization using inverse document frequencies should be used.
          device: A device to be used for calculation.
          max_length: A maximum length of input sequences. Sequences longer than ``max_length`` are to be trimmed.
          batch_size: A batch size used for model processing.
          num_threads: A number of threads to use for a dataloader.
          return_hash: An indication of whether the correspodning ``hash_code`` should be returned.
          lang: A language of input sentences. It is used when the scores are rescaled with a baseline.
          rescale_with_baseline:
              An indication of whether bertscore should be rescaled with a pre-computed baseline.
              When a pretrained model from ``transformers`` model is used, the corresponding baseline is downloaded
              from the original ``bert-score`` package from `BERT_score`_ if available.
              In other cases, please specify a path to the baseline csv/tsv file, which must follow the formatting
              of the files from `BERT_score`_
          baseline_path: A path to the user's own local csv/tsv file with the baseline scale.
          baseline_url: A url path to the user's own  csv/tsv file with the baseline scale.
  
      Returns:
          Python dictionary containing the keys ``precision``, ``recall`` and ``f1`` with corresponding values.
  
      Raises:
          ValueError:
              If ``len(preds) != len(target)``.
          ModuleNotFoundError:
              If `tqdm` package is required and not installed.
          ModuleNotFoundError:
              If ``transformers`` package is required and not installed.
          ValueError:
              If ``num_layer`` is larger than the number of the model layers.
          ValueError:
              If invalid input is provided.
  
      Example:
          >>> from pprint import pprint
          >>> from torchmetrics.functional.text.bert import bert_score
          >>> preds = ["hello there", "general kenobi"]
          >>> target = ["hello there", "master kenobi"]
          >>> pprint(bert_score(preds, target))
          {'f1': tensor([1.0000, 0.9961]), 'precision': tensor([1.0000, 0.9961]), 'recall': tensor([1.0000, 0.9961])}
  
      """
      if len(preds) != len(target):
          raise ValueError(
              "Number of predicted and reference sententes must be the same!"
          )
      if not isinstance(
          preds, (str, list, dict)
      ):  # dict for BERTScore class compute call
          preds = list(preds)
      if not isinstance(
          target, (str, list, dict)
      ):  # dict for BERTScore class compute call
          target = list(target)
  
      if verbose and (not _TQDM_AVAILABLE):
          raise ModuleNotFoundError(
              "An argument `verbose = True` requires `tqdm` package be installed. Install with `pip install tqdm`."
          )
  
      if model is None:
          if not _TRANSFORMERS_GREATER_EQUAL_4_4:
              raise ModuleNotFoundError(
                  "`bert_score` metric with default models requires `transformers` package be installed."
                  " Either install with `pip install transformers>=4.4` or `pip install torchmetrics[text]`."
              )
          if model_name_or_path is None:
              rank_zero_warn(
                  "The argument `model_name_or_path` was not specified while it is required when default"
                  " `transformers` model are used."
                  f"It is, therefore, used the default recommended model - {_DEFAULT_MODEL}."
              )
          tokenizer = AutoTokenizer.from_pretrained(
              model_name_or_path or _DEFAULT_MODEL
          )
          model = AutoModel.from_pretrained(
              model_name_or_path or _DEFAULT_MODEL
          )
      else:
          tokenizer = user_tokenizer
      model.eval()
      model.to(device)
  
      try:
          if num_layers and num_layers > model.config.num_hidden_layers:
              raise ValueError(
                  f"num_layers={num_layers} is forbidden for {model_name_or_path}."
                  f" Please use num_layers <= {model.config.num_hidden_layers}"
              )
      except AttributeError:
          rank_zero_warn(
              "It was not possible to retrieve the parameter `num_layers` from the model specification."
          )
  
      _are_empty_lists = all(
          isinstance(text, list) and len(text) == 0
          for text in (preds, target)
      )
      _are_valid_lists = all(
          isinstance(text, list)
          and len(text) > 0
          and isinstance(text[0], str)
          for text in (preds, target)
      )
      _are_valid_tensors = all(
          isinstance(text, dict)
          and isinstance(text["input_ids"], Tensor)
          for text in (preds, target)
      )
      if _are_empty_lists:
          rank_zero_warn("Predictions and references are empty.")
          output_dict: Dict[str, Union[Tensor, List[float], str]] = {
              "precision": [0.0],
              "recall": [0.0],
              "f1": [0.0],
          }
          if return_hash:
              output_dict.update(
                  {
                      "hash": _get_hash(
                          model_name_or_path, num_layers, idf
                      )
                  }
              )
          return output_dict
  
      # Load baselines if needed
      baseline = (
          _load_baseline(
              lang, model_name_or_path, baseline_path, baseline_url
          )
          if rescale_with_baseline
          else None
      )
  
      # We ignore mypy typing below as the proper typing is ensured by conditions above, only mypy cannot infer that.
      if _are_valid_lists:
          target_dataset = TextDataset(target, tokenizer, max_length, idf=idf)  # type: ignore
          preds_dataset = TextDataset(
              preds,  # type: ignore
              tokenizer,
              max_length,
              idf=idf,
              tokens_idf=target_dataset.tokens_idf,
          )
      elif _are_valid_tensors:
          target_dataset = TokenizedDataset(**target, idf=idf)  # type: ignore
          preds_dataset = TokenizedDataset(**preds, idf=idf, tokens_idf=target_dataset.tokens_idf)  # type: ignore
      else:
          raise ValueError("Invalid input provided.")
  
      target_loader = DataLoader(
          target_dataset, batch_size=batch_size, num_workers=num_threads
      )
      preds_loader = DataLoader(
          preds_dataset, batch_size=batch_size, num_workers=num_threads
      )
  
      (
          target_embeddings,
          target_idf_scale,
      ) = _get_embeddings_and_idf_scale(
          target_loader,
          target_dataset.max_length,
          model,
          device,
          num_layers,
          all_layers,
          idf,
          verbose,
          user_forward_fn,
      )
      preds_embeddings, preds_idf_scale = _get_embeddings_and_idf_scale(
          preds_loader,
          preds_dataset.max_length,
          model,
          device,
          num_layers,
          all_layers,
          idf,
          verbose,
          user_forward_fn,
      )
  
      precision, recall, f1_score = _get_precision_recall_f1(
          preds_embeddings,
          target_embeddings,
          preds_idf_scale,
          target_idf_scale,
      )
      # Sort predictions
      if len(precision.shape) == 1:  # i.e. when all_layers = False
          precision = precision[preds_loader.dataset.sorting_indices]
          recall = recall[preds_loader.dataset.sorting_indices]
          f1_score = f1_score[preds_loader.dataset.sorting_indices]
      elif len(precision.shape) == 2:  # i.e. when all_layers = True
          precision = precision[:, preds_loader.dataset.sorting_indices]
          recall = recall[:, preds_loader.dataset.sorting_indices]
          f1_score = f1_score[:, preds_loader.dataset.sorting_indices]
  
      if baseline is not None:
          precision, recall, f1_score = _rescale_metrics_with_baseline(
              precision,
              recall,
              f1_score,
              baseline,
              num_layers,
              all_layers,
          )
  
      output_dict = {
          "precision": precision,
          "recall": recall,
          "f1": f1_score,
      }
      if return_hash:
          output_dict.update(
              {"hash": _get_hash(model_name_or_path, num_layers, idf)}
          )
      return output_dict
  
file_path: torchmetrics/src/torchmetrics/functional/text/bert.py
imports: import csv
  import urllib
  from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
  import torch
  from torch import Tensor
  from torch.nn import Module
  from torch.utils.data import DataLoader
  from torchmetrics.functional.text.helper_embedding_metric import TextDataset, TokenizedDataset, _check_shape_of_model_output, _get_progress_bar, _input_data_collator, _output_data_collator, _process_attention_mask_for_special_tokens
  from torchmetrics.utilities import rank_zero_warn
  from torchmetrics.utilities.checks import _SKIP_SLOW_DOCTEST, _try_proceed_with_timeout
  from torchmetrics.utilities.imports import _TQDM_AVAILABLE, _TRANSFORMERS_GREATER_EQUAL_4_4
label_desc: Use image node_id 9 for calling a global function with example usage: bert_score(preds, target, model_name_or_path, num_layers, all_layers, model, user_tokenizer, user_forward_fn, verbose, idf, device, max_length, batch_size, num_threads, return_hash, lang, rescale_with_baseline, baseline_path, baseline_url) and returns: output_dict, output_dict