MCPcopy Create free account
hub / github.com/Arm-Examples/ML-examples / ConditionersModule

Class ConditionersModule

kleidiai-examples/audiogen/scripts/model.py:150–224  ·  view source on GitHub ↗

Conditioners Module. Takes the T5 encoder and seconds_total conditioner, and returns the cross-attention inputs and global conditioning inputs.

Source from the content-addressed store, hash-verified

148 return float_embeds, torch.ones(float_embeds.shape[0], 1)
149
150class ConditionersModule(torch.nn.Module):
151 """Conditioners Module. Takes the T5 encoder and seconds_total conditioner,
152 and returns the cross-attention inputs and global conditioning inputs.
153 """
154
155 def __init__(
156 self,
157 sao_t5_cond: torch.nn.Module,
158 sao_seconds_total_cond: torch.nn.Module,
159 dtype: torch.dtype = torch.float
160 ):
161 super().__init__()
162 self.sao_t5 = sao_t5_cond
163 self.sao_seconds_total_cond = ExportableNumberConditioner(
164 sao_seconds_total_cond
165 )
166 self.dtype = dtype
167
168 # Use float
169 self.sao_t5 = force_t5_conditioner_float32(self.sao_t5.to("cpu"))
170 self.sao_t5 = self.sao_t5.to(dtype).eval().requires_grad_(False)
171 self.sao_seconds_total_cond = self.sao_seconds_total_cond.to(dtype=dtype)
172
173 def forward(
174 self,
175 input_ids: torch.Tensor,
176 attention_mask: torch.Tensor,
177 seconds_total: torch.Tensor,
178 ):
179 # Get the projections and conditioner results
180 with torch.no_grad():
181 t5_embeddings = self.sao_t5.model(
182 input_ids=input_ids, attention_mask=attention_mask
183 )["last_hidden_state"]
184 # Resize the embeddings and attention mask to 64 to match DiT model
185 t5_embeddings = t5_embeddings[:, :64, :]
186 attention_mask = attention_mask[:, :64]
187 # Get the T5 projections
188 t5_proj = self.sao_t5.proj_out(t5_embeddings).to(dtype=self.dtype)
189 t5_proj = t5_proj * attention_mask.unsqueeze(-1).to(dtype=self.dtype)
190 t5_mask = attention_mask
191
192 # Get seconds_total conditioner results
193 seconds_total_embedding, seconds_total_mask = self.sao_seconds_total_cond(
194 seconds_total
195 )
196
197 # Concatenate all cross-attention inputs (t5_embedding, seconds_total) over the sequence dimension
198 # Assumes that the cross-attention inputs are of shape (batch, seq, channels)
199 cross_attention_input = torch.cat(
200 [
201 t5_proj,
202 seconds_total_embedding,
203 ],
204 dim=1,
205 )
206 cross_attention_masks = torch.cat(
207 [

Callers 1

get_conditioners_moduleFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected