
    +h                     b   S SK Jr  S SKJrJrJrJrJrJr  S SK	r
S SKrS SKJr  S SKJr  S SKJrJr  SSKJr  SS	KJrJr  SS
KJrJr  SSKJrJrJrJr  SSK J!r!  SSK"J#r#  SSK$J%r%  \" 5       (       a  S SK&J's  J(r)  Sr*OSr*\RV                  " \,5      r-Sr.\ " S S\5      5       r/ " S S\#5      r0g)    )	dataclass)AnyDictListOptionalTupleUnionN)Image)tqdm)CLIPTextModelCLIPTokenizer   )PipelineImageInput)AutoencoderKLUNet2DConditionModel)DDIMSchedulerLCMScheduler)
BaseOutputis_torch_xla_availableloggingreplace_example_docstring)randn_tensor   )DiffusionPipeline   )MarigoldImageProcessorTFa  
Examples:
```py
>>> import diffusers
>>> import torch

>>> pipe = diffusers.MarigoldIntrinsicsPipeline.from_pretrained(
...     "prs-eth/marigold-iid-appearance-v1-1", variant="fp16", torch_dtype=torch.float16
... ).to("cuda")

>>> image = diffusers.utils.load_image("https://marigoldmonodepth.github.io/images/einstein.jpg")
>>> intrinsics = pipe(image)

>>> vis = pipe.image_processor.visualize_intrinsics(intrinsics.prediction, pipe.target_properties)
>>> vis[0]["albedo"].save("einstein_albedo.png")
>>> vis[0]["roughness"].save("einstein_roughness.png")
>>> vis[0]["metallicity"].save("einstein_metallicity.png")
```
```py
>>> import diffusers
>>> import torch

>>> pipe = diffusers.MarigoldIntrinsicsPipeline.from_pretrained(
...     "prs-eth/marigold-iid-lighting-v1-1", variant="fp16", torch_dtype=torch.float16
... ).to("cuda")

>>> image = diffusers.utils.load_image("https://marigoldmonodepth.github.io/images/einstein.jpg")
>>> intrinsics = pipe(image)

>>> vis = pipe.image_processor.visualize_intrinsics(intrinsics.prediction, pipe.target_properties)
>>> vis[0]["albedo"].save("einstein_albedo.png")
>>> vis[0]["shading"].save("einstein_shading.png")
>>> vis[0]["residual"].save("einstein_residual.png")
```
c                       \ rS rSr% Sr\\R                  \R                  4   \
S'   \S\R                  \R                  4   \
S'   \S\R                  4   \
S'   Srg)MarigoldIntrinsicsOutput_   aq  
Output class for Marigold Intrinsic Image Decomposition pipeline.

Args:
    prediction (`np.ndarray`, `torch.Tensor`):
        Predicted image intrinsics with values in the range [0, 1]. The shape is $(numimages * numtargets)  imes 3
            imes height     imes width$ for `torch.Tensor` or $(numimages * numtargets)     imes height     imes width
            imes 3$ for `np.ndarray`, where `numtargets` corresponds to the number of predicted target modalities of
        the intrinsic image decomposition.
    uncertainty (`None`, `np.ndarray`, `torch.Tensor`):
        Uncertainty maps computed from the ensemble, with values in the range [0, 1]. The shape is $(numimages *
        numtargets)         imes 3  imes height     imes width$ for `torch.Tensor` or $(numimages * numtargets)     imes
        height      imes width      imes 3$ for `np.ndarray`.
    latent (`None`, `torch.Tensor`):
        Latent features corresponding to the predictions, compatible with the `latents` argument of the pipeline.
        The shape is $(numimages * numensemble)     imes (numtargets * 4)   imes latentheight       imes latentwidth$.

predictionNuncertaintylatent )__name__
__module____qualname____firstlineno____doc__r	   npndarraytorchTensor__annotations____static_attributes__r#       s/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/marigold/pipeline_marigold_intrinsics.pyr   r   _   sO    $ bjj%,,.//tRZZ566$$%%r/   r   c            !         ^  \ rS rSrSrSrSr    S*S\S\S\	\
\4   S\S	\S
\\   S\\\\4      S\\   S\\   4U 4S jjjr\S 5       rS\S\S\S\S\S\S\S\\\\4      S\\R0                     S\\	\R2                  \\R2                     4      S\S\S\4S jr\R:                  R<                  S+S j5       r\R@                  " 5       \!" \"5                    S,S\S\\   S\S\\   S\S\S\S\S\\\\4      S\\	\R0                  \\R0                     4      S\\	\R2                  \\R2                     4      S\S\S \S!\4S" jj5       5       r#S\R0                  S\\R0                     S\\R2                     S\S\S\$\R0                  \R0                  4   4S# jr%S$\R0                  S\R0                  4S% jr&\'  S-S&\R0                  S\S'\S\$\R0                  \\R0                     4   4S( jj5       r(S)r)U =r*$ ).MarigoldIntrinsicsPipelinex   a=	  
Pipeline for Intrinsic Image Decomposition (IID) using the Marigold method:
https://marigoldcomputervision.github.io.

This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods the
library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)

Args:
    unet (`UNet2DConditionModel`):
        Conditional U-Net to denoise the targets latent, conditioned on image latent.
    vae (`AutoencoderKL`):
        Variational Auto-Encoder (VAE) Model to encode and decode images and predictions to and from latent
        representations.
    scheduler (`DDIMScheduler` or `LCMScheduler`):
        A scheduler to be used in combination with `unet` to denoise the encoded image latents.
    text_encoder (`CLIPTextModel`):
        Text-encoder, for empty text embedding.
    tokenizer (`CLIPTokenizer`):
        CLIP tokenizer.
    prediction_type (`str`, *optional*):
        Type of predictions made by the model.
    target_properties (`Dict[str, Any]`, *optional*):
        Properties of the predicted modalities, such as `target_names`, a `List[str]` used to define the number,
        order and names of the predicted modalities, and any other metadata that may be required to interpret the
        predictions.
    default_denoising_steps (`int`, *optional*):
        The minimum number of denoising diffusion steps that are required to produce a prediction of reasonable
        quality with the given model. This value must be set in the model config. When the pipeline is called
        without explicitly setting `num_inference_steps`, the default value is used. This is required to ensure
        reasonable results with various model flavors compatible with the pipeline, such as those relying on very
        short denoising schedules (`LCMScheduler`) and those with full diffusion schedules (`DDIMScheduler`).
    default_processing_resolution (`int`, *optional*):
        The recommended value of the `processing_resolution` parameter of the pipeline. This value must be set in
        the model config. When the pipeline is called without explicitly setting `processing_resolution`, the
        default value is used. This is required to ensure reasonable results with various model flavors trained
        with varying optimal processing resolution values.
ztext_encoder->unet->vae)
intrinsicsunetvae	schedulertext_encoder	tokenizerprediction_typetarget_propertiesdefault_denoising_stepsdefault_processing_resolutionc
                   > [         T
U ]  5         X`R                  ;  a&  [        R	                  SU SU R                   S35        U R                  UUUUUS9  U R                  UUUU	S9  [        U SS 5      (       a/  S[        U R                  R                  R                  5      S-
  -  OS	U l        Xpl        Xl        Xl        S U l        [#        U R                  S
9U l        g )Nz*Potentially unsupported `prediction_type='z&'`; values supported by the pipeline: .)r5   r6   r7   r8   r9   )r:   r;   r<   r=   r6   r   r      )vae_scale_factor)super__init__supported_prediction_typesloggerwarningregister_modulesregister_to_configgetattrlenr6   configblock_out_channelsrA   r;   r<   r=   empty_text_embeddingr   image_processor)selfr5   r6   r7   r8   r9   r:   r;   r<   r=   	__class__s             r0   rC   #MarigoldIntrinsicsPipeline.__init__   s     	"A"AANN<_<MMs22316
 	% 	 	
 	+/$;*G	 	  	
 W^^bdikoVpVpc$((//*L*L&MPQ&Q Rvw!2'>$-J*$(!5tG\G\]r/   c                     U R                   R                  R                  U R                  R                  R                  -  $ )N)r5   rK   out_channelsr6   latent_channels)rO   s    r0   	n_targets$MarigoldIntrinsicsPipeline.n_targets   s+    yy,,0O0OOOr/   imagenum_inference_stepsensemble_sizeprocessing_resolutionresample_method_inputresample_method_output
batch_sizeensembling_kwargslatents	generatoroutput_typeoutput_uncertaintyreturnc           
      H	  ^
 S[        U R                  R                  R                  5      S-
  -  nXR                  :w  a  [        SU R                   SU S35      eUc  [        S5      eUS:  a  [        S5      eUS:  a  [        S5      eUS:X  a  [        R                  S	5        US:X  a  U(       a  [        S
5      eUc  [        S5      eUS:  a  [        S5      eX@R                  -  S:w  a  [        SU R                   S35      eUS;  a  [        S5      eUS;  a  [        S5      eUS:  a  [        S5      eUS;  a  [        S5      eU	b  T
b  [        S5      eUb:  [        U[        5      (       d  [        S5      eSU;   a  US   S;  a  [        S5      eSnSu  nn[        U[        5      (       d  U/n[        U5       GH  u  nn[        U[        R                  5      (       d  [        R                  " U5      (       a`  UR                   S;  a  [        SU SUR"                   S35      eUR"                  SS  u  nnSnUR                   S :X  a  UR"                  S   nOL[        U[$        R$                  5      (       a  UR&                  u  nnSnO[        S!U S"[)        U5       S35      eUc  UUnnO"UU4UU4:w  a  [        S#U S$UU4 S%UU4 35      eUU-  nGM     U	Gb%  [        R                  " U	5      (       d  [        S&5      eU	R+                  5       S :w  a  [        S'U	R"                   S35      eUS:  a=  [-        UU5      nUU-  U-  nX-  U-  nUS:X  d  US:X  a  [        S(U S)U S*35      eUUnnXR                  -   S-
  U R                  -  nUU R                  -   S-
  U R                  -  nX-  U R.                  R                  R0                  UU4nU	R"                  U:w  a  [        S+U	R"                   S,U S35      eT
b  [        T
[        5      (       aD  [        T
5      X-  :w  a  [        S-5      e[3        U
4S. jT
 5       5      (       d  [        S/5      e U$ [        T
[        R4                  5      (       d  [        S0[)        T
5       S35      eU$ )1Nr   r   z/`vae_scale_factor` computed at initialization (z) differs from the actual one (z).zW`num_inference_steps` is not specified and could not be resolved from the model config.z'`num_inference_steps` must be positive.z!`ensemble_size` must be positive.zk`ensemble_size` == 2 results are similar to no ensembling (1); consider increasing the value to at least 3.zpComputing uncertainty by setting `output_uncertainty=True` also requires setting `ensemble_size` greater than 1.zY`processing_resolution` is not specified and could not be resolved from the model config.r   zx`processing_resolution` must be non-negative: 0 for native resolution, or any positive value for downsampled processing.z.`processing_resolution` must be a multiple of r?   )nearestznearest-exactbilinearbicubicareazy`resample_method_input` takes string values compatible with PIL library: nearest, nearest-exact, bilinear, bicubic, area.zz`resample_method_output` takes string values compatible with PIL library: nearest, nearest-exact, bilinear, bicubic, area.z`batch_size` must be positive.)ptr)   z*`output_type` must be one of `pt` or `np`.z2`latents` and `generator` cannot be used together.z)`ensembling_kwargs` must be a dictionary.	reductionmedianmeanzF`ensembling_kwargs['reduction']` can be either `'median'` or `'mean'`.)NN)r   r      z`image[z(]` has unsupported dimensions or shape: rn   zUnsupported `image[z	]` type: zInput `image[z]` has incompatible dimensions z with the previous images z!`latents` must be a torch.Tensor.z/`latents` has unsupported dimensions or shape: z*Extreme aspect ratio of the input image: [z x ]z`latents` has unexpected shape=z
 expected=z^The number of generators must match the total number of ensemble members for all input images.c              3      >#    U  H4  oR                   R                  TS    R                   R                  :H  v   M6     g7f)r   N)devicetype).0gr`   s     r0   	<genexpr>:MarigoldIntrinsicsPipeline.check_inputs.<locals>.<genexpr>M  s,     Xi88==IaL,?,?,D,DDis   <?z;`generator` device placement is not consistent in the list.zUnsupported generator type: )rJ   r6   rK   rL   rA   
ValueErrorrE   rF   
isinstancedictlist	enumerater)   r*   r+   	is_tensorndimshaper
   sizers   dimmaxr5   rS   all	Generator)rO   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   actual_vae_scale_factor
num_imagesWHiimgH_iW_iN_imax_orignew_Hnew_Wwhshape_expecteds             `                 r0   check_inputs'MarigoldIntrinsicsPipeline.check_inputs   s4    #$DHHOO,N,N(ORS(S"T"&;&;;A$BWBWAXXw  yP  xQ  QS  T  &vww"FGG1@AAANN? A"4"  !(k  !1$*  !#8#88A=MdNcNcMddefgg (ccC  ")ddC  >=>>l*IJJ9#8QRR(/66 !LMM//4Ek4RZl4l !ijj 
1%&&GE&FAs#rzz**eooc.B.B889,$wqc1YZ]ZcZcYdde%fgg99RS>S88q=))A,CC--88S #6qc49+Q!OPPyC11QC:% #A3&EsCj\Qkmnpqlrkst  #J' ', ??7++ !DEE{{}! #RSZS`S`Raab!cdd$q(q!911X=1X=A:!$'QRSQTTWXYWZZ[%\]]e1***Q.43H3HHAT***Q.43H3HHA(8$)):J:J:W:WYZ\]^N}}. #B7==/Q[\j[kkl!mnn  )T**y>Z%??$x  XiXXX$%bcc Y
   	5??;; #?Y?PPQ!RSSr/   c                    [        U S5      (       d  0 U l        OA[        U R                  [        5      (       d"  [	        S[        U R                  5       S35      e[        S0 U R                  D6nUR                  SU5      US'   UR                  SU5      US'   Ub  [        U40 UD6$ Ub  [        SSU0UD6$ [	        S5      e)	N_progress_bar_configz=`self._progress_bar_config` should be of type `dict`, but is r?   descleavetotalz/Either `total` or `iterable` has to be defined.r#   )hasattrr   ry   rz   rx   rs   getr   )rO   iterabler   r   r   progress_bar_configs         r0   progress_bar'MarigoldIntrinsicsPipeline.progress_barT  s    t344(*D%D55t<<OPTUYUnUnPoOppqr  #?T%>%>?&9&=&=fd&KF#':'>'>w'NG$8$788;e;':;;NOOr/   match_input_resolutionoutput_latentreturn_dictc                 	   U R                   nU R                  nUc  U R                  nUc  U R                  nU R	                  UUUUUUUU	U
UUU5      nU R
                  c]  SnU R                  USU R                  R                  SSS9nUR                  R                  U5      nU R                  U5      S   U l        U R                  R                  XUUU5      u  nnnU R                  XXU5      u  nnAU R
                  R                  UUS9R                  US	S	5      n/ nU R                  [!        SUU-  U5      SS
S9 H  nUUUU-    nUUUU-    nUR"                  S   nUSU n U R$                  R'                  UUS9  U R                  U R$                  R(                  SSS9 Hw  n![*        R,                  " UU/S	S9n"U R/                  U"U!U SS9S   n#U R$                  R1                  U#U!UUS9R2                  n[4        (       d  Mb  [6        R8                  " 5         My     UR;                  U5        M     [*        R,                  " USS9nAAAAAA A"A#UR<                  " UU-  U R>                  -  U R@                  RB                  RD                  /UR"                  SS Q76 n$[*        R,                  " [!        SU$R"                  S   U5       Vs/ s H  nU RG                  U$UUU-    5      PM     snSS9n%A$U(       d  SnU R                  RI                  U%U5      n%Sn&US	:  a  U%R<                  " UX0R>                  /U%R"                  S	S Q76 n%[!        U5       Vs/ s H#  nU RJ                  " U%U   U40 U	=(       d    0 D6PM%     n%n[M        U%6 u  n%n&[*        R,                  " U%SS9n%U(       a  [*        R,                  " U&SS9n&OSn&U(       aB  U R                  RO                  U%UUSS9n%U&b#  U(       a  U R                  RO                  U&UUSS9n&US:X  a@  U R                  RQ                  U%5      n%U&b"  U(       a  U R                  RQ                  U&5      n&U RS                  5         U(       d  U%U&U4$ [U        U%U&US9$ s  snf s  snf )a  
Function invoked when calling the pipeline.

Args:
    image (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`),
        `List[torch.Tensor]`: An input image or images used as an input for the intrinsic decomposition task.
        For arrays and tensors, the expected value range is between `[0, 1]`. Passing a batch of images is
        possible by providing a four-dimensional array or a tensor. Additionally, a list of images of two- or
        three-dimensional arrays or tensors can be passed. In the latter case, all list elements must have the
        same width and height.
    num_inference_steps (`int`, *optional*, defaults to `None`):
        Number of denoising diffusion steps during inference. The default value `None` results in automatic
        selection.
    ensemble_size (`int`, defaults to `1`):
        Number of ensemble predictions. Higher values result in measurable improvements and visual degradation.
    processing_resolution (`int`, *optional*, defaults to `None`):
        Effective processing resolution. When set to `0`, matches the larger input image dimension. This
        produces crisper predictions, but may also lead to the overall loss of global context. The default
        value `None` resolves to the optimal value from the model config.
    match_input_resolution (`bool`, *optional*, defaults to `True`):
        When enabled, the output prediction is resized to match the input dimensions. When disabled, the longer
        side of the output will equal to `processing_resolution`.
    resample_method_input (`str`, *optional*, defaults to `"bilinear"`):
        Resampling method used to resize input images to `processing_resolution`. The accepted values are:
        `"nearest"`, `"nearest-exact"`, `"bilinear"`, `"bicubic"`, or `"area"`.
    resample_method_output (`str`, *optional*, defaults to `"bilinear"`):
        Resampling method used to resize output predictions to match the input resolution. The accepted values
        are `"nearest"`, `"nearest-exact"`, `"bilinear"`, `"bicubic"`, or `"area"`.
    batch_size (`int`, *optional*, defaults to `1`):
        Batch size; only matters when setting `ensemble_size` or passing a tensor of images.
    ensembling_kwargs (`dict`, *optional*, defaults to `None`)
        Extra dictionary with arguments for precise ensembling control. The following options are available:
        - reduction (`str`, *optional*, defaults to `"median"`): Defines the ensembling function applied in
          every pixel location, can be either `"median"` or `"mean"`.
    latents (`torch.Tensor`, *optional*, defaults to `None`):
        Latent noise tensors to replace the random initialization. These can be taken from the previous
        function call's output.
    generator (`torch.Generator`, or `List[torch.Generator]`, *optional*, defaults to `None`):
        Random number generator object to ensure reproducibility.
    output_type (`str`, *optional*, defaults to `"np"`):
        Preferred format of the output's `prediction` and the optional `uncertainty` fields. The accepted
        values are: `"np"` (numpy array) or `"pt"` (torch tensor).
    output_uncertainty (`bool`, *optional*, defaults to `False`):
        When enabled, the output's `uncertainty` field contains the predictive uncertainty map, provided that
        the `ensemble_size` argument is set to a value above 2.
    output_latent (`bool`, *optional*, defaults to `False`):
        When enabled, the output's `latent` field contains the latent codes corresponding to the predictions
        within the ensemble. These codes can be saved, modified, and used for subsequent calls with the
        `latents` argument.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`~pipelines.marigold.MarigoldIntrinsicsOutput`] instead of a plain tuple.

Examples:

Returns:
    [`~pipelines.marigold.MarigoldIntrinsicsOutput`] or `tuple`:
        If `return_dict` is `True`, [`~pipelines.marigold.MarigoldIntrinsicsOutput`] is returned, otherwise a
        `tuple` is returned where the first element is the prediction, the second element is the uncertainty
        (or `None`), and the third is the latent (or `None`).
N 
do_not_padTri   )padding
max_length
truncationreturn_tensorsr   )rr   dtyper   zMarigold predictions...)r   r   )rr   FzDiffusion steps...r   )encoder_hidden_statesr   )r`   r   )is_aar)   )r    r!   r"   )+_execution_devicer   r<   r=   r   rM   r9   model_max_length	input_idstor8   rN   
preprocessprepare_latentsrepeatr   ranger   r7   set_timesteps	timestepsr+   catr5   stepprev_sampleXLA_AVAILABLExm	mark_stepappendreshaperU   r6   rK   rT   decode_predictionunpad_imageensemble_intrinsicszipresize_antialiaspt_to_numpymaybe_free_model_hooksr   )'rO   rW   rX   rY   rZ   r   r[   r\   r]   r^   r_   r`   ra   rb   r   r   rr   r   r   prompttext_inputstext_input_idsr   original_resolutionimage_latentpred_latentbatch_empty_text_embeddingpred_latentsr   batch_image_latentbatch_pred_latenteffective_batch_sizetexttbatch_latentnoisepred_latent_for_decodingr    r!   s'                                          r0   __call__#MarigoldIntrinsicsPipeline.__call__g  s.   d ''

 &"&">"> ($($F$F! &&!!"

" $$,F..$>>::# ) K )2255f=N(,(9(9.(I!(LD% /3.B.B.M.M*?/
+w+ %)$8$8Ij%
!k %)%>%>%A%AW\%A%]%d%d1&
" ""!Z-/<DOh # 
A ".a!j.!A +AJ ?#5#;#;A#> -.C/CDDNN(()<V(L&&t~~'?'?uSg&h$yy*<>O)PVWX		,[`	abcd$(NN$7$71/9 %8 %+ " !=LLN i  12'
* ii!4 & $/#6#6&79X9X$
[f[l[lmnmo[p$
  YY q":"@"@"CZPPA &&'?A
N'STP 

 %K ))55j'J
 1#++M>><F<L<LQR<PJ
 z**A ((A8JhO`Ofdfh*   '*:&6#J:15J!#ii;" "-->>/1Gu ? J &+="22CC!46LTY D 
 $--99*EJ&+="22>>{K 	##%[99'!#
 	
y0s   !Q>*Rc                    S n[         R                  " [        SUR                  S   U5       Vs/ s H(  nU" U R                  R                  XXu-    5      5      PM*     snSS9nXR                  R                  R                  -  nUR                  USS9nUR                  u  ppUnUc/  [        XR                  U
-  X4UUR                  UR                  S9nX4$ s  snf )Nc                     [        U S5      (       a  U R                  R                  5       $ [        U S5      (       a  U R                  $ [	        S5      e)Nlatent_distr_   z3Could not access latents of provided encoder_output)r   r   moder_   AttributeError)encoder_outputs    r0   retrieve_latentsDMarigoldIntrinsicsPipeline.prepare_latents.<locals>.retrieve_latents{  sI    ~}55%11668833%---$%Z[[r/   r   r   )r`   rr   r   )r+   r   r   r   r6   encoderK   scaling_factorrepeat_interleaver   rU   rr   r   )rO   rW   r_   r`   rY   r]   r   r   r   N_ECr   r   r   s                 r0   r   *MarigoldIntrinsicsPipeline.prepare_latentss  s    	\ yy q%++a.*==A !1>1J!KL= 
 $hhoo&D&DD#55m5K#))&nnq(!/##**"((	K (('s   /Cr   c                    UR                  5       S:w  d1  UR                  S   U R                  R                  R                  :w  a:  [        SU R                  R                  R                   SUR                   S35      eU R                  R                  XR                  R                  R                  -  SS9S   n[        R                  " US	S
5      nUS
-   S-  nU$ )Nrn   r   z Expecting 4D tensor of shape [B,z,H,W]; got r?   F)r   r   g      g      ?g       @)
r   r   r6   rK   rT   rx   decoder   r+   clip)rO   r   r    s      r0   r   ,MarigoldIntrinsicsPipeline.decode_prediction  s    ??![%6%6q%9TXX__=\=\%\2488??3R3R2SS^_j_p_p^qqrs  XX__[88??3Q3Q%Q_d_efgh
ZZ
D#6
 3&#-
r/   targetsrj   c                 R   U R                  5       S:w  d  U R                  S   S:w  a  [        SU R                   S35      eUS;  a  [        SU S35      eU R                  u  p4pVnSnUS	:X  a4  [        R                  " U S
S9n	U(       a  [        R
                  " U S
S9nX4$ US:X  ar  [        R                  " U S
SS9R                  n	U(       a7  [        R                  " X	-
  5      n[        R                  " US
S9R                  nU	R                  S
5      n	X4$ [        SU S35      e)a  
Ensembles the intrinsic decomposition represented by the `targets` tensor with expected shape `(B, T, 3, H,
W)`, where B is the number of ensemble members for a given prediction of size `(H x W)`, and T is the number of
predicted targets.

Args:
    targets (`torch.Tensor`):
        Input ensemble of intrinsic image decomposition maps.
    output_uncertainty (`bool`, *optional*, defaults to `False`):
        Whether to output uncertainty map.
    reduction (`str`, *optional*, defaults to `"mean"`):
        Reduction method used to ensemble aligned predictions. The accepted values are: `"median"` and
        `"mean"`.

Returns:
    A tensor of aligned and ensembled intrinsic decomposition maps with shape `(T, 3, H, W)` and optionally a
    tensor of uncertainties of shape `(T, 3, H, W)`.
   r   r   z.Expecting 4D tensor of shape [B,T,3,H,W]; got r?   rk   zUnrecognized reduction method: Nrm   r   r   rl   T)r   keepdim)
r   r   rx   r+   rm   stdrl   valuesabssqueeze)
r   rb   rj   BT_r   r   r!   r    s
             r0   r   .MarigoldIntrinsicsPipeline.ensemble_intrinsics  s"   0 ;;=Aq!1Q!6Mgmm_\]^__..>ykKLLaAG3J!#iiQ7 && ("g1dCJJJ!#ii(<=#ll;A>EE#++A.J && >ykKLLr/   )r   r<   r=   rM   rN   r;   rA   )NNNN)NNNT)Nr   NTrf   rf   r   NNNr)   FFT)Frl   )+r$   r%   r&   r'   r(   model_cpu_offload_seqrD   r   r   r	   r   r   r   r   r   strr   r   intrC   propertyrU   r   r+   r,   r   r   boolr   compilerdisabler   no_gradr   EXAMPLE_DOC_STRINGr   r   r   r   staticmethodr   r.   __classcell__)rP   s   @r0   r2   r2   x   s   $L 6!0 *.6:157;*^"*^ *^ 45	*^
 $*^ !*^ "#*^ $DcN3*^ "*#*^ (0}*^ *^X P P@!@ !@ 	@
  #@  #@ !$@ @ $DcN3@ %,,'@ E%//43H"HIJ@ @ !@ 
@D ^^P P$ ]]_12 .2/3'+%/&06:EIMQ#(# !H
!H
 &c]H
 	H

  (}H
 !%H
  #H
 !$H
 H
 $DcN3H
 %d5<<.@ @ABH
 E%//43H"HIJH
 H
 !H
 H
  !H
 3 H
T$)||$) %,,'$) EOO,	$)
 $) $) 
u||U\\)	*$)LU\\ ell   $)!*'*' *' *' 
u||Xell33	4	*' *'r/   r2   )1dataclassesr   typingr   r   r   r   r   r	   numpyr)   r+   PILr
   	tqdm.autor   transformersr   r   rN   r   modelsr   r   
schedulersr   r   utilsr   r   r   r   utils.torch_utilsr   pipeline_utilsr   marigold_image_processingr   torch_xla.core.xla_modelcore	xla_modelr   r   
get_loggerr$   rE   r   r   r2   r#   r/   r0   <module>r     s   & " : :     5 1  . . = ))MM			H	%" J &z & &0Y	'!2 Y	'r/   