code: def _build_diffusion(self):
      betas = cosine_beta_schedule(self.timesteps)
      alphas = 1.0 - betas
      alphas_cumprod = torch.cumprod(alphas, dim=0)
      alphas_cumprod_prev = F.pad(
          alphas_cumprod[:-1], (1, 0), value=1.0
      )
  
      self.register_buffer("betas", betas)
      self.register_buffer("alphas_cumprod", alphas_cumprod)
      self.register_buffer("alphas_cumprod_prev", alphas_cumprod_prev)
  
      # calculations for diffusion q(x_t | x_{t-1}) and others
      self.register_buffer(
          "sqrt_alphas_cumprod", torch.sqrt(alphas_cumprod)
      )
      self.register_buffer(
          "sqrt_one_minus_alphas_cumprod",
          torch.sqrt(1.0 - alphas_cumprod),
      )
      self.register_buffer(
          "log_one_minus_alphas_cumprod",
          torch.log(1.0 - alphas_cumprod),
      )
      self.register_buffer(
          "sqrt_recip_alphas_cumprod", torch.sqrt(1.0 / alphas_cumprod)
      )
      self.register_buffer(
          "sqrt_recipm1_alphas_cumprod",
          torch.sqrt(1.0 / alphas_cumprod - 1),
      )
  
      # calculations for posterior q(x_{t-1} | x_t, x_0)
      # equal to 1. / (1. / (1. - alpha_cumprod_tm1) + alpha_t / beta_t)
      posterior_variance = (
          betas * (1.0 - alphas_cumprod_prev) / (1.0 - alphas_cumprod)
      )
      self.register_buffer("posterior_variance", posterior_variance)
  
      # log calculation clipped because the posterior variance is 0 at
      # the beginning of the diffusion chain
      self.register_buffer(
          "posterior_log_variance_clipped",
          torch.log(posterior_variance.clamp(min=1e-20)),
      )
      self.register_buffer(
          "posterior_mean_coef1",
          betas
          * torch.sqrt(alphas_cumprod_prev)
          / (1.0 - alphas_cumprod),
      )
      self.register_buffer(
          "posterior_mean_coef2",
          (1.0 - alphas_cumprod_prev)
          * torch.sqrt(alphas)
          / (1.0 - alphas_cumprod),
      )
  
imports: import copy
  import math
  import random
  import warnings
  from typing import Tuple
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import build_activation_layer
  from mmcv.ops import batched_nms
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import bbox2roi, bbox_cxcywh_to_xyxy, bbox_xyxy_to_cxcywh, get_box_wh, scale_boxes
  from mmdet.utils import InstanceList
label_desc: Use image node_id 3 for calling the DynamicDiffusionDetHead obj's underlying member method code with example usage: obj._build_diffusion() without return types