docstr: The ``Learn2LearnAdapter`` is an :class:`~flash.core.adapter.Adapter` for integrating with `learn 2 learn`
  library (https://github.com/learnables/learn2learn).
  
  Args:
      task: Task to be used. This adapter should work with any Flash Classification task
      backbone: Feature extractor to be used.
      head: Predictive head.
      algorithm_cls: Algorithm class coming
          from: https://github.com/learnables/learn2learn/tree/master/learn2learn/algorithms/lightning
      ways: Number of classes conserved for generating the task.
      shots: Number of samples used for adaptation.
      meta_batch_size: Number of task to be sampled and optimized over before doing a meta optimizer step.
      queries: Number of samples used for computing the meta loss after the adaption on the `shots` samples.
      num_task: Total number of tasks to be sampled during training. If -1, a new task will always be sampled.
      epoch_length: Total number of tasks to be sampled to make an epoch.
      test_ways: Number of classes conserved for generating the validation and testing task.
      test_shots: Number of samples used for adaptation during validation and testing phase.
      test_queries: Number of samples used for computing the meta loss during validation or testing
          after the adaption on `shots` samples.
      epoch_length: Total number of tasks to be sampled to make an epoch during validation and testing phase.
      default_transforms_fn: A Callable to create the task transform.
          The callable should take the dataset, ways and shots as arguments.
      algorithm_kwargs: Keyword arguments to be provided to the algorithm class from learn2learn
docstr_tok: ["The","``","Learn2LearnAdapter","``","is","an",":","class",":","`","~flash.core.adapter.Adapter","`","for","integrating","with","`","learn","2","learn","`","library","(","https",":","\/\/github.com\/learnables\/learn2learn",")",".","Args",":","task",":","Task","to","be","used",".","This","adapter","should","work","with","any","Flash","Classification","task","backbone",":","Feature","extractor","to","be","used",".","head",":","Predictive","head",".","algorithm_cls",":","Algorithm","class","coming","from",":","https",":","\/\/github.com\/learnables\/learn2learn\/tree\/master\/learn2learn\/algorithms\/lightning","ways",":","Number","of","classes","conserved","for","generating","the","task",".","shots",":","Number","of","samples","used","for","adaptation",".","meta_batch_size",":","Number","of","task","to","be","sampled","and","optimized","over","before","doing","a","meta","optimizer","step",".","queries",":","Number","of","samples","used","for","computing","the","meta","loss","after","the","adaption","on","the","`","shots","`","samples",".","num_task",":","Total","number","of","tasks","to","be","sampled","during","training",".","If","-1",",","a","new","task","will","always","be","sampled",".","epoch_length",":","Total","number","of","tasks","to","be","sampled","to","make","an","epoch",".","test_ways",":","Number","of","classes","conserved","for","generating","the","validation","and","testing","task",".","test_shots",":","Number","of","samples","used","for","adaptation","during","validation","and","testing","phase",".","test_queries",":","Number","of","samples","used","for","computing","the","meta","loss","during","validation","or","testing","after","the","adaption","on","`","shots","`","samples",".","epoch_length",":","Total","number","of","tasks","to","be","sampled","to","make","an","epoch","during","validation","and","testing","phase",".","default_transforms_fn",":","A","Callable","to","create","the","task","transform",".","The","callable","should","take","the","dataset",",","ways","and","shots","as","arguments",".","algorithm_kwargs",":","Keyword","arguments","to","be","provided","to","the","algorithm","class","from","learn2learn"]
code: def __init__(
      self,
      backbone: nn.Module,
      head: nn.Module,
      algorithm_cls: Type[LightningModule],
      ways: int,
      shots: int,
      meta_batch_size: int,
      queries: int = 1,
      num_task: int = -1,
      epoch_length: Optional[int] = None,
      test_epoch_length: Optional[int] = None,
      test_ways: Optional[int] = None,
      test_shots: Optional[int] = None,
      test_queries: Optional[int] = None,
      test_num_task: Optional[int] = None,
      default_transforms_fn: Optional[Callable] = None,
      seed: int = 42,
      **algorithm_kwargs,
  ):
      """The ``Learn2LearnAdapter`` is an :class:`~flash.core.adapter.Adapter` for integrating with `learn 2 learn`
      library (https://github.com/learnables/learn2learn).
  
      Args:
          task: Task to be used. This adapter should work with any Flash Classification task
          backbone: Feature extractor to be used.
          head: Predictive head.
          algorithm_cls: Algorithm class coming
              from: https://github.com/learnables/learn2learn/tree/master/learn2learn/algorithms/lightning
          ways: Number of classes conserved for generating the task.
          shots: Number of samples used for adaptation.
          meta_batch_size: Number of task to be sampled and optimized over before doing a meta optimizer step.
          queries: Number of samples used for computing the meta loss after the adaption on the `shots` samples.
          num_task: Total number of tasks to be sampled during training. If -1, a new task will always be sampled.
          epoch_length: Total number of tasks to be sampled to make an epoch.
          test_ways: Number of classes conserved for generating the validation and testing task.
          test_shots: Number of samples used for adaptation during validation and testing phase.
          test_queries: Number of samples used for computing the meta loss during validation or testing
              after the adaption on `shots` samples.
          epoch_length: Total number of tasks to be sampled to make an epoch during validation and testing phase.
          default_transforms_fn: A Callable to create the task transform.
              The callable should take the dataset, ways and shots as arguments.
          algorithm_kwargs: Keyword arguments to be provided to the algorithm class from learn2learn
      """
  
      super().__init__()
  
      self.backbone = backbone
      self.head = head
      self.algorithm_cls = algorithm_cls
      self.meta_batch_size = meta_batch_size
  
      self.num_task = num_task
      self.default_transforms_fn = default_transforms_fn
      self.seed = seed
      self.epoch_length = epoch_length or meta_batch_size
  
      self.ways = ways
      self.shots = shots
      self.queries = queries
  
      self.test_ways = test_ways or ways
      self.test_shots = test_shots or shots
      self.test_queries = test_queries or queries
      self.test_num_task = test_num_task or num_task
      self.test_epoch_length = test_epoch_length or self.epoch_length
  
      params = inspect.signature(self.algorithm_cls).parameters
  
      algorithm_kwargs["train_ways"] = ways
      algorithm_kwargs["train_shots"] = shots
      algorithm_kwargs["train_queries"] = queries
  
      algorithm_kwargs["test_ways"] = self.test_ways
      algorithm_kwargs["test_shots"] = self.test_shots
      algorithm_kwargs["test_queries"] = self.test_queries
  
      if "model" in params:
          algorithm_kwargs["model"] = Model(
              backbone=backbone, head=head
          )
  
      if "features" in params:
          algorithm_kwargs["features"] = Model(
              backbone=backbone, head=None
          )
  
      if "classifier" in params:
          algorithm_kwargs["classifier"] = head
  
      self.model = self.algorithm_cls(**algorithm_kwargs)
  
      # Patch log to avoid error with learn2learn and PL 1.5
      self.model.log = functools.partial(
          self._patch_log, self.model.log
      )
  
      # this algorithm requires a special treatment
      self._algorithm_has_validated = (
          self.algorithm_cls
          != l2l.algorithms.LightningPrototypicalNetworks
      )
  
imports: import functools
  import inspect
  import os
  from collections import defaultdict
  from functools import partial
  from typing import Any, Callable, List, Optional, Type
  import torch
  from lightning_utilities.core.rank_zero import WarningCache
  from pytorch_lightning import LightningModule
  from pytorch_lightning.strategies import DataParallelStrategy, DDPSpawnStrategy, DDPStrategy
  from pytorch_lightning.trainer.states import TrainerFn
  from torch import Tensor, nn
  from torch.utils.data import DataLoader, IterableDataset, Sampler
  import flash
  from flash.core.adapter import Adapter, AdapterTask
  from flash.core.data.io.input import DataKeys, InputBase
  from flash.core.data.io.input_transform import InputTransform
  from flash.core.model import Task
  from flash.core.registry import FlashRegistry
  from flash.core.utilities.compatibility import accelerator_connector
  from flash.core.utilities.imports import _LEARN2LEARN_AVAILABLE
  from flash.core.utilities.providers import _LEARN2LEARN
  from flash.core.utilities.stability import beta
  from flash.core.utilities.url_error import catch_url_error
  from flash.image.classification.integrations.learn2learn import TaskDataParallel, TaskDistributedDataParallel
label_desc: Use image node_id 1 to create a new Learn2LearnAdapter object from inherited base classes: Adapter with example: obj = Learn2LearnAdapter(backbone, head, algorithm_cls, ways, shots, meta_batch_size, queries, num_task, epoch_length, test_epoch_length, test_ways, test_shots, test_queries, test_num_task, default_transforms_fn, seed)