





# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import importlib.metadata
import json
import os
from dataclasses import dataclass
from typing import Any, Dict, Union

from packaging import version

from ..utils import is_torch_available, logging


if is_torch_available():
     import torch


_snake_case  :   Union[str, Any]						   =  logging.get_logger(__name__)



@dataclass
class     _UpperCAmelCase   :

  """simple docstring"""


  def __init__(    self			:		List[str] ,  lowerCAmelCase_			:		Any=False ,  lowerCAmelCase_			:		List[str]=False ,  lowerCAmelCase_			:		Optional[Any]=6.0 ,  lowerCAmelCase_			:		Dict=None ,  lowerCAmelCase_			:		Optional[int]=False ,  lowerCAmelCase_			:		List[str]=False ,  lowerCAmelCase_			:		List[Any]=None ,  lowerCAmelCase_			:		Optional[int]="fp4" ,  lowerCAmelCase_			:		Optional[Any]=False ,  **lowerCAmelCase_			:		Tuple ,  )							->						List[Any]:
     __lowerCAmelCase							     =     load_in_abit
     __lowerCAmelCase							     =     load_in_abit
     __lowerCAmelCase							     =     llm_inta_threshold
     __lowerCAmelCase							     =     llm_inta_skip_modules
     __lowerCAmelCase							     =     llm_inta_enable_fpaa_cpu_offload
     __lowerCAmelCase							     =     llm_inta_has_fpaa_weight
     __lowerCAmelCase							     =     bnb_abit_quant_type
     __lowerCAmelCase							     =     bnb_abit_use_double_quant

     if bnb_abit_compute_dtype is None:
        __lowerCAmelCase							     =     torch.floataa
     elif isinstance(lowerCAmelCase_ ,  lowerCAmelCase_					):
        __lowerCAmelCase							     =     getattr(lowerCAmelCase_ ,  lowerCAmelCase_					)
     elif isinstance(lowerCAmelCase_ ,  torch.dtype					):
        __lowerCAmelCase							     =     bnb_abit_compute_dtype
     else:
        raise ValueError('bnb_4bit_compute_dtype must be a string or a torch.dtype'					)

     self.post_init()






  def     lowercase		(    self			:		Optional[int]					)							->						Union[str, Any]:
     if not isinstance(self.llm_inta_threshold ,  lowerCAmelCase_					):
        raise ValueError('llm_int8_threshold must be a float'					)

     if self.llm_inta_skip_modules is not None and not isinstance(self.llm_inta_skip_modules ,  lowerCAmelCase_					):
        raise ValueError('llm_int8_skip_modules must be a list of strings'					)
     if not isinstance(self.llm_inta_enable_fpaa_cpu_offload ,  lowerCAmelCase_					):
        raise ValueError('llm_int8_enable_fp32_cpu_offload must be a boolean'					)

     if not isinstance(self.llm_inta_has_fpaa_weight ,  lowerCAmelCase_					):
        raise ValueError('llm_int8_has_fp16_weight must be a boolean'					)

     if self.bnb_abit_compute_dtype is not None and not isinstance(self.bnb_abit_compute_dtype ,  torch.dtype					):
        raise ValueError('bnb_4bit_compute_dtype must be torch.dtype'					)

     if not isinstance(self.bnb_abit_quant_type ,  lowerCAmelCase_					):
        raise ValueError('bnb_4bit_quant_type must be a string'					)

     if not isinstance(self.bnb_abit_use_double_quant ,  lowerCAmelCase_					):
        raise ValueError('bnb_4bit_use_double_quant must be a boolean'					)

     if self.load_in_abit and not version.parse(importlib.metadata.version('bitsandbytes'					)					) >= version.parse(
         '0.39.0'					):
        raise ValueError(
            '4 bit quantization requires bitsandbytes>=0.39.0 - please upgrade your bitsandbytes version'					)






  def     lowercase		(    self			:		Optional[Any]					)							->						Any:
     return self.load_in_abit or self.load_in_abit






  def     lowercase		(    self			:		Any					)							->						Dict:
     if self.load_in_abit:
        return "llm_int8"
     elif self.load_in_abit and self.bnb_abit_quant_type == "fp4":
        return "fp4"
     elif self.load_in_abit and self.bnb_abit_quant_type == "nf4":
        return "nf4"
     else:
        return None






  @classmethod
  def     lowercase		(    cls			:		Optional[int] ,  lowerCAmelCase_			:		Any ,  lowerCAmelCase_			:		Any ,  **lowerCAmelCase_			:		Optional[int]					)							->						Any:
     __lowerCAmelCase							     =     cls(**lowerCAmelCase_					)

     __lowerCAmelCase							     =     []
     for key, value in kwargs.items():
        if hasattr(lowerCAmelCase_ ,  lowerCAmelCase_					):
           setattr(lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_					)
           to_remove.append(lowerCAmelCase_					)
     for key in to_remove:
        kwargs.pop(lowerCAmelCase_ ,  lowerCAmelCase_					)

     if return_unused_kwargs:
        return config, kwargs
     else:
        return config






  def     lowercase		(    self			:		Any ,  lowerCAmelCase_			:		Union[str, os.PathLike]					)							->						List[str]:
     with open(lowerCAmelCase_ ,  'w' ,  encoding='utf-8'					) as writer:
        __lowerCAmelCase							     =     self.to_dict()
        __lowerCAmelCase							     =     json.dumps(lowerCAmelCase_ ,  indent=2 ,  sort_keys=lowerCAmelCase_					) + '\n'

        writer.write(lowerCAmelCase_					)






  def     lowercase		(    self			:		Union[str, Any]					)							->						Dict[str, Any]:
     __lowerCAmelCase							     =     copy.deepcopy(self.__dict__					)
     __lowerCAmelCase							     =     str(output['bnb_4bit_compute_dtype']					).split('.'					)[1]

     return output






  def __repr__(    self			:		Tuple					)							->						str:
     return f"""{self.__class__.__name__} {self.to_json_string()}"""






  def     lowercase		(    self			:		List[str] ,  lowerCAmelCase_			:		bool = True					)							->						str:
     if use_diff is True:
        __lowerCAmelCase							     =     self.to_diff_dict()
     else:
        __lowerCAmelCase							     =     self.to_dict()
     return json.dumps(lowerCAmelCase_ ,  indent=2 ,  sort_keys=lowerCAmelCase_					) + "\n"






  def     lowercase		(    self			:		Optional[Any]					)							->						Dict[str, Any]:
     __lowerCAmelCase							     =     self.to_dict()

     # get the default config dict
     __lowerCAmelCase							     =     BitsAndBytesConfig().to_dict()

     __lowerCAmelCase							     =     {}

     # only serialize values that differ from the default config
     for key, value in config_dict.items():
        if value != default_config_dict[key]:
           __lowerCAmelCase							     =     value

     return serializable_config_dict








from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .modeling_utils import ModelMixin



@dataclass
class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			42





class     _UpperCAmelCase   (   _UpperCamelCase		,   _UpperCamelCase   ):

  """simple docstring"""


  @register_to_config
  def __init__(    self			:		Optional[int] ,  lowerCAmelCase_			:		int = 1_6 ,  lowerCAmelCase_			:		int = 8_8 ,  lowerCAmelCase_			:		Optional[int] = None ,  lowerCAmelCase_			:		Optional[int] = None ,  lowerCAmelCase_			:		int = 1 ,  lowerCAmelCase_			:		float = 0.0 ,  lowerCAmelCase_			:		int = 3_2 ,  lowerCAmelCase_			:		Optional[int] = None ,  lowerCAmelCase_			:		bool = False ,  lowerCAmelCase_			:		Optional[int] = None ,  lowerCAmelCase_			:		str = "geglu" ,  lowerCAmelCase_			:		bool = True ,  lowerCAmelCase_			:		bool = True ,  )							->						Dict:
     super().__init__()
     __lowerCAmelCase							     =     num_attention_heads
     __lowerCAmelCase							     =     attention_head_dim
     __lowerCAmelCase							     =     num_attention_heads * attention_head_dim

     __lowerCAmelCase							     =     in_channels

     __lowerCAmelCase							     =     torch.nn.GroupNorm(num_groups=lowerCAmelCase_ ,  num_channels=lowerCAmelCase_ ,  eps=1e-6 ,  affine=lowerCAmelCase_					)
     __lowerCAmelCase							     =     nn.Linear(lowerCAmelCase_ ,  lowerCAmelCase_					)

     # 3. Define transformers blocks
     __lowerCAmelCase							     =     nn.ModuleList(
         [
             BasicTransformerBlock(
                 lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_ ,  dropout=lowerCAmelCase_ ,  cross_attention_dim=lowerCAmelCase_ ,  activation_fn=lowerCAmelCase_ ,  attention_bias=lowerCAmelCase_ ,  double_self_attention=lowerCAmelCase_ ,  norm_elementwise_affine=lowerCAmelCase_ ,  )
             for d in range(lowerCAmelCase_					)
         ]					)

     __lowerCAmelCase							     =     nn.Linear(lowerCAmelCase_ ,  lowerCAmelCase_					)






  def     lowercase		(    self			:		Dict ,  lowerCAmelCase_			:		Tuple ,  lowerCAmelCase_			:		List[Any]=None ,  lowerCAmelCase_			:		Optional[int]=None ,  lowerCAmelCase_			:		Union[str, Any]=None ,  lowerCAmelCase_			:		Optional[int]=1 ,  lowerCAmelCase_			:		Optional[int]=None ,  lowerCAmelCase_			:		bool = True ,  )							->						str:
     __lowerCAmelCase     ,     __lowerCAmelCase     ,     __lowerCAmelCase     ,     __lowerCAmelCase							     =     hidden_states.shape
     __lowerCAmelCase							     =     batch_frames // num_frames

     __lowerCAmelCase							     =     hidden_states

     __lowerCAmelCase							     =     hidden_states[None, :].reshape(lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_					)
     __lowerCAmelCase							     =     hidden_states.permute(0 ,  2 ,  1 ,  3 ,  4					)

     __lowerCAmelCase							     =     self.norm(lowerCAmelCase_					)
     __lowerCAmelCase							     =     hidden_states.permute(0 ,  3 ,  4 ,  2 ,  1					).reshape(batch_size * height * width ,  lowerCAmelCase_ ,  lowerCAmelCase_					)

     __lowerCAmelCase							     =     self.proj_in(lowerCAmelCase_					)

     # 2. Blocks
     for block in self.transformer_blocks:
        __lowerCAmelCase							     =     block(
            lowerCAmelCase_ ,  encoder_hidden_states=lowerCAmelCase_ ,  timestep=lowerCAmelCase_ ,  cross_attention_kwargs=lowerCAmelCase_ ,  class_labels=lowerCAmelCase_ ,  )

     # 3. Output
     __lowerCAmelCase							     =     self.proj_out(lowerCAmelCase_					)
     __lowerCAmelCase							     =     (
         hidden_states[None, None, :]
         .reshape(lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_					)
         .permute(0 ,  3 ,  4 ,  1 ,  2					)
         .contiguous()
     )
     __lowerCAmelCase							     =     hidden_states.reshape(lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_ ,  lowerCAmelCase_					)

     __lowerCAmelCase							     =     hidden_states + residual

     if not return_dict:
        return (output,)

     return TransformerTemporalModelOutput(sample=lowerCAmelCase_					)

